-
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?
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 |
@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)
what's the current status of https://spec.graphql.org/draft/ doesn't yet include (I'm hoping to get |
In GraphQL js 15/16 you can use it unflagged and same goes for GraphQL ruby so I'd say it's stable for implementations. It's also in stage 2 which seems a bit more solidified in terms of how it will work |
It's available unflagged in GraphQL.NET v8 |
Also available in GraphQL Java since v21.2 |
I will do a final review and merge |
I've merged in the latest |
Hello @leebyron have any idea when would this pr be merged |
@Nomia it's on the agenda of the June working group. Feel free to add yourself if you want to attend! |
@Nomia It's RFC3 (Accepted) so you should feel comfortable using it even before it is merged, further changes will likely be very minor in nature: rephrasing sentences for clarity and the like. |
mutation addNullableCatWithDefault($cat: CatInput = { name: "Brontie" }) { | ||
addPet(pet: { cat: $cat }) { | ||
name | ||
} | ||
} |
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 there a test like this in GraphQL.js? I'm trying to understand why it's not passing in our implementation.
IsNonNullPosition
returnstrue
(becausecat
is a field of the OneOf input objectPetInput
).variableType
is not non-null (i.e. it's nullable [$cat: CatInput
]).hasNonNullVariableDefaultValue
istrue
, so that condition won't run and returnfalse
.AreTypesCompatible(variableType, nullableLocationType)
returnstrue
since both types are nullableCatInput
.
... so IsVariableUsageAllowed
returns true, and there are no errors. Am I missing something here? 🙂
(FWIW, listOfOneOfWithNullableVariable
is also failing, but that could be for other reasons.)
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 think that historically a nullable type was allowed with a default value for a non-null position, so this sample should pass validation (assuming that no cat
variable was provided, or that it is non-null). As far as I know, this hasn't been changed -- to support backwards compatibility. FYI, GraphQL.NET also allows this sample to pass the validation phase, but only when the cat
variable is missing or non-null.
Offhand I don't see changes in this PR that would alter this legacy behavior, but I didn't do a thorough review.
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.
Good catch; this counter-example will probably NOT fail validation until we adopt:
I should remove it or opt oneof into the stricter (non-legacy) behavior.
@@ -41,6 +41,11 @@ type Query { | |||
findDog(searchBy: FindDogInput): Dog | |||
} | |||
|
|||
type Mutation { | |||
addPet(pet: PetInput!): Pet | |||
addPets(pet: [PetInput!]!): [Pet] |
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.
addPets(pet: [PetInput!]!): [Pet] | |
addPets(pets: [PetInput!]!): [Pet] |
Follow up of
the @oneField directiveandthe Tagged type.Introducing: OneOf Input Objects.
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.isOneOf: Boolean
field, and in SDL via the@oneOf
directive on the input object.This variant of an input object introduces a form of input polymorphism to GraphQL.
Example 1 -
addPet
The following
PetInput
oneof input object lets you choose between a number of potential input types:Example 2 -
user(by:)
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?
At its core, it's a property of the type that's exposed through introspection - much in the same way that deprecation is. There's nothing in introspection, nor in the types exposed through the reference implementation (
new GraphQLInputObjectType({ name: "...", isOneOf: true, ... })
) that relates to directives. It just happens to be that after I analysed a number of potential syntaxes (including keywords and alternative syntax) I've found that when representing the schema as SDL, using a directive to do so 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?🤫 👀 😉