async_graphql

Derive Macro Enum

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

Define a GraphQL enum

See also the Book.

§Macro attributes

AttributedescriptionTypeOptional
nameEnum namestringY
name_typeIf true, the enum name will be specified from async_graphql::TypeName traitboolY
displayImplements std::fmt::Display for the enum typeboolY
rename_itemsRename all the fields according to the given case convention. The possible values are “lowercase”, “UPPERCASE”, “PascalCase”, “camelCase”, “snake_case”, “SCREAMING_SNAKE_CASE”.stringY
remoteDerive a remote enumstringY
visibleIf false, it will not be displayed in introspection. See also the Book.boolY
visibleCall the specified function. If the return value is false, it will not be displayed in introspection.stringY
inaccessibleIndicate that an enum is not accessible from a supergraph when using Apollo FederationboolY
tagArbitrary string metadata that will be propagated to the supergraph when using Apollo Federation. This attribute is repeatablestringY

§Item attributes

AttributedescriptionTypeOptional
nameItem namestringY
deprecationItem deprecatedboolY
deprecationItem deprecation reasonstringY
visibleIf false, it will not be displayed in introspection. See also the Book.boolY
visibleCall the specified function. If the return value is false, it will not be displayed in introspection.stringY
inaccessibleIndicate that an item is not accessible from a supergraph when using Apollo FederationboolY
tagArbitrary string metadata that will be propagated to the supergraph when using Apollo Federation. This attribute is repeatablestringY

§Examples

use async_graphql::*;

#[derive(Enum, Copy, Clone, Eq, PartialEq)]
enum MyEnum {
    A,
    #[graphql(name = "b")] B,
}

struct Query {
    value1: MyEnum,
    value2: MyEnum,
}

#[Object]
impl Query {
    /// value1
    async fn value1(&self) -> MyEnum {
        self.value1
    }

    /// value2
    async fn value2(&self) -> MyEnum {
        self.value2
    }
}

let schema = Schema::new(Query{ value1: MyEnum::A, value2: MyEnum::B }, EmptyMutation, EmptySubscription);
let res = schema.execute("{ value1 value2 }").await.into_result().unwrap().data;
assert_eq!(res, value!({ "value1": "A", "value2": "b" }));