Generate an enum for @oneOf
input
#450
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The RFC for
oneOf
on input type is currently in at the draft stage. In short, adding the@oneOf
directive to an input type means exactly one field of the input's field must be provided.While the spec still hasn't been accepted, there's a strong need for input unions, the spec is pretty far along and already seeing adoption in the community. It's available in the GraphQL Ruby gem, and we make use of it in Shopify Functions.
With this PR, inputs tagged with
@oneOf
will generate a Rust enum instead of a struct.From the test, this type:
generates this Rust enum:
That is, it generates an enum with 1 variant per field, where the variant has 1 required field.
Worth noting that we'll only generate an enum when using IDL-defined schema (
.graphql
file), but not when using the introspection JSON response. The reason for that is that introspectingoneOf
is done through the__Type.isOneOf
field, which isn't widely implemented. To make it work, we'd have to dynamically select__Type.isOneOf
in the introspection query, based on user opt-in. Since my use case didn't require it, and the introspection query is currently static, I didn't do it.