async_graphql/model/
schema.rs

1use std::collections::HashSet;
2
3use crate::{
4    Object,
5    model::{__Directive, __Type},
6    registry,
7};
8
9pub struct __Schema<'a> {
10    registry: &'a registry::Registry,
11    visible_types: &'a HashSet<&'a str>,
12}
13
14impl<'a> __Schema<'a> {
15    pub fn new(registry: &'a registry::Registry, visible_types: &'a HashSet<&'a str>) -> Self {
16        Self {
17            registry,
18            visible_types,
19        }
20    }
21}
22
23/// A GraphQL Schema defines the capabilities of a GraphQL server. It exposes
24/// all available types and directives on the server, as well as the entry
25/// points for query, mutation, and subscription operations.
26#[Object(internal, name = "__Schema")]
27impl<'a> __Schema<'a> {
28    /// description of __Schema for newer graphiql introspection schema
29    /// requirements
30    async fn description(&self) -> String {
31        String::from(
32            "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.",
33        )
34    }
35
36    /// A list of all types supported by this server.
37    async fn types(&self) -> Vec<__Type<'a>> {
38        let mut types: Vec<_> = self
39            .registry
40            .types
41            .values()
42            .filter_map(|ty| {
43                if self.visible_types.contains(ty.name()) {
44                    Some((
45                        ty.name(),
46                        __Type::new_simple(self.registry, self.visible_types, ty),
47                    ))
48                } else {
49                    None
50                }
51            })
52            .collect();
53        types.sort_by(|a, b| a.0.cmp(b.0));
54        types.into_iter().map(|(_, ty)| ty).collect()
55    }
56
57    /// The type that query operations will be rooted at.
58    #[inline]
59    async fn query_type(&self) -> __Type<'a> {
60        __Type::new_simple(
61            self.registry,
62            self.visible_types,
63            &self.registry.types[&self.registry.query_type],
64        )
65    }
66
67    /// If this server supports mutation, the type that mutation operations will
68    /// be rooted at.
69    #[inline]
70    async fn mutation_type(&self) -> Option<__Type<'a>> {
71        self.registry.mutation_type.as_ref().and_then(|ty| {
72            if self.visible_types.contains(ty.as_str()) {
73                Some(__Type::new_simple(
74                    self.registry,
75                    self.visible_types,
76                    &self.registry.types[ty],
77                ))
78            } else {
79                None
80            }
81        })
82    }
83
84    /// If this server support subscription, the type that subscription
85    /// operations will be rooted at.
86    #[inline]
87    async fn subscription_type(&self) -> Option<__Type<'a>> {
88        self.registry.subscription_type.as_ref().and_then(|ty| {
89            if self.visible_types.contains(ty.as_str()) {
90                Some(__Type::new_simple(
91                    self.registry,
92                    self.visible_types,
93                    &self.registry.types[ty],
94                ))
95            } else {
96                None
97            }
98        })
99    }
100
101    /// A list of all directives supported by this server.
102    async fn directives(&self) -> Vec<__Directive<'a>> {
103        let mut directives: Vec<_> = self
104            .registry
105            .directives
106            .values()
107            .map(|directive| __Directive {
108                registry: self.registry,
109                visible_types: self.visible_types,
110                directive,
111            })
112            .collect();
113        directives.sort_by(|a, b| a.directive.name.cmp(&b.directive.name));
114        directives
115    }
116}