#[derive(GraphQLInputObject)]
{
// Attributes available to this derive:
#[graphql]
}
Expand description
#[derive(GraphQLInputObject)]
macro for deriving a
GraphQL input object implementation for a Rust struct. Each
non-ignored field type must itself be GraphQL input object or a
GraphQL scalar.
The #[graphql]
helper attribute is used for configuring the derived
implementation. Specifying multiple #[graphql]
attributes on the same
definition is totally okay. They all will be treated as a single attribute.
use juniper::GraphQLInputObject;
#[derive(GraphQLInputObject)]
struct Point2D {
x: f64,
y: f64,
}
§Custom name and description
The name of a GraphQL input object or its fields may be overridden
with the name
attribute’s argument. By default, a type name or a struct
field name is used in a camelCase
.
The description of a GraphQL input object or its fields may be
specified either with the description
/desc
attribute’s argument, or with
a regular Rust doc comment.
#[derive(GraphQLInputObject)]
#[graphql(
// Rename the type for GraphQL by specifying the name here.
name = "Point",
// You may also specify a description here.
// If present, doc comments will be ignored.
desc = "A point is the simplest two-dimensional primitive.",
)]
struct Point2D {
/// Abscissa value.
x: f64,
#[graphql(name = "y", desc = "Ordinate value")]
y_coord: f64,
}
§Renaming policy
By default, all GraphQL input object fields are renamed in a
camelCase
manner (so a y_coord
Rust struct field becomes a
yCoord
value in GraphQL schema, and so on). This complies with
default GraphQL naming conventions as demonstrated in spec.
However, if you need for some reason another naming convention, it’s
possible to do so by using the rename_all
attribute’s argument. At the
moment, it supports the following policies only: SCREAMING_SNAKE_CASE
,
camelCase
, none
(disables any renaming).
#[derive(GraphQLInputObject)]
#[graphql(rename_all = "none")] // disables renaming
struct Point2D {
x: f64,
y_coord: f64, // will be `y_coord` instead of `yCoord` in GraphQL schema
}
§Ignoring fields
To omit exposing a Rust field in a GraphQL schema, use the ignore
attribute’s argument directly on that field. Ignored fields must implement
[Default
] or have the default = <expression>
attribute’s argument.
enum System {
Cartesian,
}
#[derive(GraphQLInputObject)]
struct Point2D {
x: f64,
y: f64,
#[graphql(ignore, default = System::Cartesian)]
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
// This attribute is required, as we need to be able to construct
// a `Point2D` value from the `{ x: 0.0, y: 0.0 }` GraphQL input value,
// received from client-side.
system: System,
// `Default::default()` value is used, if no
// `#[graphql(default = <expression>)]` is specified.
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
shift: f64,
}