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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::context::Data;
use crate::model::__DirectiveLocation;
use crate::query::QueryBuilder;
use crate::registry::{Directive, InputValue, Registry};
use crate::types::QueryRoot;
use crate::{ObjectType, SubscribeBuilder, SubscriptionType, Type};
use std::any::Any;
use std::collections::HashMap;
pub struct Schema<Query, Mutation, Subscription> {
pub(crate) query: QueryRoot<Query>,
pub(crate) mutation: Mutation,
pub(crate) subscription: Subscription,
pub(crate) registry: Registry,
pub(crate) data: Data,
pub(crate) complexity: Option<usize>,
pub(crate) depth: Option<usize>,
}
impl<Query: ObjectType, Mutation: ObjectType, Subscription: SubscriptionType>
Schema<Query, Mutation, Subscription>
{
pub fn new(query: Query, mutation: Mutation, subscription: Subscription) -> Self {
let mut registry = Registry {
types: Default::default(),
directives: Default::default(),
implements: Default::default(),
query_type: Query::type_name().to_string(),
mutation_type: if Mutation::is_empty() {
None
} else {
Some(Mutation::type_name().to_string())
},
subscription_type: if Subscription::is_empty() {
None
} else {
Some(Subscription::type_name().to_string())
},
};
registry.add_directive(Directive {
name: "include",
description: Some("Directs the executor to include this field or fragment only when the `if` argument is true."),
locations: vec![
__DirectiveLocation::FIELD,
__DirectiveLocation::FRAGMENT_SPREAD,
__DirectiveLocation::INLINE_FRAGMENT
],
args: {
let mut args = HashMap::new();
args.insert("if", InputValue {
name: "if",
description: Some("Included when true."),
ty: "Boolean!".to_string(),
default_value: None,
validator: None,
});
args
}
});
registry.add_directive(Directive {
name: "skip",
description: Some("Directs the executor to skip this field or fragment when the `if` argument is true."),
locations: vec![
__DirectiveLocation::FIELD,
__DirectiveLocation::FRAGMENT_SPREAD,
__DirectiveLocation::INLINE_FRAGMENT
],
args: {
let mut args = HashMap::new();
args.insert("if", InputValue {
name: "if",
description: Some("Skipped when true."),
ty: "Boolean!".to_string(),
default_value: None,
validator: None,
});
args
}
});
bool::create_type_info(&mut registry);
i32::create_type_info(&mut registry);
f32::create_type_info(&mut registry);
String::create_type_info(&mut registry);
QueryRoot::<Query>::create_type_info(&mut registry);
if !Mutation::is_empty() {
Mutation::create_type_info(&mut registry);
}
if !Subscription::is_empty() {
Subscription::create_type_info(&mut registry);
}
Self {
query: QueryRoot {
inner: query,
disable_introspection: false,
},
mutation,
subscription,
registry,
data: Default::default(),
complexity: None,
depth: None,
}
}
pub fn disable_introspection(mut self) -> Self {
self.query.disable_introspection = true;
self
}
pub fn limit_complexity(mut self, complexity: usize) -> Self {
self.complexity = Some(complexity);
self
}
pub fn limit_depth(mut self, depth: usize) -> Self {
self.depth = Some(depth);
self
}
pub fn data<D: Any + Send + Sync>(mut self, data: D) -> Self {
self.data.insert(data);
self
}
pub fn query<'a>(&'a self, source: &'a str) -> QueryBuilder<'a, Query, Mutation, Subscription> {
QueryBuilder {
schema: self,
source,
operation_name: None,
variables: None,
data: &self.data,
}
}
pub fn subscribe<'a>(&'a self, source: &'a str) -> SubscribeBuilder<'a, Subscription> {
SubscribeBuilder {
subscription: &self.subscription,
registry: &self.registry,
source,
operation_name: None,
variables: None,
}
}
}