async_graphql

Derive Macro OneofObject

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

Define a GraphQL oneof input object

§Macro attributes

AttributedescriptionTypeOptional
nameOneof input object namestringY
name_typeIf true, the object name will be specified from async_graphql::TypeName traitboolY
rename_fieldsRename all the fields according to the given case convention. The possible values are “lowercase”, “UPPERCASE”, “PascalCase”, “camelCase”, “snake_case”, “SCREAMING_SNAKE_CASE”.stringY
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
concretesSpecify how the concrete type of the generic SimpleObject should be implemented.ConcreteTypeY

§Field attributes

AttributedescriptionTypeOptional
nameField namestringY
validatorInput value validator See also the BookobjectY
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
secretMark this field as a secret, it will not output the actual value in the log.boolY

§Examples

use async_graphql::*;

#[derive(OneofObject)]
enum MyInputObject {
    A(i32),
    B(String),
}

struct Query;

#[Object]
impl Query {
    async fn value(&self, input: MyInputObject) -> String {
        match input {
            MyInputObject::A(value) => format!("a:{}", value),
            MyInputObject::B(value) => format!("b:{}", value),
        }
    }
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let res = schema.execute(r#"
{
    value1: value(input:{a:100})
    value2: value(input:{b:"abc"})
}"#).await.into_result().unwrap().data;
assert_eq!(res, value!({ "value1": "a:100", "value2": "b:abc" }));