Skip to content

Commit a4f8e67

Browse files
committed
Refactor, vol.1
1 parent 3e50adf commit a4f8e67

File tree

15 files changed

+430
-246
lines changed

15 files changed

+430
-246
lines changed

juniper/src/executor/mod.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
},
2323
model::{RootNode, SchemaType, TypeType},
2424
},
25-
types::{base::GraphQLType, name::Name},
25+
types::{base::{GraphQLType, GraphQLValue}, name::Name, async_await::GraphQLValueAsync},
2626
value::{DefaultScalarValue, ParseScalarValue, ScalarValue, Value},
2727
GraphQLError,
2828
};
@@ -244,7 +244,7 @@ impl<S> IntoFieldError<S> for FieldError<S> {
244244
#[doc(hidden)]
245245
pub trait IntoResolvable<'a, S, T, C>
246246
where
247-
T: GraphQLType<S>,
247+
T: GraphQLValue<S>,
248248
S: ScalarValue,
249249
{
250250
#[doc(hidden)]
@@ -253,7 +253,7 @@ where
253253

254254
impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for T
255255
where
256-
T: GraphQLType<S>,
256+
T: GraphQLValue<S>,
257257
S: ScalarValue,
258258
T::Context: FromContext<C>,
259259
{
@@ -265,7 +265,7 @@ where
265265
impl<'a, S, T, C, E: IntoFieldError<S>> IntoResolvable<'a, S, T, C> for Result<T, E>
266266
where
267267
S: ScalarValue,
268-
T: GraphQLType<S>,
268+
T: GraphQLValue<S>,
269269
T::Context: FromContext<C>,
270270
{
271271
fn into(self, ctx: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
@@ -277,7 +277,7 @@ where
277277
impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for (&'a T::Context, T)
278278
where
279279
S: ScalarValue,
280-
T: GraphQLType<S>,
280+
T: GraphQLValue<S>,
281281
{
282282
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
283283
Ok(Some(self))
@@ -287,7 +287,7 @@ where
287287
impl<'a, S, T, C> IntoResolvable<'a, S, Option<T>, C> for Option<(&'a T::Context, T)>
288288
where
289289
S: ScalarValue,
290-
T: GraphQLType<S>,
290+
T: GraphQLValue<S>,
291291
{
292292
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, Option<T>)>, S> {
293293
Ok(self.map(|(ctx, v)| (ctx, Some(v))))
@@ -297,7 +297,7 @@ where
297297
impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for FieldResult<(&'a T::Context, T), S>
298298
where
299299
S: ScalarValue,
300-
T: GraphQLType<S>,
300+
T: GraphQLValue<S>,
301301
{
302302
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
303303
self.map(Some)
@@ -308,7 +308,7 @@ impl<'a, S, T, C> IntoResolvable<'a, S, Option<T>, C>
308308
for FieldResult<Option<(&'a T::Context, T)>, S>
309309
where
310310
S: ScalarValue,
311-
T: GraphQLType<S>,
311+
T: GraphQLValue<S>,
312312
{
313313
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, Option<T>)>, S> {
314314
self.map(|o| o.map(|(ctx, v)| (ctx, Some(v))))
@@ -405,7 +405,7 @@ where
405405
pub fn resolve_with_ctx<NewCtxT, T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
406406
where
407407
NewCtxT: FromContext<CtxT>,
408-
T: GraphQLType<S, Context = NewCtxT> + ?Sized,
408+
T: GraphQLValue<S, Context = NewCtxT> + ?Sized,
409409
{
410410
self.replaced_context(<NewCtxT as FromContext<CtxT>>::from(self.context))
411411
.resolve(info, value)
@@ -414,15 +414,15 @@ where
414414
/// Resolve a single arbitrary value into an `ExecutionResult`
415415
pub fn resolve<T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
416416
where
417-
T: GraphQLType<S, Context = CtxT> + ?Sized,
417+
T: GraphQLValue<S, Context = CtxT> + ?Sized,
418418
{
419419
value.resolve(info, self.current_selection_set, self)
420420
}
421421

422422
/// Resolve a single arbitrary value into an `ExecutionResult`
423423
pub async fn resolve_async<T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
424424
where
425-
T: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync + ?Sized,
425+
T: GraphQLValueAsync<S, Context = CtxT> + Send + Sync + ?Sized,
426426
T::TypeInfo: Send + Sync,
427427
CtxT: Send + Sync,
428428
S: Send + Sync,
@@ -439,7 +439,7 @@ where
439439
value: &T,
440440
) -> ExecutionResult<S>
441441
where
442-
T: crate::GraphQLTypeAsync<S, Context = NewCtxT> + Send + Sync,
442+
T: GraphQLValueAsync<S, Context = NewCtxT> + Send + Sync,
443443
T::TypeInfo: Send + Sync,
444444
S: Send + Sync,
445445
NewCtxT: FromContext<CtxT> + Send + Sync,
@@ -453,23 +453,20 @@ where
453453
/// If the field fails to resolve, `null` will be returned.
454454
pub fn resolve_into_value<T>(&self, info: &T::TypeInfo, value: &T) -> Value<S>
455455
where
456-
T: GraphQLType<S, Context = CtxT>,
456+
T: GraphQLValue<S, Context = CtxT>,
457457
{
458-
match self.resolve(info, value) {
459-
Ok(v) => v,
460-
Err(e) => {
461-
self.push_error(e);
462-
Value::null()
463-
}
464-
}
458+
self.resolve(info, value).unwrap_or_else(|e| {
459+
self.push_error(e);
460+
Value::null()
461+
})
465462
}
466463

467464
/// Resolve a single arbitrary value into a return value
468465
///
469466
/// If the field fails to resolve, `null` will be returned.
470467
pub async fn resolve_into_value_async<T>(&self, info: &T::TypeInfo, value: &T) -> Value<S>
471468
where
472-
T: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync + ?Sized,
469+
T: GraphQLValueAsync<S, Context = CtxT> + Send + Sync + ?Sized,
473470
T::TypeInfo: Send + Sync,
474471
CtxT: Send + Sync,
475472
S: Send + Sync,
@@ -757,9 +754,9 @@ pub fn execute_validated_query<'a, 'b, QueryT, MutationT, SubscriptionT, CtxT, S
757754
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
758755
where
759756
S: ScalarValue,
760-
QueryT: GraphQLType<S, Context = CtxT>,
761-
MutationT: GraphQLType<S, Context = CtxT>,
762-
SubscriptionT: GraphQLType<S, Context = CtxT>,
757+
QueryT: GraphQLValue<S, Context = CtxT>,
758+
MutationT: GraphQLValue<S, Context = CtxT>,
759+
SubscriptionT: GraphQLValue<S, Context = CtxT>,
763760
{
764761
if operation.item.operation_type == OperationType::Subscription {
765762
return Err(GraphQLError::IsSubscription);
@@ -851,11 +848,11 @@ pub async fn execute_validated_query_async<'a, 'b, QueryT, MutationT, Subscripti
851848
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
852849
where
853850
S: ScalarValue + Send + Sync,
854-
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
851+
QueryT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
855852
QueryT::TypeInfo: Send + Sync,
856-
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
853+
MutationT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
857854
MutationT::TypeInfo: Send + Sync,
858-
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
855+
SubscriptionT: GraphQLValue<S, Context = CtxT> + Send + Sync,
859856
SubscriptionT::TypeInfo: Send + Sync,
860857
CtxT: Send + Sync,
861858
{
@@ -998,9 +995,9 @@ where
998995
'd: 'r,
999996
'op: 'd,
1000997
S: ScalarValue + Send + Sync,
1001-
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
998+
QueryT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
1002999
QueryT::TypeInfo: Send + Sync,
1003-
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
1000+
MutationT: GraphQLValueAsync<S, Context = CtxT> + Send + Sync,
10041001
MutationT::TypeInfo: Send + Sync,
10051002
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
10061003
SubscriptionT::TypeInfo: Send + Sync,

juniper/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ pub use crate::{
185185
model::{RootNode, SchemaType},
186186
},
187187
types::{
188-
async_await::GraphQLTypeAsync,
189-
base::{Arguments, GraphQLType, TypeKind},
188+
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
189+
base::{Arguments, GraphQLType, GraphQLValue, TypeKind},
190190
marker::{self, GraphQLUnion},
191191
scalars::{EmptyMutation, EmptySubscription, ID},
192192
subscriptions::{GraphQLSubscriptionType, SubscriptionConnection, SubscriptionCoordinator},

juniper/src/macros/interface.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ macro_rules! graphql_interface {
134134
) => {
135135
$crate::__juniper_impl_trait!(
136136
impl<$($scalar)* $(, $lifetimes)* > GraphQLType for $name {
137-
type Context = $ctx;
138-
type TypeInfo = ();
139-
140-
fn name(_ : &Self::TypeInfo) -> Option<&str> {
137+
fn name(_ : &Self::TypeInfo) -> Option<&'static str> {
141138
Some($($outname)*)
142139
}
143140

@@ -178,7 +175,17 @@ macro_rules! graphql_interface {
178175
$(.description($desciption))*
179176
.into_meta()
180177
}
178+
}
179+
);
181180

181+
$crate::__juniper_impl_trait!(
182+
impl<$($scalar)* $(, $lifetimes)* > GraphQLValue for $name {
183+
type Context = $ctx;
184+
type TypeInfo = ();
185+
186+
fn type_name(&self, _ : &Self::TypeInfo) -> Option<&'static str> {
187+
Some($($outname)*)
188+
}
182189

183190
#[allow(unused_variables)]
184191
fn resolve_field(

juniper/src/schema/schema.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
ast::Selection,
33
executor::{ExecutionResult, Executor, Registry},
4-
types::base::{Arguments, GraphQLType, TypeKind},
4+
types::{base::{Arguments, GraphQLType, GraphQLValue, TypeKind}, async_await::{GraphQLValueAsync, GraphQLTypeAsync}},
55
value::{ScalarValue, Value},
66
};
77

@@ -21,9 +21,6 @@ where
2121
MutationT: GraphQLType<S, Context = CtxT>,
2222
SubscriptionT: GraphQLType<S, Context = CtxT>,
2323
{
24-
type Context = CtxT;
25-
type TypeInfo = QueryT::TypeInfo;
26-
2724
fn name(info: &QueryT::TypeInfo) -> Option<&str> {
2825
QueryT::name(info)
2926
}
@@ -34,6 +31,22 @@ where
3431
{
3532
QueryT::meta(info, registry)
3633
}
34+
}
35+
36+
impl<'a, CtxT, S, QueryT, MutationT, SubscriptionT> GraphQLValue<S>
37+
for RootNode<'a, QueryT, MutationT, SubscriptionT, S>
38+
where
39+
S: ScalarValue,
40+
QueryT: GraphQLType<S, Context = CtxT>,
41+
MutationT: GraphQLType<S, Context = CtxT>,
42+
SubscriptionT: GraphQLType<S, Context = CtxT>,
43+
{
44+
type Context = CtxT;
45+
type TypeInfo = QueryT::TypeInfo;
46+
47+
fn type_name<'i>(&self, info: &'i QueryT::TypeInfo) -> Option<&'i str> {
48+
QueryT::name(info)
49+
}
3750

3851
fn resolve_field(
3952
&self,
@@ -77,13 +90,13 @@ where
7790
}
7891
}
7992

80-
impl<'a, CtxT, S, QueryT, MutationT, SubscriptionT> crate::GraphQLTypeAsync<S>
93+
impl<'a, CtxT, S, QueryT, MutationT, SubscriptionT> GraphQLValueAsync<S>
8194
for RootNode<'a, QueryT, MutationT, SubscriptionT, S>
8295
where
8396
S: ScalarValue + Send + Sync,
84-
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT>,
97+
QueryT: GraphQLTypeAsync<S, Context = CtxT>,
8598
QueryT::TypeInfo: Send + Sync,
86-
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT>,
99+
MutationT: GraphQLTypeAsync<S, Context = CtxT>,
87100
MutationT::TypeInfo: Send + Sync,
88101
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
89102
SubscriptionT::TypeInfo: Send + Sync,

0 commit comments

Comments
 (0)