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.
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 introspecting oneOf 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.
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.