@@ -66,6 +66,7 @@ pub enum TypeType<'a, S: 'a> {
66
66
}
67
67
68
68
#[ derive( Debug ) ]
69
+ /// Used to describe GraphQL directives
69
70
pub struct DirectiveType < ' a , S > {
70
71
pub name : String ,
71
72
pub description : Option < String > ,
@@ -108,6 +109,7 @@ where
108
109
pub fn new ( query : QueryT , mutation : MutationT , subscription : SubscriptionT ) -> Self {
109
110
Self :: new_with_info ( query, mutation, subscription, ( ) , ( ) , ( ) )
110
111
}
112
+
111
113
}
112
114
113
115
impl < ' a , QueryT , MutationT , SubscriptionT , S > RootNode < ' a , QueryT , MutationT , SubscriptionT , S >
@@ -126,6 +128,44 @@ where
126
128
) -> Self {
127
129
RootNode :: new_with_info ( query, mutation, subscription, ( ) , ( ) , ( ) )
128
130
}
131
+
132
+ /// ```rust
133
+ /// use juniper::{
134
+ /// graphql_object, graphql_vars, EmptyMutation, EmptySubscription, GraphQLError,
135
+ /// RootNode, DirectiveLocation , DirectiveType
136
+ /// };
137
+ ///
138
+ /// struct Query{}
139
+ ///
140
+ /// #[graphql_object]
141
+ /// impl Query {
142
+ /// pub fn hello() -> String {
143
+ /// "Hello".to_string()
144
+ /// }
145
+ /// }
146
+ ///
147
+ /// type Schema = RootNode<'static, Query, EmptyMutation, EmptySubscription>;
148
+ ///
149
+ /// let schema = Schema::new_with_directives(Query {}, EmptyMutation::new(), EmptySubscription::new()
150
+ /// ,vec![ DirectiveType::new("my_directive", &[DirectiveLocation::Query] , &[] , false )]);
151
+ ///
152
+ /// let query = "query @my_directive { hello }";
153
+ ///
154
+ /// match juniper::execute_sync(query, None, &schema, &graphql_vars! {}, &()) {
155
+ /// Err(GraphQLError::ValidationError(errs)) => { panic!("should not give an error"); }
156
+ /// res => {}
157
+ /// }
158
+ ///
159
+ /// let query = "query @non_existing_directive { hello }";
160
+ ///
161
+ /// match juniper::execute_sync(query, None, &schema, &graphql_vars! {}, &()) {
162
+ /// Err(GraphQLError::ValidationError(errs)) => { }
163
+ /// res => { panic!("should give an error"); }
164
+ /// }
165
+ /// ```
166
+ pub fn new_with_directives ( query : QueryT , mutation : MutationT , subscription : SubscriptionT , directives : Vec < DirectiveType < ' a , S > > ) -> Self {
167
+ Self :: new_with_info_directives ( query, mutation, subscription, ( ) , ( ) , ( ) , directives)
168
+ }
129
169
}
130
170
131
171
impl < ' a , S , QueryT , MutationT , SubscriptionT > RootNode < ' a , QueryT , MutationT , SubscriptionT , S >
@@ -135,6 +175,7 @@ where
135
175
SubscriptionT : GraphQLType < S > ,
136
176
S : ScalarValue + ' a ,
137
177
{
178
+
138
179
/// Construct a new root node from query and mutation nodes,
139
180
/// while also providing type info objects for the query and
140
181
/// mutation types.
@@ -162,6 +203,41 @@ where
162
203
}
163
204
}
164
205
206
+ /// Construct a new root node from query and mutation nodes,
207
+ /// while also providing type info objects for the query and
208
+ /// mutation types and custom directives
209
+ pub fn new_with_info_directives (
210
+ query_obj : QueryT ,
211
+ mutation_obj : MutationT ,
212
+ subscription_obj : SubscriptionT ,
213
+ query_info : QueryT :: TypeInfo ,
214
+ mutation_info : MutationT :: TypeInfo ,
215
+ subscription_info : SubscriptionT :: TypeInfo ,
216
+ directives : Vec < DirectiveType < ' a , S > >
217
+ ) -> Self {
218
+
219
+ let mut schema_type = SchemaType :: new :: < QueryT , MutationT , SubscriptionT > (
220
+ & query_info,
221
+ & mutation_info,
222
+ & subscription_info,
223
+ ) ;
224
+
225
+ for directive in directives {
226
+ schema_type. add_directive ( directive) ;
227
+ }
228
+
229
+ Self {
230
+ query_type : query_obj,
231
+ mutation_type : mutation_obj,
232
+ subscription_type : subscription_obj,
233
+ schema : schema_type,
234
+ query_info,
235
+ mutation_info,
236
+ subscription_info,
237
+ introspection_disabled : false ,
238
+ }
239
+ }
240
+
165
241
/// Disables introspection for this [`RootNode`], making it to return a [`FieldError`] whenever
166
242
/// its `__schema` or `__type` field is resolved.
167
243
///
@@ -585,6 +661,8 @@ impl<'a, S> DirectiveType<'a, S>
585
661
where
586
662
S : ScalarValue + ' a ,
587
663
{
664
+ /// Create a new graphql directive
665
+
588
666
pub fn new (
589
667
name : & str ,
590
668
locations : & [ DirectiveLocation ] ,
0 commit comments