Skip to content

Commit 29a0350

Browse files
committed
Refactor, vol.2
1 parent 28370b6 commit 29a0350

File tree

13 files changed

+104
-69
lines changed

13 files changed

+104
-69
lines changed

juniper/src/executor/mod.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ use crate::{
2222
},
2323
model::{RootNode, SchemaType, TypeType},
2424
},
25-
types::{base::{GraphQLType, GraphQLValue}, name::Name, async_await::GraphQLValueAsync},
25+
types::{
26+
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
27+
base::{GraphQLType, GraphQLValue},
28+
name::Name,
29+
},
2630
value::{DefaultScalarValue, ParseScalarValue, ScalarValue, Value},
2731
GraphQLError,
2832
};
@@ -754,9 +758,9 @@ pub fn execute_validated_query<'a, 'b, QueryT, MutationT, SubscriptionT, CtxT, S
754758
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
755759
where
756760
S: ScalarValue,
757-
QueryT: GraphQLValue<S, Context = CtxT>,
758-
MutationT: GraphQLValue<S, Context = CtxT>,
759-
SubscriptionT: GraphQLValue<S, Context = CtxT>,
761+
QueryT: GraphQLType<S, Context = CtxT>,
762+
MutationT: GraphQLType<S, Context = CtxT>,
763+
SubscriptionT: GraphQLType<S, Context = CtxT>,
760764
{
761765
if operation.item.operation_type == OperationType::Subscription {
762766
return Err(GraphQLError::IsSubscription);
@@ -848,11 +852,11 @@ pub async fn execute_validated_query_async<'a, 'b, QueryT, MutationT, Subscripti
848852
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
849853
where
850854
S: ScalarValue + Send + Sync,
851-
QueryT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
855+
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
852856
QueryT::TypeInfo: Send + Sync,
853-
MutationT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
857+
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
854858
MutationT::TypeInfo: Send + Sync,
855-
SubscriptionT: GraphQLValue<S, Context = CtxT> + Send + Sync,
859+
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
856860
SubscriptionT::TypeInfo: Send + Sync,
857861
CtxT: Send + Sync,
858862
{
@@ -995,9 +999,9 @@ where
995999
'd: 'r,
9961000
'op: 'd,
9971001
S: ScalarValue + Send + Sync,
998-
QueryT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
1002+
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
9991003
QueryT::TypeInfo: Send + Sync,
1000-
MutationT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
1004+
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
10011005
MutationT::TypeInfo: Send + Sync,
10021006
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
10031007
SubscriptionT::TypeInfo: Send + Sync,

juniper/src/schema/schema.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::{
22
ast::Selection,
33
executor::{ExecutionResult, Executor, Registry},
4-
types::{base::{Arguments, GraphQLType, GraphQLValue, TypeKind}, async_await::{GraphQLValueAsync, GraphQLTypeAsync}},
4+
types::{
5+
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
6+
base::{Arguments, GraphQLType, GraphQLValue, TypeKind},
7+
},
58
value::{ScalarValue, Value},
69
};
710

juniper/src/schema/translate/graphql_parser.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
use std::boxed::Box;
2-
use std::collections::BTreeMap;
1+
use std::{boxed::Box, collections::BTreeMap};
32

4-
use graphql_parser::query::{
5-
Directive as ExternalDirective, Number as ExternalNumber, Type as ExternalType,
3+
use graphql_parser::{
4+
query::{Directive as ExternalDirective, Number as ExternalNumber, Type as ExternalType},
5+
schema::{
6+
Definition, Document, EnumType as ExternalEnum, EnumValue as ExternalEnumValue,
7+
Field as ExternalField, InputObjectType as ExternalInputObjectType,
8+
InputValue as ExternalInputValue, InterfaceType as ExternalInterfaceType,
9+
ObjectType as ExternalObjectType, ScalarType as ExternalScalarType, SchemaDefinition, Text,
10+
TypeDefinition as ExternalTypeDefinition, UnionType as ExternalUnionType,
11+
Value as ExternalValue,
12+
},
13+
Pos,
614
};
7-
use graphql_parser::schema::{Definition, Document, SchemaDefinition, Text};
8-
use graphql_parser::schema::{
9-
EnumType as ExternalEnum, EnumValue as ExternalEnumValue, Field as ExternalField,
10-
InputObjectType as ExternalInputObjectType, InputValue as ExternalInputValue,
11-
InterfaceType as ExternalInterfaceType, ObjectType as ExternalObjectType,
12-
ScalarType as ExternalScalarType, TypeDefinition as ExternalTypeDefinition,
13-
UnionType as ExternalUnionType, Value as ExternalValue,
14-
};
15-
use graphql_parser::Pos;
1615

17-
use crate::ast::{InputValue, Type};
18-
use crate::schema::meta::DeprecationStatus;
19-
use crate::schema::meta::{Argument, EnumValue, Field, MetaType};
20-
use crate::schema::model::SchemaType;
21-
use crate::schema::translate::SchemaTranslator;
22-
use crate::value::ScalarValue;
16+
use crate::{
17+
ast::{InputValue, Type},
18+
schema::{
19+
meta::{Argument, DeprecationStatus, EnumValue, Field, MetaType},
20+
model::SchemaType,
21+
translate::SchemaTranslator,
22+
},
23+
value::ScalarValue,
24+
};
2325

2426
pub struct GraphQLParserTranslator;
2527

juniper/src/types/async_await.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
ast::Selection,
33
executor::{ExecutionResult, Executor},
44
parser::Spanning,
5-
value::{Object, ScalarValue, Value, DefaultScalarValue},
5+
value::{DefaultScalarValue, Object, ScalarValue, Value},
66
};
77

88
use crate::BoxFuture;
@@ -117,9 +117,22 @@ crate::sa::assert_obj_safe!(GraphQLValueAsync<Context = (), TypeInfo = ()>);
117117
///
118118
/// It's automatically implemented for [`GraphQLValueAsync`] and [`GraphQLType`] implementors, so
119119
/// doesn't require manual or code-generated implementation.
120-
pub trait GraphQLTypeAsync<S = DefaultScalarValue>: GraphQLValueAsync<S> + GraphQLType<S> {}
120+
pub trait GraphQLTypeAsync<S = DefaultScalarValue>: GraphQLValueAsync<S> + GraphQLType<S>
121+
where
122+
Self::Context: Send + Sync,
123+
Self::TypeInfo: Send + Sync,
124+
S: ScalarValue + Send + Sync,
125+
{
126+
}
121127

122-
impl<S, T> GraphQLTypeAsync<S> for T where T: GraphQLValueAsync<S> + GraphQLType<S> {}
128+
impl<S, T> GraphQLTypeAsync<S> for T
129+
where
130+
T: GraphQLValueAsync<S> + GraphQLType<S>,
131+
T::Context: Send + Sync,
132+
T::TypeInfo: Send + Sync,
133+
S: ScalarValue + Send + Sync,
134+
{
135+
}
123136

124137
// Wrapper function around resolve_selection_set_into_async_recursive.
125138
// This wrapper is necessary because async fns can not be recursive.
@@ -175,7 +188,8 @@ where
175188
let meta_type = executor
176189
.schema()
177190
.concrete_type_by_name(
178-
instance.type_name(info)
191+
instance
192+
.type_name(info)
179193
.expect("Resolving named type's selection set")
180194
.as_ref(),
181195
)

juniper/src/types/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ where
417417
let meta_type = executor
418418
.schema()
419419
.concrete_type_by_name(
420-
instance.type_name(info)
420+
instance
421+
.type_name(info)
421422
.expect("Resolving named type's selection set")
422423
.as_ref(),
423424
)

juniper/src/types/containers.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@ use crate::{
22
ast::{FromInputValue, InputValue, Selection, ToInputValue},
33
executor::{ExecutionResult, Executor, Registry},
44
schema::meta::MetaType,
5-
types::{async_await::GraphQLValueAsync, base::{GraphQLType, GraphQLValue}},
5+
types::{
6+
async_await::GraphQLValueAsync,
7+
base::{GraphQLType, GraphQLValue},
8+
},
69
value::{ScalarValue, Value},
710
};
811

912
impl<S, T, CtxT> GraphQLType<S> for Option<T>
10-
where
11-
S: ScalarValue,
12-
T: GraphQLType<S, Context = CtxT>,
13+
where
14+
S: ScalarValue,
15+
T: GraphQLType<S, Context = CtxT>,
1316
{
1417
fn name(_: &T::TypeInfo) -> Option<&'static str> {
1518
None
1619
}
1720

1821
fn meta<'r>(info: &T::TypeInfo, registry: &mut Registry<'r, S>) -> MetaType<'r, S>
19-
where
20-
S: 'r,
22+
where
23+
S: 'r,
2124
{
2225
registry.build_nullable_type::<T>(info).into_meta()
2326
}
@@ -103,7 +106,6 @@ where
103106
T: GraphQLType<S, Context = CtxT>,
104107
S: ScalarValue,
105108
{
106-
107109
fn name(_: &T::TypeInfo) -> Option<&'static str> {
108110
None
109111
}

juniper/src/types/pointers.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ use crate::{
1313
};
1414

1515
impl<S, T, CtxT> GraphQLType<S> for Box<T>
16-
where
17-
S: ScalarValue,
18-
T: GraphQLType<S, Context = CtxT> + ?Sized,
16+
where
17+
S: ScalarValue,
18+
T: GraphQLType<S, Context = CtxT> + ?Sized,
1919
{
2020
fn name(info: &T::TypeInfo) -> Option<&str> {
2121
T::name(info)
2222
}
2323

2424
fn meta<'r>(info: &T::TypeInfo, registry: &mut Registry<'r, S>) -> MetaType<'r, S>
25-
where
26-
S: 'r,
25+
where
26+
S: 'r,
2727
{
2828
T::meta(info, registry)
2929
}

juniper/src/types/scalars.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
use std::{char, convert::From, marker::PhantomData, ops::Deref, rc::Rc, thread::JoinHandle, u32};
2+
13
use serde::{Deserialize, Serialize};
2-
use std::{char, convert::From, marker::PhantomData, ops::Deref, u32};
34

45
use crate::{
56
ast::{InputValue, Selection, ToInputValue},
67
executor::{ExecutionResult, Executor, Registry},
78
parser::{LexerError, ParseError, ScalarToken, Token},
89
schema::meta::MetaType,
9-
types::{base::{GraphQLType, GraphQLValue}, async_await::GraphQLValueAsync},
10+
types::{
11+
async_await::GraphQLValueAsync,
12+
base::{GraphQLType, GraphQLValue},
13+
},
1014
value::{ParseScalarResult, ScalarValue, Value},
1115
};
1216

@@ -215,7 +219,7 @@ where
215219
type Context = ();
216220
type TypeInfo = ();
217221

218-
fn type_name(&self, info: &()) -> Option<&'static str> {
222+
fn type_name<'i>(&self, info: &'i Self::TypeInfo) -> Option<&'i str> {
219223
<Self as GraphQLType<S>>::name(info)
220224
}
221225

