Skip to content

Commit 34a6ed7

Browse files
JoviDeCroockbenjie
andcommitted
Apply suggestions from code review
Co-authored-by: Benjie <[email protected]>
1 parent 6503fd6 commit 34a6ed7

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

cspell.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ words:
5959
- tailwindcss
6060
- svgr
6161
- ruru
62+
- oneof
6263

6364
# used as href anchors
6465
- graphqlerror

website/pages/_meta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const meta = {
1717
title: 'Advanced Guides',
1818
},
1919
'constructing-types': '',
20-
'input-unions': '',
20+
'oneof-input-objects': 'OneOf input objects',
2121
'defer-stream': '',
2222
'-- 3': {
2323
type: 'separator',

website/pages/input-unions.mdx renamed to website/pages/oneof-input-objects.mdx

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
2-
title: Input Unions
2+
title: OneOf input objects
33
---
44

55
import { Tabs } from 'nextra/components';
66

77
Some inputs will behave differently depending on what input we choose. Let's look at the case for
88
a field named `product`, we can fetch a `Product` by either its `id` or its `name`. Currently we'd
99
make a tradeoff for this by introducing two arguments that are both nullable, now if both are passed
10-
as null we'd have to handle that in code. To fix this the `@oneOf` directive was introduced so we
11-
can create these input-unions without sacrificing the strictly typed nature of our GraphQL Schema.
10+
as null (or both non-null) we'd have to handle that in code - the type system wouldn't indicate that exactly one was required. To fix this, the `@oneOf` directive was introduced so we
11+
can create this "exactly one option" constraint without sacrificing the strictly typed nature of our GraphQL Schema.
1212

1313
<Tabs items={['SDL', 'Code']}>
1414
<Tabs.Tab>
@@ -19,13 +19,20 @@ const schema = buildSchema(`
1919
name: String!
2020
}
2121
22-
input ProductInput @oneOf {
22+
input ProductLocation {
23+
aisleNumber: Int!
24+
shelfNumber: Int!
25+
positionOnShelf: Int!
26+
}
27+
28+
input ProductSpecifier @oneOf {
2329
id: ID
2430
name: String
31+
location: ProductLocation
2532
}
2633
2734
type Query {
28-
product(input: ProductInput!): Product
35+
product(by: ProductSpecifier!): Product
2936
}
3037
`);
3138
```
@@ -44,12 +51,23 @@ const Product = new GraphQLObjectType({
4451
},
4552
});
4653

47-
const ProductInput = new GraphQLInputObjectType({
48-
name: 'ProductInput',
54+
const ProductLocation = new GraphQLInputObjectType({
55+
name: 'ProductLocation',
56+
isOneOf: true,
57+
fields: {
58+
aisleNumber: { type: GraphQLInt },
59+
shelfNumber: { type: GraphQLInt },
60+
positionOnShelf: { type: GraphQLInt },
61+
},
62+
});
63+
64+
const ProductSpecifier = new GraphQLInputObjectType({
65+
name: 'ProductSpecifier',
4966
isOneOf: true,
5067
fields: {
5168
id: { type: GraphQLID },
5269
name: { type: GraphQLString },
70+
location: { type: ProductLocation },
5371
},
5472
});
5573

@@ -59,7 +77,7 @@ const schema = new GraphQLSchema({
5977
fields: {
6078
product: {
6179
type: Product,
62-
args: { input: { type: ProductInput } },
80+
args: { by: { type: ProductSpecifier } },
6381
},
6482
},
6583
}),
@@ -69,4 +87,5 @@ const schema = new GraphQLSchema({
6987
</Tabs>
7088

7189
It doesn't matter whether you have 2 or more inputs here, all that matters is
72-
that your user will have to specify one, and only one, for this input to be valid.
90+
that your user will have to specify one, and only one, for this input to be valid.
91+
The values are not limited to scalars, lists and other input object types are also allowed.

0 commit comments

Comments
 (0)