You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/pages/oneof-input-objects.mdx
+28-9Lines changed: 28 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
1
---
2
-
title: Input Unions
2
+
title: OneOf input objects
3
3
---
4
4
5
5
import { Tabs } from'nextra/components';
6
6
7
7
Some inputs will behave differently depending on what input we choose. Let's look at the case for
8
8
a field named `product`, we can fetch a `Product` by either its `id` or its `name`. Currently we'd
9
9
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.
12
12
13
13
<Tabsitems={['SDL', 'Code']}>
14
14
<Tabs.Tab>
@@ -19,13 +19,20 @@ const schema = buildSchema(`
19
19
name: String!
20
20
}
21
21
22
-
input ProductInput @oneOf {
22
+
input ProductLocation {
23
+
aisleNumber: Int!
24
+
shelfNumber: Int!
25
+
positionOnShelf: Int!
26
+
}
27
+
28
+
input ProductSpecifier @oneOf {
23
29
id: ID
24
30
name: String
31
+
location: ProductLocation
25
32
}
26
33
27
34
type Query {
28
-
product(input: ProductInput!): Product
35
+
product(by: ProductSpecifier!): Product
29
36
}
30
37
`);
31
38
```
@@ -44,12 +51,23 @@ const Product = new GraphQLObjectType({
44
51
},
45
52
});
46
53
47
-
constProductInput=newGraphQLInputObjectType({
48
-
name:'ProductInput',
54
+
constProductLocation=newGraphQLInputObjectType({
55
+
name:'ProductLocation',
56
+
isOneOf:true,
57
+
fields: {
58
+
aisleNumber: { type: GraphQLInt },
59
+
shelfNumber: { type: GraphQLInt },
60
+
positionOnShelf: { type: GraphQLInt },
61
+
},
62
+
});
63
+
64
+
constProductSpecifier=newGraphQLInputObjectType({
65
+
name:'ProductSpecifier',
49
66
isOneOf:true,
50
67
fields: {
51
68
id: { type: GraphQLID },
52
69
name: { type: GraphQLString },
70
+
location: { type: ProductLocation },
53
71
},
54
72
});
55
73
@@ -59,7 +77,7 @@ const schema = new GraphQLSchema({
59
77
fields: {
60
78
product: {
61
79
type: Product,
62
-
args: { input: { type:ProductInput } },
80
+
args: { by: { type:ProductSpecifier } },
63
81
},
64
82
},
65
83
}),
@@ -69,4 +87,5 @@ const schema = new GraphQLSchema({
69
87
</Tabs>
70
88
71
89
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