@@ -334,7 +338,7 @@ where
334338
/// If you instantiate `RootNode` with this as the mutation, no mutation will be
335339
/// generated for the schema.
336340
#[derive(Debug)]
337-
pub struct EmptyMutation<T: ?Sized = ()>(PhantomData<std::thread::JoinHandle<T>>);
341+
pub struct EmptyMutation<T: ?Sized = ()>(PhantomData<JoinHandle<Box<T>>>);
338342

339343
// `EmptyMutation` doesn't use `T`, so should be `Send` and `Sync` even when `T` is not.
340344
crate::sa::assert_impl_all!(EmptyMutation<Rc<String>>: Send, Sync);
@@ -370,7 +374,7 @@ where
370374
type Context = T;
371375
type TypeInfo = ();
372376

373-
fn type_name(&self, info: &()) -> Option<&'static str> {
377+
fn type_name<'i>(&self, info: &'i Self::TypeInfo) -> Option<&'i str> {
374378
<Self as GraphQLType<S>>::name(info)
375379
}
376380
}
@@ -394,7 +398,7 @@ impl<T> Default for EmptyMutation<T> {
394398
///
395399
/// If you instantiate `RootNode` with this as the subscription,
396400
/// no subscriptions will be generated for the schema.
397-
pub struct EmptySubscription<T: ?Sized = ()>(PhantomData<std::thread::JoinHandle<T>>);
401+
pub struct EmptySubscription<T: ?Sized = ()>(PhantomData<JoinHandle<Box<T>>>);
398402

399403
// `EmptySubscription` doesn't use `T`, so should be `Send` and `Sync` even when `T` is not.
400404
crate::sa::assert_impl_all!(EmptySubscription<Rc<String>>: Send, Sync);
@@ -430,7 +434,7 @@ where
430434
type Context = T;
431435
type TypeInfo = ();
432436

433-
fn type_name(&self, info: &()) -> Option<&'static str> {
437+
fn type_name<'i>(&self, info: &'i Self::TypeInfo) -> Option<&'i str> {
434438
<Self as GraphQLType<S>>::name(info)
435439
}
436440
}

