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
RFC: OneOf Input Objects #825
base: main
Are you sure you want to change the base?
Conversation
Is |
I’d worry that statements around type safety are a little hard to apply in practice. It’s not the case typically that a directive would change a types underlying type yet type PetInput = { cat?: CatInput; dog?: DogInput; fish?: FishInput; } When instead I’d expect it to do something like: type PetInput = { cat: CatInput; } | { dog: DogInput; } | { fish: FishInput; }; I totally understand the motivation around the change to make it as low impact as possible, but I'd worry about the adverse side affects introduced by this subtle change to the ways that the null/non-null properties are determined. Maybe I’m just applying my understanding incorrectly, but I’d hope that any adoption doesn’t in fact mutate the type system of GraphQL using directives like this. |
@wyfo Thanks, fixed! @wyattjoh It’s not a directive, it’s a new type system constraint that DOES model the type of the input differently and would have different types generated. Have a look at the alternative syntaxes document for other ways this could be exposed via SDL and let us know your preference, perhaps you would prefer the oneof keyword to make it clearer (in SDL only, this would not affect introspection) the change in behaviour. |
It looks like an existing syntax, but the semantics are different? I am worried that if it will end up asking for dirty exception handling for every directive code path.
Could we consider a new syntax that hasn't been mentioned? type Query {
user(id: ID!): User
user(email: String!): User
user(username: String!): User
user(registrationNumber: Int!): User
} pros?:
cons:
|
@cometkim Can you show how that syntax would be expanded to input objects too, please? And yes we can absolutely consider alternative syntaxes. |
Why should it be something else than a directive? Actually, it's already (almost) possible to implement By the way, GraphQL schema is kind of poor in validation stuff (compared to JSON schema for example), so part of the validation is already done by the resolvers/scalar parsing methods. In a schema-first approach, you can also defines directives for repetitive checks, maybe with JSON schema-like annotations, but your code/library will have to translate and inject them into your resolvers/scalar types(/input types when the mentioned proposal will pass). In fact, I don't really see the interest of making |
For input types |
spec/Section 4 -- Introspection.md
Outdated
@@ -156,6 +159,7 @@ type __Field { | |||
type: __Type! | |||
isDeprecated: Boolean! | |||
deprecationReason: String | |||
oneArgument: Boolean! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or oneArg
to inline with args
?
spec/Section 5 -- Validation.md
Outdated
* {arguments} must contain exactly one entry. | ||
* For the sole {argument} in {arguments}: | ||
* Let {value} be the value of {argument}. | ||
* {value} must not be the {null} literal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the word literal appropriate here in case of using variables? The same question about Oneof for Input Object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe so; I've modeled it on the language already used in this section, namely: https://spec.graphql.org/draft/#sel-LALTHHDHFFFJDAAACDJ-3S
@benjie I don't understand. You wrote about |
Another nesting level; i.e. instead of querying like: {
allEntities {
... on User { username }
... on Pet { name }
... on Car { registrationNumber }
... on Building { numberOfFloors }
}
} it'd look like: {
allEntities {
user { username }
pet { name }
car { registrationNumber }
building { numberOfFloors }
}
} |
The input union working group have not decided what syntax to use for oneOf yet. It might end up as being presented as a directive, or it might be a keyword or any other combination of things. Check out this document for alternatives: https://gist.github.com/benjie/5e7324c64f42dd818b9c3ac2a91b6b12 and note that whichever alternative you pick only affects the IDL, it does not affect the functionality or appearance of GraphQL operations, validation, execution, etc. Please see the FAQ above. TL;DR: do not judge the functionality of this RFC by its current IDL syntax. We can change the IDL syntax. |
OK. In my opinion if something is presented as a directive than ... it is just a directive. |
Thanks for the review @sungam3r; good to have additional scrutiny! I don't think any modifications to the RFC are required to address your concerns (other than perhaps writing an alternative IDL syntax, but I don't plan to invest time in that until there's general concensus on what the syntax should be, for now the directive syntax can act as a placeholder). I think all the conversations in your review can be closed except for the |
spec/Section 3 -- Type System.md
Outdated
`$var` | `{ var: { a: "abc" } }` | `{ a: "abc" }` | ||
`{ a: "abc", b: null }` | `{}` | Error: Exactly one key must be specified | ||
`{ b: $var }` | `{ var: null }` | Error: Value for member field {b} must be non-null | ||
`{ b: 123, c: "xyz" }` | `{}` | Error: Exactly one key must be specified |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing { a: $varA, b: $varB }
with various combinations of values for varA and varB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My in meeting proposal was that this case could just be invalid at start.
This L1441 in Validation file in this PR sounds like it would do just that:
https://github.com/graphql/graphql-spec/pull/825/files#diff-607ee7e6b71821eecadde7d92451b978e8a75e23d596150950799dc5f8afa43eR1441
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are exactly the same as for input objects (which also don't specify what happens if you have multiple variables); but I'll add some for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leebyron Good catch; that was not my intent. I have updated the PR with better validation and more examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've since revisited my thoughts on this and for the sake of defining types of variables on the client I've adopted the suggestion: #825 (comment)
Yep, I agree. I was just trying to illustrate, not propose an alternative, at least not yet.
Not sure what you're trying to point out here. (1) isn't a constraint of
I disagree with that. I illustrated it to help readers connect the dots but |
It's a constraint that comes from the mapping to fields.
The constraint is across all the fields in the union types. For example, it is not possible to have the type union
I tried to show above that there is a 1-1-correspondence between |
Indeed, if all fields are nullable then there's no way to discern what input AnimalOwnership {
animal: Animal!
durationInDays: Int
# ...
}
input Animal @oneOf {
cat: Cat
dog: Dog
} |
Yes, that would work but doesn't let you reuse All I wanted was to point out that typed unions are essentially equivalent, in the sense that everything you can model using on of the construction can be achieved in the other construction as well:
Although equivalent, I think the typed union yields easier to understand code and less nesting. |
- 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
There are two arguments against this:
- The benefit of the current relaxed version of
VariablesInAllowedPositions
is that you can use a variable for an argument, not supply the value, and get the default. But with OneOf, default values are not allowed for any fields, so treating this as the same as the general case would only be sensical if (a) we adopt the strict version of the general rule (b) we can convince ourselves that there is a real value in consistency almost for consistency's case. - We can only consider this to be a specific case of the general rule if the
@oneof
directive is held to transform all of the input object's field types into non-nullable (but still not required) types. Then, these become non-nullable positions. There is a certain ambiguity as to whether the field types themselves are nullable or not. By syntax, we want to make sure older clients can leave them out, and so we define them to be nullable. But for clients aware of@oneof
, presumably we are ok to define them as non-nullable, with the caveat that there would have to be a change to theIsRequiredInputField
algorithm. Currently, an input object field is required if is of a non-null type and does not have a default value. This would have to be changed to have an additional condition, that the parent input object does not have isOneOf to be true. Note thatIsRequiredInputField
is part of graphql-js as a utility, and referenced many times in the spec, but does not form a formal agorithm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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).
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 comment
The reason will be displayed to describe this comment to others. Learn more.
and it must be non-null
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.)
If you want stronger typing, can't you do: mutation AddPet ($pet: PetInput) {
addPet(pet: $petInput) { name }
} Although I agree that the stricter initial approach is best, and we could always relax later! |
Isn't that exactly what Lee's proposing? Requiring people to pass oneOf types directly, or single concrete pre-resolved types ( |
What I’m arguing is that it doesn’t make sense to adopt @leebyron suggestion forbidding weaker typing in order to allow for stronger typing if stronger typing is possible regardless with better variable definitions. I mean, I see why someone might disagree with the above , but just to clarify what I’m saying. |
Oh right; I see what you're saying. I agree with Lee though, let's guide people to the pit of success 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Please excuse me, if this is a noob question, but I didn't quite understand, why it would be such a bad idea to allow The examples given above seem a bit over-complicated to me. What I'd like to define, is something like this: input CatInput { name: String!, nickname: String, meowVolume: Int }
input DogInput { name: String!, nickname: String, barkVolume: Int }
type Mutation {
addPet(cat: CatInput, dog: DocInput): Pet @oneOf
}
#----
mutation AddPet ($cat: CatInput, $dog: DogInput) {
addPet(cat: $cat, dog: $dog) { name }
} On the client side, the |
I think the example you give indicates the problem: you've tried to apply The GraphQL grammar does not afford applying directives to multiple argument types (see TypeSystemDirectiveLocation, the closest you can get is |
I was very unsure about the allowed locations of directive, so I looked it up in the spec (draft version): a directive is allowed on a FIELD_DEFINITION, i.e. a simplified version of example No 91: directive @example on FIELD_DEFINITION
type SomeType {
field(arg: Int): String @example
} |
I don't think it would be "such a bad idea", but it does open a can of worms. For example, consider this (partial) schema: type Query {
userById(id: ID!): User
userByUsername(username: String!): User
userByRegistrationNumber(registrationNumber: String!): User
} In the current proposal you could collapse this to something like: input UserFinder @oneOf {
id: ID
username: String
registrationNumber: String
}
type Query {
user(by: UserFinder!): User
} What you're proposing is to condense this further: type Query {
user(
id: ID
username: String
registrationNumber: String
): User @oneOf
} At first blush, this looks like a great win, right? But one issue is that it severely inhibits schema evolution. Let's imagine that you now want the ability to see what a user looks like when viewed as another user; our original schema would look like: type Query {
userById(id: ID!, asUser: ID): User
userByUsername(username: String!, asUser: ID): User
userByRegistrationNumber(registrationNumber: String!, asUser: ID): User
} The current proposal allows a similar change: input UserFinder @oneOf {
id: ID
username: String
registrationNumber: String
}
type Query {
- user(by: UserFinder!): User
+ user(by: UserFinder!, asUser: ID): User
} But your proposed type Query {
user(
id: ID
username: String
registrationNumber: String
): User @oneOf
+ userAsUser(by: UserFinder!, asUser: ID): User
}
+input UserFinder @oneOf {
+ id: ID
+ username: String
+ registrationNumber: String
+} And the structure of the new field would likely be something like So by preventing We did discuss applying Further, one of the GraphQL guiding principles is:
So again, since it's already achievable with input objects there doesn't seem to be a pressing need to extend it to arguments. I hope this helps you understand why the decision was made to not apply |
@benjie: yes, that explains it very well. I'm not sure I fully agree, but now I understand and fully accept your reasoning. Thank you! |
First came
the @oneField directive.Then there was
the Tagged type.Introducing: OneOf Input Objects
and OneOf Fields.OneOf Input Objects are a special variant of Input Objects where the type system asserts that exactly one of the fields must be set and non-null, all others being omitted. This is represented in introspection with the
__Type.oneField: Boolean
field, and in SDL via the@oneOf
directive on the input object.OneOf Fields are a special variant of Object Type fields where the type system asserts that exactly one of the field's arguments must be set and non-null, all others being omitted. This is represented in introspection with the__Field.oneArgument: Boolean!
field, and in SDL via the@oneOf
directive on the field.(Why a directive? See the FAQ below.)
This variant introduces a form of input polymorphism to GraphQL. For example, the following
PetInput
input object lets you choose between a number of potential input types:Previously you may have had a situation where you had multiple ways to locate a user:
with OneOf Input Objects you can now express this via a single field without loss of type safety:
FAQ
Why is this a directive?
It's not. Well, not really - its an internal property of the type that's exposed through introspection - much in the same way that deprecation is. It just happens to be that after I analysed a number of potential syntaxes (including keywords and alternative syntax) I've found that the directive approach is the least invasive (all current GraphQL parsers can already parse it!) and none of the alternative syntaxes sufficiently justified the increased complexity they would introduce.
Why is this a good approach?
This approach, as a small change to existing types, is the easiest to adopt of any of the solutions we came up with to the InputUnion problem. It's also more powerful in that it allows additional types to be part of the "input union" - in fact any valid input type is allowed: input objects, scalars, enums, and lists of the same. Further it can be used on top of existing GraphQL tooling, so it can be adopted much sooner. Finally it's very explicit, so doesn't suffer the issues that "duck typed" input unions could face.
Why did you go full circle via the tagged type?
When the @oneField directive was proposed some members of the community felt that augmenting the behaviour of existing types might not be the best approach, so the Tagged type was born. (We also researched a lot of other approaches too.) However, the Tagged type brought with it a lot of complexity and controversy, and the Input Unions Working Group decided that we should revisit the simpler approach again. This time around I'm a lot better versed in writing spec edits 😁
Why are all the fields nullable? Shouldn't they be non-nullable?
To make this change minimally invasive I wanted:
To accomplish this, we add the "exactly one value, and that value is non-null" as a validation rule that runs after all the existing validation rules - it's an additive change.
Can this allow a field to accept both a scalar and an object?
Yes!
Can I use existing GraphQL clients to issue requests to OneOf-enabled schemas?
Yes - so long as you stick to the rules of one field / one argument manually - note that GraphQL already differentiates between a field not being supplied and a field being supplied with the value
null
.Without explicit client support you may lose a little type safety, but all major GraphQL clients can already speak this language. Given this nonsense schema:
the following are valid queries that you could issue from existing GraphQL clients:
{foo(by:{id: "..."})}
{foo(by:{str1: "..."})}
{foo(by:{str2: "..."})}
query Foo($by: FooBy!) {foo(by: $by)}
If my input object has only one field, should I use
@oneOf
?Doing so would preserve your option value - making a OneOf Input Object into a regular Input Object is a non-breaking change (the reverse is a breaking change). In the case of having one field on your type changing it from oneOf (and nullable) to regular and non-null is a non-breaking change (the reverse is also true in this degenerate case). The two
Example
types below are effectively equivalent - both require thatvalue
is supplied with a non-null int:Can we expand
@oneOf
to output types to allow for unions of objects, interfaces, scalars, enums and lists; potentially replacing the union type?🤫 👀 😉