Input objects
Fields may accept arguments to configure their behavior. These inputs are often scalars or enums, but they sometimes need to represent more complex values.
A GraphQL input object defines a set of input fields; the input fields are either scalars, enums, or other input objects. This allows arguments to accept arbitrarily complex structs.
In Juniper, defining a GraphQL input object is quite straightforward and similar to how trivial GraphQL objects are defined - by using the #[derive(GraphQLInputObject)]
attribute on a Rust struct:
Renaming
Just as with defining GraphQL objects, by default struct fields are converted from Rust's standard snake_case
naming convention into GraphQL's camelCase
convention:
We can override the name by using the #[graphql(name = "...")]
attribute:
Or provide a different renaming policy for all the struct fields:
TIP: Supported policies are:
SCREAMING_SNAKE_CASE
,camelCase
andnone
(disables any renaming).
Documentation
Similarly, GraphQL descriptions may be provided by either using Rust doc comments or with the #[graphql(description = "...")]
attribute:
NOTE: As of October 2021 GraphQL specification, GraphQL input object's fields cannot be deprecated.
Ignoring
By default, all struct fields are included into the generated GraphQL input object type. To prevent inclusion of a specific field annotate it with the #[graphql(ignore)]
attribute:
WARNING: Ignored fields must either implement
Default
or be annotated with the#[graphql(default = <expression>)]
argument.
TIP: See more available features in the API docs of the
#[derive(GraphQLInputObject)]
attribute.