juniper/src/types/subscriptions.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
http::{GraphQLRequest, GraphQLResponse},
33
parser::Spanning,
44
types::base::{is_excluded, merge_key_into},
5-
Arguments, BoxFuture, Executor, FieldError, GraphQLValue, Object, ScalarValue, Selection, Value,
5+
Arguments, BoxFuture, Executor, FieldError, GraphQLType, Object, ScalarValue, Selection, Value,
66
ValuesStream,
77
};
88

@@ -68,7 +68,7 @@ pub trait SubscriptionConnection<'a, S>: futures::Stream<Item = GraphQLResponse<
6868
6969
See trait methods for more detailed explanation on how this trait works.
7070
*/
71-
pub trait GraphQLSubscriptionType<S>: GraphQLValue<S> + Send + Sync
71+
pub trait GraphQLSubscriptionType<S>: GraphQLType<S> + Send + Sync
7272
where
7373
Self::Context: Send + Sync,
7474
Self::TypeInfo: Send + Sync,
@@ -218,7 +218,8 @@ where
218218
let meta_type = executor
219219
.schema()
220220
.concrete_type_by_name(
221-
instance.type_name(info)
221+
instance
222+
.type_name(info)
222223
.expect("Resolving named type's selection set")
223224
.as_ref(),
224225
)

