Enums

Enums in GraphQL are string constants grouped together to represent a set of possible values. Simple Rust enums can be converted to GraphQL enums by using a custom derive attribute:

#[macro_use] extern crate juniper_codegen;
extern crate juniper;

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

Juniper converts all enum variants to uppercase, so the corresponding string values for these variants are NEWHOPE, EMPIRE, and JEDI, respectively. If you want to override this, you can use the graphql attribute, similar to how it works when defining objects:


#[derive(GraphQLEnum)]
enum Episode {
    #[graphql(name="NEW_HOPE")]
    NewHope,
    Empire,
    Jedi,
}

Documentation and deprecation

Just like when defining objects, the type itself can be renamed and documented, while individual enum variants can be renamed, documented, and deprecated:


#[derive(GraphQLEnum)]
#[graphql(name="Episode", description="An episode of Star Wars")]
enum StarWarsEpisode {
    #[graphql(deprecated="We don't really talk about this one")]
    ThePhantomMenace,

    #[graphql(name="NEW_HOPE")]
    NewHope,

    #[graphql(description="Arguably the best one in the trilogy")]
    Empire,
    Jedi,
}

results matching ""

    No results matching ""