-
Notifications
You must be signed in to change notification settings - Fork 1.1k
RFC: OneOf Input Objects #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 20 commits
c385058
f6bd659
39e593c
b6741c3
d17d5ec
dca3826
7e02f5a
4111476
6754e0a
7c4c1a2
bb225f7
05fde06
e8f6145
08abf49
59cb12d
c470afb
99aa5d9
691087d
7109dbc
05ab541
6a6be52
de87d2f
57e2388
5a966f2
e78d2b5
c6cd857
d106233
87d0b22
d88d62a
a810aef
a1563a9
b45c0e4
0c9830e
c4d0b50
340594e
dbccf84
17f1304
d15f7bb
6310475
c26463f
ff986be
016f47f
b1ff1ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ words: | |
- tatooine | ||
- zuck | ||
- zuckerberg | ||
- brontie | ||
- oneOf |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,10 @@ type Query { | |
dog: Dog | ||
} | ||
|
||
type Mutation { | ||
addPet(pet: PetInput): Pet | ||
} | ||
|
||
enum DogCommand { | ||
SIT | ||
DOWN | ||
|
@@ -87,6 +91,23 @@ type Cat implements Pet { | |
union CatOrDog = Cat | Dog | ||
union DogOrHuman = Dog | Human | ||
union HumanOrAlien = Human | Alien | ||
|
||
input CatInput { | ||
name: String! | ||
nickname: String | ||
meowVolume: Int | ||
} | ||
|
||
input DogInput { | ||
name: String! | ||
nickname: String | ||
barkVolume: Int | ||
} | ||
|
||
input PetInput @oneOf { | ||
cat: CatInput | ||
dog: DogInput | ||
} | ||
``` | ||
|
||
## Documents | ||
|
@@ -1406,6 +1427,103 @@ arguments, an input object may have required fields. An input field is required | |
if it has a non-null type and does not have a default value. Otherwise, the | ||
input object field is optional. | ||
|
||
### OneOf Input Objects Have Exactly One Field | ||
|
||
**Formal Specification** | ||
|
||
- For each {operation} in {document}: | ||
- Let {oneofInputObjects} be all OneOf Input Objects transitively included in | ||
the {operation}. | ||
- For each {oneofInputObject} in {oneofInputObjects}: | ||
- Let {fields} be the fields provided by {oneofInputObject}. | ||
benjie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- {fields} must contain exactly one entry. | ||
- Let {field} be the sole entry in {fields}. | ||
- Let {value} be the value of {field}. | ||
- {value} must not be the {null} literal. | ||
- If {value} is a variable: | ||
- Let {variableName} be the name of {variable}. | ||
- Let {variableDefinition} be the {VariableDefinition} named | ||
{variableName} defined within {operation}. | ||
- Let {variableType} be the expected type of {variableDefinition}. | ||
- {variableType} must be a non-null type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if these lines relating to variable usage pre-empt the discussion around #1059 and should be pulled from this spec change (simplifying it). Variables must be only of the allowed type, but it seems that we should specify what that entails for all variables and types only in one place, i.e. the separate rule. So if we currently allow variables of nullable types to be used in non-null positions and throw a field error at runtime -- which we do -we should continue to do so irrespective of isOneOf, and if/when we make the change there, that should be done in a way that covers isOneOf as well. Encountered this while attempting to rebase graphql/graphql-js#3813 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two arguments against this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't think so? Technically all fields on a oneof are nullable, but you must specify one and it must be non-null, so this seems a very straightforward way to require that when it comes to a variable? #1059 handles non-null positions, but this is a nullable position according to the type system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So it's a nullable type only because we want introspection to say that it's nullable because currently that's the only way of making something optional. But a null cannot be supplied, so in a sense it's by definition "a non-nullable position." So we would then have to introduce the concept of non-nullable positions that occur when (1) the type for the position is non-nullable or (2) the containing type is oneOf, and then the general rule about matching nullable and non-nullable would have to depend on this new "position" concept rather than the type itself. As I type this, I can see that this additional layer is a bad idea, and I appreciate the compromise that you have ended up with. On the other hand, in GraphQL 2.0 / TreeQL, we should definitely separate optionality and nullability, and remember to change oneOf to be better defined. (It really shouldn't be the case that you cannot use null at a nullable position.) |
||
|
||
**Explanatory Text** | ||
|
||
OneOf Input Objects require that exactly one field must be supplied and that | ||
field must not be {null}. | ||
|
||
An empty OneOf Input Object is invalid. | ||
|
||
```graphql counter-example | ||
mutation addPet { | ||
addPet(pet: {}) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
Multiple fields are not allowed. | ||
|
||
```graphql counter-example | ||
mutation addPet($cat: CatInput, $dog: DogInput) { | ||
addPet(pet: { cat: $cat, dog: $dog }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
```graphql counter-example | ||
mutation addPet($dog: DogInput) { | ||
addPet(pet: { cat: { name: "Brontie" }, dog: $dog }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
```graphql counter-example | ||
mutation addPet { | ||
addPet(pet: { cat: { name: "Brontie" }, dog: null }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
Variables used for OneOf Input Object fields must be non-nullable. | ||
|
||
```graphql example | ||
mutation addPet($cat: CatInput!) { | ||
addPet(pet: { cat: $cat }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
```graphql counter-example | ||
mutation addPet($cat: CatInput) { | ||
addPet(pet: { cat: $cat }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
If a field with a literal value is present then the value must not be {null}. | ||
|
||
```graphql example | ||
mutation addPet { | ||
addPet(pet: { cat: { name: "Brontie" } }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
```graphql counter-example | ||
mutation addPet { | ||
addPet(pet: { cat: null }) { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
## Directives | ||
|
||
### Directives Are Defined | ||
|
Uh oh!
There was an error while loading. Please reload this page.