/rust/registry/src/index.crates.io-1949cf8c6b5b557f/async-graphql-7.0.17/src/model/schema.rs
Line | Count | Source |
1 | | use std::collections::HashSet; |
2 | | |
3 | | use crate::{ |
4 | | Object, |
5 | | model::{__Directive, __Type}, |
6 | | registry, |
7 | | }; |
8 | | |
9 | | pub struct __Schema<'a> { |
10 | | registry: &'a registry::Registry, |
11 | | visible_types: &'a HashSet<&'a str>, |
12 | | } |
13 | | |
14 | | impl<'a> __Schema<'a> { |
15 | 0 | pub fn new(registry: &'a registry::Registry, visible_types: &'a HashSet<&'a str>) -> Self { |
16 | 0 | Self { |
17 | 0 | registry, |
18 | 0 | visible_types, |
19 | 0 | } |
20 | 0 | } |
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")] |
27 | | impl<'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 | 0 | .filter_map(|ty| { |
43 | 0 | if self.visible_types.contains(ty.name()) { |
44 | 0 | Some(( |
45 | 0 | ty.name(), |
46 | 0 | __Type::new_simple(self.registry, self.visible_types, ty), |
47 | 0 | )) |
48 | | } else { |
49 | 0 | None |
50 | | } |
51 | 0 | }) |
52 | | .collect(); |
53 | 0 | 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 | 0 | self.registry.mutation_type.as_ref().and_then(|ty| { |
72 | 0 | if self.visible_types.contains(ty.as_str()) { |
73 | 0 | Some(__Type::new_simple( |
74 | 0 | self.registry, |
75 | 0 | self.visible_types, |
76 | 0 | &self.registry.types[ty], |
77 | 0 | )) |
78 | | } else { |
79 | 0 | None |
80 | | } |
81 | 0 | }) |
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 | 0 | self.registry.subscription_type.as_ref().and_then(|ty| { |
89 | 0 | if self.visible_types.contains(ty.as_str()) { |
90 | 0 | Some(__Type::new_simple( |
91 | 0 | self.registry, |
92 | 0 | self.visible_types, |
93 | 0 | &self.registry.types[ty], |
94 | 0 | )) |
95 | | } else { |
96 | 0 | None |
97 | | } |
98 | 0 | }) |
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 | 0 | registry: self.registry, |
109 | 0 | visible_types: self.visible_types, |
110 | 0 | directive, |
111 | 0 | }) |
112 | | .collect(); |
113 | 0 | directives.sort_by(|a, b| a.directive.name.cmp(&b.directive.name)); |
114 | | directives |
115 | | } |
116 | | } |