juniper_codegen/src/derive_scalar_value.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ fn impl_scalar_struct(
125125
selection_set: Option<&'a [#crate_name::Selection<__S>]>,
126126
executor: &'a #crate_name::Executor<Self::Context, __S>,
127127
) -> #crate_name::BoxFuture<'a, #crate_name::ExecutionResult<__S>> {
128-
use #crate_name::GraphQLType;
129128
use #crate_name::futures::future;
130-
let v = self.resolve(info, selection_set, executor);
129+
let v = #crate_name::GraphQLValue::resolve(self, info, selection_set, executor);
131130
Box::pin(future::ready(v))
132131
}
133132
}
@@ -164,7 +163,7 @@ fn impl_scalar_struct(
164163
type Context = ();
165164
type TypeInfo = ();
166165

167-
fn type_name(&self, info: &Self::TypeInfo) -> Option<&'static str> {
166+
fn type_name<'__i>(&self, info: &'__i Self::TypeInfo) -> Option<&'__i str> {
168167
<Self as #crate_name::GraphQLType<S>>::name(info)
169168
}
170169

@@ -174,7 +173,7 @@ fn impl_scalar_struct(
174173
selection: Option<&[#crate_name::Selection<S>]>,
175174
executor: &#crate_name::Executor<Self::Context, S>,
176175
) -> #crate_name::ExecutionResult<S> {
177-
#crate_name::GraphQLType::resolve(&self.0, info, selection, executor)
176+
#crate_name::GraphQLValue::resolve(&self.0, info, selection, executor)
178177
}
179178
}
180179

juniper_codegen/src/graphql_union/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ impl ToTokens for UnionDefinition {
552552
type Context = #context;
553553
type TypeInfo = ();
554554

555-
fn type_name(&self, info: &Self::TypeInfo) -> Option<&'static str> {
555+
fn type_name<'__i>(&self, info: &'__i Self::TypeInfo) -> Option<&'__i str> {
556556
<Self as #crate_path::GraphQLType<#scalar>>::name(info)
557557
}
558558

@@ -632,7 +632,13 @@ impl ToTokens for UnionDefinition {
632632
}
633633
};
634634

635-
into.append_all(&[union_impl, output_type_impl, type_impl, value_impl, value_async_impl]);
635+
into.append_all(&[
636+
union_impl,
637+
output_type_impl,
638+
type_impl,
639+
value_impl,
640+
value_async_impl,
641+
]);
636642
}
637643
}
638644

juniper_codegen/src/impl_scalar.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,8 @@ pub fn build_scalar(
263263
selection_set: Option<&'a [#crate_name::Selection<#async_generic_type>]>,
264264
executor: &'a #crate_name::Executor<Self::Context, #async_generic_type>,
265265
) -> #crate_name::BoxFuture<'a, #crate_name::ExecutionResult<#async_generic_type>> {
266-
use #crate_name::GraphQLType;
267266
use #crate_name::futures::future;
268-
let v = self.resolve(info, selection_set, executor);
267+
let v = #crate_name::GraphQLValue::resolve(self, info, selection_set, executor);
269268
Box::pin(future::ready(v))
270269
}
271270
}
@@ -306,7 +305,7 @@ pub fn build_scalar(
306305
type Context = ();
307306
type TypeInfo = ();
308307

309-
fn type_name(&self, info: &Self::TypeInfo) -> Option<&'static str> {
308+
fn type_name<'__i>(&self, info: &'__i Self::TypeInfo) -> Option<&'__i str> {
310309
<Self as #crate_name::GraphQLType<#generic_type>>::name(info)
311310
}
312311

0 commit comments

Comments
 (0)