async_graphql

Attribute Macro ComplexObject

source
#[ComplexObject]
Expand description

Define a complex GraphQL object for SimpleObject’s complex field resolver.

See also the Book.

Sometimes most of the fields of a GraphQL object simply return the value of the structure member, but a few fields are calculated. Usually we use the Object macro to define such a GraphQL object.

But this can be done more beautifully with the ComplexObject macro. We can use the SimpleObject macro to define some simple fields, and use the ComplexObject macro to define some other fields that need to be calculated.

§Macro attributes

AttributedescriptionTypeOptional
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
rename_argsRename all the arguments according to the given case convention. The possible values are “lowercase”, “UPPERCASE”, “PascalCase”, “camelCase”, “snake_case”, “SCREAMING_SNAKE_CASE”.stringY
guardField of guard See also the BookstringY
inaccessibleIndicate that an object 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

§Field attributes

AttributedescriptionTypeOptional
skipSkip this fieldboolY
nameField namestringY
descField descriptionstringY
deprecationField deprecatedboolY
deprecationField deprecation reasonstringY
cache_controlField cache controlCacheControlY
externalMark a field as owned by another service. This allows service A to use fields from service B while also knowing at runtime the types of that field.boolY
providesAnnotate the expected returned fieldset from a field on a base type that is guaranteed to be selectable by the gateway.stringY
requiresAnnotate the required input fieldset from a base type for a resolver. It is used to develop a query plan where the required fields may not be needed by the client, but the service may need additional information from other services.stringY
shareableIndicate that a field is allowed to be resolved by multiple subgraphsboolY
inaccessibleIndicate that a field 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
override_fromMark the field as overriding a field currently present on another subgraph. It is used to migrate fields between subgraphs.stringY
guardField of guard See also the BookstringY
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
complexityCustom field complexity. See also the Book.boolY
complexityCustom field complexity.stringY
derivedGenerate derived fields See also the Book.objectY
flattenSimilar to serde (flatten)booleanY

§Field argument attributes

AttributedescriptionTypeOptional
nameArgument namestringY
descArgument descriptionstringY
defaultUse Default::default for default valuenoneY
defaultArgument default valueliteralY
default_withExpression to generate default valuecode stringY
validatorInput value validator See also the BookobjectY
inaccessibleIndicate that a field argument 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
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
process_withUpon successful parsing, invokes specified function. Its signature must be fn(&mut T).code pathY

§Examples

use async_graphql::*;

#[derive(SimpleObject)]
#[graphql(complex)] // NOTE: If you want the `ComplexObject` macro to take effect, this `complex` attribute is required.
struct MyObj {
    a: i32,
    b: i32,
}

#[ComplexObject]
impl MyObj {
    async fn c(&self) -> i32 {
        self.a + self.b
    }
}

struct Query;

#[Object]
impl Query {
    async fn obj(&self) -> MyObj {
        MyObj { a: 10, b: 20 }
    }
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let res = schema.execute("{ obj { a b c } }").await.into_result().unwrap().data;
assert_eq!(res, value!({
    "obj": {
        "a": 10,
        "b": 20,
        "c": 30,
    },
}));