1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::model::{__Directive, __Type};
use crate::registry;
use async_graphql_derive::Object;

pub struct __Schema<'a> {
    pub registry: &'a registry::Registry,
}

#[Object(
    internal,
    desc = "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations."
)]
impl<'a> __Schema<'a> {
    #[field(desc = "A list of all types supported by this server.")]
    async fn types(&self) -> Vec<__Type<'a>> {
        self.registry
            .types
            .values()
            .map(|ty| __Type::new_simple(self.registry, ty))
            .collect()
    }

    #[field(desc = "The type that query operations will be rooted at.")]
    async fn query_type(&self) -> __Type<'a> {
        __Type::new_simple(
            self.registry,
            &self.registry.types[&self.registry.query_type],
        )
    }

    #[field(
        desc = "If this server supports mutation, the type that mutation operations will be rooted at."
    )]
    async fn mutation_type(&self) -> Option<__Type<'a>> {
        if let Some(ty) = &self.registry.mutation_type {
            Some(__Type::new_simple(self.registry, &self.registry.types[ty]))
        } else {
            None
        }
    }

    #[field(
        desc = "If this server support subscription, the type that subscription operations will be rooted at."
    )]
    async fn subscription_type(&self) -> Option<__Type<'a>> {
        if let Some(ty) = &self.registry.subscription_type {
            Some(__Type::new_simple(self.registry, &self.registry.types[ty]))
        } else {
            None
        }
    }

    #[field(desc = "A list of all directives supported by this server.")]
    async fn directives(&self) -> Vec<__Directive<'a>> {
        self.registry
            .directives
            .values()
            .map(|directive| __Directive {
                registry: &self.registry,
                directive,
            })
            .collect()
    }
}