Skip to content

Commit

Permalink
refactor!: Move as_id out of TypeNode (#866)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squashed and merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog.
-->

<!-- 1. Explain WHAT the change is about -->

Solve
[MET-684](https://linear.app/metatypedev/issue/MET-684/store-the-id-field-in-tstruct-instead-of-in-the-target-type-as-id)
and
[MET-471](https://linear.app/metatypedev/issue/MET-471/parent-injection-use-property-name-instead-of-function-name)
- **common/typegraph**
- [x] Store the id field in `ObjectTypeData` instead of in the target
type (`as_id`)
  - [x] Add `id()` method on `t.integer` and `t.string`
- **typegraph/core**
  - [x] Store `as_id`, `injection` and `policy` in `TypeRef::attribute`
  - [x] Add support for direct and link target in `TypeRef`
  - [x] Only allow name registration for `TypeDef`
- Semantics
- [x] Use property name instead of type name in from_parent injection
source

#### Migration notes
**BREAKING CHANGE**
`from_parent` injections source shall be changed to the key in the
parent `t.struct` instead of the type name.


#### Checklist

- [x] The change comes with new or modified tests
- [x] Hard-to-understand functions have explanatory comments
- [x] End-user documentation is updated to reflect the change
  • Loading branch information
Natoandro authored Oct 11, 2024
1 parent 9aefe89 commit cae1342
Show file tree
Hide file tree
Showing 174 changed files with 20,379 additions and 20,154 deletions.
134 changes: 50 additions & 84 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions docs/metatype.dev/docs/reference/types/injections.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ Injection is a mechanism to get the value of a parameter from other sources than

## Parent

**Description:** Parent injection gets the value output of a sibling field in the parent struct. It adds a dependency to the sibling field, so make sure to prevent circular dependencies.
**Description:** Parent injection gets the value output of a sibling field (_source_) in the parent struct. It adds a dependency to the sibling field, so make sure to prevent circular dependencies.
Additionally, it requires that the source type is a [subtype of](/docs/reference/types/comparison) the target type.

**Method:** `.from_parent(type_name)`
Note: the parent struct is relative to the function in which the current type is an input.

**Parameter:** The type name of the sibling field in the parent struct.
**Method:** `.from_parent(key)`

**Parameter:** The name (key) of the source field in the parent struct.

## Context

Expand Down
2 changes: 1 addition & 1 deletion examples/typegraphs/backend-for-frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def backend_for_frontend(g: Graph):

stargazer = t.struct(
{
"login": t.string(name="login"),
"login": t.string(),
"user": github.get(
"/users/{user}",
t.struct({"user": t.string().from_parent("login")}),
Expand Down
6 changes: 3 additions & 3 deletions examples/typegraphs/backend-for-frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ await typegraph(
const pub = Policy.public();

const stargazer = t.struct({
login: t.string({}, { name: "login" }),
login: t.string({}),
user: github.get(
t.struct({ user: t.string().fromParent("login") }),
t.struct({ name: t.string().optional() }),
{ path: "/users/{user}" }
{ path: "/users/{user}" },
),
});

Expand All @@ -30,5 +30,5 @@ await typegraph(
})
.withPolicy(pub),
});
}
},
);
6 changes: 2 additions & 4 deletions examples/typegraphs/func-gql.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,15 @@ def func_gql(g: Graph):
),
t.struct(
{
# rename here is necessary to make
# `fromParent` down below work
"voteId": t.uuid().rename("Vote_id"),
"voteId": t.uuid(),
# using `reduce` we improve the API allowing
# create calls to get the newly created object
# without having to send this data from the
# custom funciton
"vote": db.find_unique(vote).reduce(
{
"where": {
"id": g.inherit().from_parent("Vote_id"),
"id": g.inherit().from_parent("voteId"),
},
}
),
Expand Down
18 changes: 8 additions & 10 deletions examples/typegraphs/func-gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ await typegraph(
authorEmail: t.email(),
votes: t.list(g.ref("vote")),
},
{ name: "idea" }
{ name: "idea" },
);
const vote = t.struct(
{
id: t.uuid({ asId: true, config: { auto: true } }),
authorEmail: t.email(),
idea: g.ref("idea"),
},
{ name: "vote" }
{ name: "vote" },
);

// Policy.internal means only custom functions
Expand All @@ -40,7 +40,7 @@ await typegraph(
i_get_idea: db.findUnique(idea),
i_create_vote: db.create(vote),
},
Policy.internal()
Policy.internal(),
);

g.expose(
Expand All @@ -52,16 +52,14 @@ await typegraph(
.rename("CreateVoteInput"),
t
.struct({
// rename here is necessary to make
// `fromParent` down below work
voteId: t.uuid().rename("Vote_id"),
voteId: t.uuid(),
// using `reduce` we improve the API allowing
// create calls to get the newly created object
// without having to send this data from the
// custom funciton
vote: db.findUnique(vote).reduce({
where: {
id: g.inherit().fromParent("Vote_id"),
id: g.inherit().fromParent("voteId"),
},
}),
})
Expand All @@ -70,11 +68,11 @@ await typegraph(
module: "scripts/createVote.ts",
name: "handle", // name the exported function to run
effect: fx.create(),
}
},
),
},
Policy.public()
Policy.public(),
);
// skip:start
}
},
);
2 changes: 1 addition & 1 deletion examples/typegraphs/graphql-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def graphql_server(g: Graph):

stargazer = t.struct(
{
"login": t.string().rename("login"),
"login": t.string(),
"user": github.get(
"/users/{user}",
t.struct({"user": t.string().from_parent("login")}),
Expand Down
Loading

0 comments on commit cae1342

Please sign in to comment.