Skip to content

Commit

Permalink
Merge pull request hayes#781 from hayes/mh--relay-enforce-global-id-t…
Browse files Browse the repository at this point in the history
…ypes

Add option for enforcing type of for globalID inputs
  • Loading branch information
hayes authored Feb 7, 2023
2 parents bce9778 + 0737488 commit cd2c866
Show file tree
Hide file tree
Showing 25 changed files with 505 additions and 101 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-lobsters-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-relay': minor
---

Add new parse option for id field on nodes, and a `for` option on globalID args
40 changes: 40 additions & 0 deletions packages/deno/packages/plugin-relay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ globalIDs used in arguments expect the client to send a globalID string, but wil
converted to an object with 2 properties (`id` and `typename`) before they are passed to your
resolver in the arguments object.

#### Limiting global ID args to specific types

`globalID` input's can be configured to validate the type of the globalID. This is useful if you
only want to accept IDs for specific node types.

```typescript
builder.queryType({
fields: (t) => ({
fieldThatAcceptsGlobalID: t.boolean({
args: {
id: t.arg.globalID({
for: SomeType,
// or allow multiple types
for: [TypeOne, TypeTwo],
required: true,
}),
},
}),
}),
});
```

### Creating Nodes

To create objects that extend the `Node` interface, you can use the new `builder.node` method.
Expand Down Expand Up @@ -168,6 +190,24 @@ The means that for many cases if you are using classes in your type parameters,
are instances of those classes, you won't need to implement an `isTypeOf` method, but it is usually
better to explicitly define that behavior.

#### parsing node ids

By default all node ids are parsed as string. This behavior can be customized by providing a custom
parse function for your node's ID field:

```ts
builder.node(NumberThing, {
// define an id field
id: {
resolve: (num) => num.id,
parse: (id) => Number.parseInt(id, 10),
},
// the ID is now a number
loadOne: (id) => new NumberThing(id),
...
});
```

### Creating Connections

The `t.connection` field builder method can be used to define connections. This method will
Expand Down
4 changes: 2 additions & 2 deletions packages/deno/packages/plugin-relay/field-builder.ts

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

15 changes: 8 additions & 7 deletions packages/deno/packages/plugin-relay/global-types.ts

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

21 changes: 14 additions & 7 deletions packages/deno/packages/plugin-relay/index.ts

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

25 changes: 16 additions & 9 deletions packages/deno/packages/plugin-relay/input-field-builder.ts

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

13 changes: 13 additions & 0 deletions packages/deno/packages/plugin-relay/node-ref.ts

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

17 changes: 13 additions & 4 deletions packages/deno/packages/plugin-relay/schema-builder.ts

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

Loading

0 comments on commit cd2c866

Please sign in to comment.