Skip to content

Commit ed11cc0

Browse files
Signed-off-by: Marco Fuijkschot <marcof@titaniumit.nl>
modified: juniper/src/lib.rs modified: juniper/src/schema/model.rs (feat) create schema with custom directives
1 parent 32c0588 commit ed11cc0

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

juniper/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ pub use crate::{
8080
macros::helper::subscription::{ExtractTypeFromStream, IntoFieldResult},
8181
parser::{ParseError, ScalarToken, Span, Spanning},
8282
schema::{
83+
meta::Argument,
8384
meta,
84-
model::{RootNode, SchemaType},
85+
model::{RootNode, SchemaType , DirectiveType,DirectiveLocation },
8586
},
8687
types::{
8788
async_await::{GraphQLTypeAsync, GraphQLValueAsync},

juniper/src/schema/model.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub enum TypeType<'a, S: 'a> {
6666
}
6767

6868
#[derive(Debug)]
69+
/// Used to describe GraphQL directives
6970
pub struct DirectiveType<'a, S> {
7071
pub name: String,
7172
pub description: Option<String>,
@@ -108,6 +109,7 @@ where
108109
pub fn new(query: QueryT, mutation: MutationT, subscription: SubscriptionT) -> Self {
109110
Self::new_with_info(query, mutation, subscription, (), (), ())
110111
}
112+
111113
}
112114

113115
impl<'a, QueryT, MutationT, SubscriptionT, S> RootNode<'a, QueryT, MutationT, SubscriptionT, S>
@@ -126,6 +128,44 @@ where
126128
) -> Self {
127129
RootNode::new_with_info(query, mutation, subscription, (), (), ())
128130
}
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+
}
129169
}
130170

131171
impl<'a, S, QueryT, MutationT, SubscriptionT> RootNode<'a, QueryT, MutationT, SubscriptionT, S>
@@ -135,6 +175,7 @@ where
135175
SubscriptionT: GraphQLType<S>,
136176
S: ScalarValue + 'a,
137177
{
178+
138179
/// Construct a new root node from query and mutation nodes,
139180
/// while also providing type info objects for the query and
140181
/// mutation types.
@@ -162,6 +203,41 @@ where
162203
}
163204
}
164205

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+
165241
/// Disables introspection for this [`RootNode`], making it to return a [`FieldError`] whenever
166242
/// its `__schema` or `__type` field is resolved.
167243
///
@@ -585,6 +661,8 @@ impl<'a, S> DirectiveType<'a, S>
585661
where
586662
S: ScalarValue + 'a,
587663
{
664+
/// Create a new graphql directive
665+
588666
pub fn new(
589667
name: &str,
590668
locations: &[DirectiveLocation],

0 commit comments

Comments
 (0)