#[derive(GraphQLObject)]
{
// Attributes available to this derive:
#[graphql]
}
Expand description
#[derive(GraphQLObject)]
macro for deriving a GraphQL object
implementation for structs.
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::GraphQLObject;
#[derive(GraphQLObject)]
struct Query {
// NOTICE: By default, field names will be converted to `camelCase`.
// In the generated GraphQL schema this field will be available
// as `apiVersion`.
api_version: &'static str,
}
§Custom name, description and deprecation
The name of GraphQL object or its field may be overridden with a name
attribute’s argument. By default, a type name is used or camelCased
field
name.
The description of GraphQL object or its field may be specified either
with a description
/desc
attribute’s argument, or with a regular Rust doc
comment.
A field of GraphQL object may be deprecated by specifying a
deprecated
attribute’s argument, or with regular Rust #[deprecated]
attribute.
#[derive(GraphQLObject)]
#[graphql(
// Rename the type for GraphQL by specifying the name here.
name = "Human",
// You may also specify a description here.
// If present, doc comments will be ignored.
desc = "Possible episode human.",
)]
struct HumanWithAttrs {
#[graphql(name = "id", desc = "ID of the human.")]
#[graphql(deprecated = "Don't use it")]
some_id: String,
}
// Rust docs are used as GraphQL description.
/// Possible episode human.
#[derive(GraphQLObject)]
struct HumanWithDocs {
// Doc comments also work on fields.
/// ID of the human.
#[deprecated]
id: String,
}
§Renaming policy
By default, all GraphQL object fields are renamed via camelCase
policy (so api_version: String
becomes apiVersion
field in GraphQL
schema, and so on). This complies with default GraphQL naming conventions
[demonstrated in spec][0].
However, if you need for some reason apply another naming convention, it’s
possible to do by using rename_all
attribute’s argument. At the moment it
supports the following policies only: SCREAMING_SNAKE_CASE
, camelCase
,
none
(disables any renaming).
#[derive(GraphQLObject)]
#[graphql(rename_all = "none")] // disables renaming
struct Query {
// NOTICE: In the generated GraphQL schema this field will be available
// as `api_version`.
api_version: String,
}
§Ignoring struct fields
To omit exposing a struct field in the GraphQL schema, use an ignore
attribute’s argument directly on that field.
#[derive(GraphQLObject)]
struct Human {
id: String,
#[graphql(ignore)]
home_planet: String,
#[graphql(skip)]
// ^^^^ alternative naming, up to your preference
password_hash: String,
}
§Custom ScalarValue
By default, #[derive(GraphQLObject)]
macro generates code, which is
generic over a ScalarValue
type. This may introduce a problem when at
least one of its fields is restricted to a concrete ScalarValue
type in
its implementation. To resolve such problem, a concrete ScalarValue
type
should be specified with a scalar
attribute’s argument.
#[derive(GraphQLObject)]
// NOTICE: Removing `scalar` argument will fail compilation.
#[graphql(scalar = DefaultScalarValue)]
struct Human {
id: String,
helper: Droid,
}
#[derive(GraphQLObject)]
#[graphql(scalar = DefaultScalarValue)]
struct Droid {
id: String,
}