#[derive(GraphQLEnum)]
{
    // Attributes available to this derive:
    #[graphql]
}
Expand description

#[derive(GraphQLEnum)] macro for deriving a GraphQL enum implementation for Rust enums.

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::GraphQLEnum;

#[derive(GraphQLEnum)]
enum Episode {
    NewHope,
    Empire,
    Jedi,
}

Custom name, description and deprecation

The name of a GraphQL enum or its values may be overridden with the name attribute’s argument. By default, a type name is used or a variant name in SCREAMING_SNAKE_CASE.

The description of a GraphQL enum or its values may be specified either with the description/desc attribute’s argument, or with a regular Rust doc comment.

GraphQL enum value may be deprecated by specifying the deprecated attribute’s argument, or with regular a Rust #[deprecated] attribute.

#[derive(GraphQLEnum)]
#[graphql(
    // Rename the type for GraphQL by specifying the name here.
    name = "AvailableEpisodes",
    // You may also specify a description here.
    // If present, doc comments will be ignored.
    desc = "Possible episodes.",
)]
enum Episode {
    /// Doc comment, also acting as description.
    #[deprecated(note = "Don't use it")]
    NewHope,

    #[graphql(name = "Jedi", desc = "Arguably the best one in the trilogy")]
    #[graphql(deprecated = "Don't use it")]
    Jedai,

    Empire,
}

Renaming policy

By default, all GraphQL enum values are renamed in a SCREAMING_SNAKE_CASE manner (so a NewHope Rust enum variant becomes a NEW_HOPE 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(GraphQLEnum)]
#[graphql(rename_all = "none")] // disables renaming
enum Episode {
    NewHope,
    Empire,
    Jedi,
}

Ignoring enum variants

To omit exposing a Rust enum variant in a GraphQL schema, use the ignore attribute’s argument directly on that variant. Only ignored Rust enum variants are allowed to contain fields.

#[derive(GraphQLEnum)]
enum Episode<T> {
    NewHope,
    Empire,
    Jedi,
    #[graphql(ignore)]
    Legends(T),
}

Custom ScalarValue

By default, #[derive(GraphQLEnum)] macro generates code, which is generic over a ScalarValue type. This can be changed with the scalar attribute’s argument.

#[derive(GraphQLEnum)]
#[graphql(scalar = DefaultScalarValue)]
enum Episode {
    NewHope,
    Empire,
    Jedi,
}