Skip to content

Commit 16b5035

Browse files
committed
Refactor, vol.4
1 parent 839be1c commit 16b5035

File tree

11 files changed

+86
-59
lines changed

11 files changed

+86
-59
lines changed

juniper/src/executor/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::{
2626
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
2727
base::{GraphQLType, GraphQLValue},
2828
name::Name,
29+
subscriptions::{GraphQLSubscriptionType, GraphQLSubscriptionValue},
2930
},
3031
value::{DefaultScalarValue, ParseScalarValue, ScalarValue, Value},
3132
GraphQLError,
@@ -373,7 +374,7 @@ where
373374
'i: 'res,
374375
'v: 'res,
375376
'a: 'res,
376-
T: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
377+
T: GraphQLSubscriptionValue<S, Context = CtxT> + Send + Sync,
377378
T::TypeInfo: Send + Sync,
378379
CtxT: Send + Sync,
379380
S: Send + Sync,
@@ -397,7 +398,7 @@ where
397398
where
398399
't: 'res,
399400
'a: 'res,
400-
T: crate::GraphQLSubscriptionType<S, Context = CtxT>,
401+
T: GraphQLSubscriptionValue<S, Context = CtxT>,
401402
T::TypeInfo: Send + Sync,
402403
CtxT: Send + Sync,
403404
S: Send + Sync,
@@ -1003,7 +1004,7 @@ where
10031004
QueryT::TypeInfo: Send + Sync,
10041005
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
10051006
MutationT::TypeInfo: Send + Sync,
1006-
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
1007+
SubscriptionT: GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
10071008
SubscriptionT::TypeInfo: Send + Sync,
10081009
CtxT: Send + Sync + 'r,
10091010
{

juniper/src/http/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ where
106106
) -> GraphQLResponse<'a, S>
107107
where
108108
S: ScalarValue + Send + Sync,
109-
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
109+
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
110110
QueryT::TypeInfo: Send + Sync,
111-
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
111+
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
112112
MutationT::TypeInfo: Send + Sync,
113113
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
114114
SubscriptionT::TypeInfo: Send + Sync,
@@ -259,13 +259,13 @@ where
259259
/// This is a simple wrapper around the `execute_sync` function exposed in GraphQLRequest.
260260
pub fn execute_sync<'a, CtxT, QueryT, MutationT, SubscriptionT>(
261261
&'a self,
262-
root_node: &'a crate::RootNode<QueryT, MutationT, SubscriptionT, S>,
262+
root_node: &'a RootNode<QueryT, MutationT, SubscriptionT, S>,
263263
context: &CtxT,
264264
) -> GraphQLBatchResponse<'a, S>
265265
where
266-
QueryT: crate::GraphQLType<S, Context = CtxT>,
267-
MutationT: crate::GraphQLType<S, Context = CtxT>,
268-
SubscriptionT: crate::GraphQLType<S, Context = CtxT>,
266+
QueryT: GraphQLType<S, Context = CtxT>,
267+
MutationT: GraphQLType<S, Context = CtxT>,
268+
SubscriptionT: GraphQLType<S, Context = CtxT>,
269269
{
270270
match *self {
271271
Self::Single(ref req) => {
@@ -285,15 +285,15 @@ where
285285
/// GraphQLRequest
286286
pub async fn execute<'a, CtxT, QueryT, MutationT, SubscriptionT>(
287287
&'a self,
288-
root_node: &'a crate::RootNode<'a, QueryT, MutationT, SubscriptionT, S>,
288+
root_node: &'a RootNode<'a, QueryT, MutationT, SubscriptionT, S>,
289289
context: &'a CtxT,
290290
) -> GraphQLBatchResponse<'a, S>
291291
where
292-
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
292+
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
293293
QueryT::TypeInfo: Send + Sync,
294-
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
294+
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
295295
MutationT::TypeInfo: Send + Sync,
296-
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
296+
SubscriptionT: GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
297297
SubscriptionT::TypeInfo: Send + Sync,
298298
CtxT: Send + Sync,
299299
S: Send + Sync,

juniper/src/integrations/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Provides GraphQLType implementations for some external types
2+
23
#[doc(hidden)]
34
pub mod serde;
45

juniper/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ pub use crate::{
189189
base::{Arguments, GraphQLType, GraphQLValue, TypeKind},
190190
marker::{self, GraphQLUnion},
191191
scalars::{EmptyMutation, EmptySubscription, ID},
192-
subscriptions::{GraphQLSubscriptionType, SubscriptionConnection, SubscriptionCoordinator},
192+
subscriptions::{
193+
GraphQLSubscriptionType, GraphQLSubscriptionValue, SubscriptionConnection,
194+
SubscriptionCoordinator,
195+
},
193196
},
194197
validation::RuleError,
195198
value::{DefaultScalarValue, Object, ParseScalarResult, ParseScalarValue, ScalarValue, Value},

juniper/src/macros/subscription_helpers.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use futures::Stream;
77

8-
use crate::{FieldError, GraphQLType, ScalarValue};
8+
use crate::{FieldError, GraphQLValue, ScalarValue};
99

1010
/// Trait for converting `T` to `Ok(T)` if T is not Result.
1111
/// This is useful in subscription macros when user can provide type alias for
@@ -50,7 +50,7 @@ pub struct ResultStreamItem;
5050
pub struct ResultStreamResult;
5151

5252
/// This trait is used in `juniper::graphql_subscription` macro to get stream's
53-
/// item type that implements `GraphQLType` from type alias provided
53+
/// item type that implements `GraphQLValue` from type alias provided
5454
/// by user.
5555
pub trait ExtractTypeFromStream<T, S>
5656
where
@@ -59,13 +59,13 @@ where
5959
/// Stream's return Value that will be returned if
6060
/// no errors occured. Is used to determine field type in
6161
/// `#[juniper::graphql_subscription]`
62-
type Item: GraphQLType<S>;
62+
type Item: GraphQLValue<S>;
6363
}
6464

6565
impl<T, I, S> ExtractTypeFromStream<StreamItem, S> for T
6666
where
6767
T: futures::Stream<Item = I>,
68-
I: GraphQLType<S>,
68+
I: GraphQLValue<S>,
6969
S: ScalarValue,
7070
{
7171
type Item = I;
@@ -74,7 +74,7 @@ where
7474
impl<Ty, T, E, S> ExtractTypeFromStream<StreamResult, S> for Ty
7575
where
7676
Ty: futures::Stream<Item = Result<T, E>>,
77-
T: GraphQLType<S>,
77+
T: GraphQLValue<S>,
7878
S: ScalarValue,
7979
{
8080
type Item = T;
@@ -83,7 +83,7 @@ where
8383
impl<T, I, E, S> ExtractTypeFromStream<ResultStreamItem, S> for Result<T, E>
8484
where
8585
T: futures::Stream<Item = I>,
86-
I: GraphQLType<S>,
86+
I: GraphQLValue<S>,
8787
S: ScalarValue,
8888
{
8989
type Item = I;
@@ -92,7 +92,7 @@ where
9292
impl<T, E, I, ER, S> ExtractTypeFromStream<ResultStreamResult, S> for Result<T, E>
9393
where
9494
T: futures::Stream<Item = Result<I, ER>>,
95-
I: GraphQLType<S>,
95+
I: GraphQLValue<S>,
9696
S: ScalarValue,
9797
{
9898
type Item = I;

juniper/src/macros/tests/util.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use crate::{DefaultScalarValue, GraphQLTypeAsync, RootNode, Value, Variables};
2-
use std::default::Default;
1+
use crate::{DefaultScalarValue, GraphQLType, GraphQLTypeAsync, RootNode, Value, Variables};
32

43
pub async fn run_query<Query, Mutation, Subscription, Context>(query: &str) -> Value
54
where
65
Query: GraphQLTypeAsync<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
76
Mutation: GraphQLTypeAsync<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
8-
Subscription: crate::GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context>
9-
+ Default
10-
+ Sync
11-
+ Send,
7+
Subscription:
8+
GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default + Sync + Send,
129
Context: Default + Send + Sync,
1310
{
1411
let schema = RootNode::new(
@@ -29,10 +26,8 @@ pub async fn run_info_query<Query, Mutation, Subscription, Context>(type_name: &
2926
where
3027
Query: GraphQLTypeAsync<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
3128
Mutation: GraphQLTypeAsync<DefaultScalarValue, TypeInfo = (), Context = Context> + Default,
32-
Subscription: crate::GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context>
33-
+ Default
34-
+ Sync
35-
+ Send,
29+
Subscription:
30+
GraphQLType<DefaultScalarValue, TypeInfo = (), Context = Context> + Default + Sync + Send,
3631
Context: Default + Send + Sync,
3732
{
3833
let query = format!(

juniper/src/types/marker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! traits are used. Encountering an error where one of these traits
66
//! is involved implies that the construct is not valid in GraphQL.
77
8-
use crate::{GraphQLType, GraphQLValue, ScalarValue};
8+
use crate::{GraphQLType, ScalarValue};
99

1010
/// Maker object for GraphQL objects.
1111
///
@@ -37,7 +37,7 @@ pub trait GraphQLObjectType<S: ScalarValue>: GraphQLType<S> {
3737
/// [4]: https://spec.graphql.org/June2018/#sec-Objects
3838
/// [5]: https://spec.graphql.org/June2018/#sec-Input-Objects
3939
/// [6]: https://spec.graphql.org/June2018/#sec-Interfaces
40-
pub trait GraphQLUnion<S: ScalarValue>: GraphQLValue<S> {
40+
pub trait GraphQLUnion<S: ScalarValue>: GraphQLType<S> {
4141
/// An arbitrary function without meaning.
4242
///
4343
/// May contain compile timed check logic which ensures that types are used correctly according

juniper/src/types/scalars.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
types::{
1111
async_await::GraphQLValueAsync,
1212
base::{GraphQLType, GraphQLValue},
13+
subscriptions::GraphQLSubscriptionValue,
1314
},
1415
value::{ParseScalarResult, ScalarValue, Value},
1516
};
@@ -439,12 +440,11 @@ where
439440
}
440441
}
441442

442-
impl<T, S> crate::GraphQLSubscriptionType<S> for EmptySubscription<T>
443+
impl<T, S> GraphQLSubscriptionValue<S> for EmptySubscription<T>
443444
where
444445
S: ScalarValue + Send + Sync + 'static,
445446
Self::TypeInfo: Send + Sync,
446447
Self::Context: Send + Sync,
447-
T: Send + Sync,
448448
{
449449
}
450450

juniper/src/types/subscriptions.rs

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

99
/// Global subscription coordinator trait.
@@ -58,23 +58,24 @@ where
5858
/// server integration crates.
5959
pub trait SubscriptionConnection<'a, S>: futures::Stream<Item = GraphQLResponse<'a, S>> {}
6060

61-
/**
62-
This trait adds resolver logic with asynchronous subscription execution logic
63-
on GraphQL types. It should be used with `GraphQLValue` in order to implement
64-
subscription resolvers on GraphQL objects.
65-
66-
Subscription-related convenience macros expand into an implementation of this
67-
trait and `GraphQLValue` for the given type.
68-
69-
See trait methods for more detailed explanation on how this trait works.
70-
*/
71-
pub trait GraphQLSubscriptionType<S>: GraphQLType<S> + Send + Sync
61+
/// Extension of [`GraphQLValue`] trait with asynchronous [subscription][1] execution logic.
62+
/// It should be used with [`GraphQLValue`] in order to implement [subscription][1] resolvers on
63+
/// [GraphQL objects][2].
64+
///
65+
/// [Subscription][1]-related convenience macros expand into an implementation of this trait and
66+
/// [`GraphQLValue`] for the given type.
67+
///
68+
/// See trait methods for more detailed explanation on how this trait works.
69+
///
70+
/// [1]: https://spec.graphql.org/June2018/#sec-Subscription
71+
/// [2]: https://spec.graphql.org/June2018/#sec-Objects
72+
pub trait GraphQLSubscriptionValue<S = DefaultScalarValue>: GraphQLValue<S> + Send + Sync
7273
where
7374
Self::Context: Send + Sync,
7475
Self::TypeInfo: Send + Sync,
7576
S: ScalarValue + Send + Sync,
7677
{
77-
/// Resolve into `Value<ValuesStream>`
78+
/// Resolves into `Value<ValuesStream>`.
7879
///
7980
/// ## Default implementation
8081
///
@@ -169,6 +170,32 @@ where
169170
}
170171
}
171172

173+
crate::sa::assert_obj_safe!(GraphQLSubscriptionValue<Context = (), TypeInfo = ()>);
174+
175+
/// Extension of [`GraphQLType`] trait with asynchronous [subscription][1] execution logic.
176+
///
177+
/// It's automatically implemented for [`GraphQLSubscriptionValue`] and [`GraphQLType`]
178+
/// implementors, so doesn't require manual or code-generated implementation.
179+
///
180+
/// [1]: https://spec.graphql.org/June2018/#sec-Subscription
181+
pub trait GraphQLSubscriptionType<S = DefaultScalarValue>:
182+
GraphQLSubscriptionValue<S> + GraphQLType<S>
183+
where
184+
Self::Context: Send + Sync,
185+
Self::TypeInfo: Send + Sync,
186+
S: ScalarValue + Send + Sync,
187+
{
188+
}
189+
190+
impl<S, T> GraphQLSubscriptionType<S> for T
191+
where
192+
T: GraphQLSubscriptionValue<S> + GraphQLType<S>,
193+
T::Context: Send + Sync,
194+
T::TypeInfo: Send + Sync,
195+
S: ScalarValue + Send + Sync,
196+
{
197+
}
198+
172199
/// Wrapper function around `resolve_selection_set_into_stream_recursive`.
173200
/// This wrapper is necessary because async fns can not be recursive.
174201
/// Panics if executor's current selection set is None.
@@ -184,7 +211,7 @@ where
184211
'e: 'fut,
185212
'ref_e: 'fut,
186213
'res: 'fut,
187-
T: GraphQLSubscriptionType<S, Context = CtxT> + ?Sized,
214+
T: GraphQLSubscriptionValue<S, Context = CtxT> + ?Sized,
188215
T::TypeInfo: Send + Sync,
189216
S: ScalarValue + Send + Sync,
190217
CtxT: Send + Sync,
@@ -203,7 +230,7 @@ async fn resolve_selection_set_into_stream_recursive<'i, 'inf, 'ref_e, 'e, 'res,
203230
executor: &'ref_e Executor<'ref_e, 'e, CtxT, S>,
204231
) -> Value<ValuesStream<'res, S>>
205232
where
206-
T: GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync + ?Sized,
233+
T: GraphQLSubscriptionValue<S, Context = CtxT> + Send + Sync + ?Sized,
207234
T::TypeInfo: Send + Sync,
208235
S: ScalarValue + Send + Sync,
209236
CtxT: Send + Sync,

juniper/src/value/scalar.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -190,34 +190,34 @@ pub trait ScalarValue:
190190

191191
/// Convert the given scalar value into an integer value
192192
///
193-
/// This function is used for implementing `GraphQLType` for `i32` for all
193+
/// This function is used for implementing `GraphQLValue` for `i32` for all
194194
/// scalar values. Implementations should convert all supported integer
195195
/// types with 32 bit or less to an integer if requested.
196196
fn as_int(&self) -> Option<i32>;
197197

198198
/// Convert the given scalar value into a string value
199199
///
200-
/// This function is used for implementing `GraphQLType` for `String` for all
200+
/// This function is used for implementing `GraphQLValue` for `String` for all
201201
/// scalar values
202202
fn as_string(&self) -> Option<String>;
203203

204204
/// Convert the given scalar value into a string value
205205
///
206-
/// This function is used for implementing `GraphQLType` for `String` for all
206+
/// This function is used for implementing `GraphQLValue` for `String` for all
207207
/// scalar values
208208
fn as_str(&self) -> Option<&str>;
209209

210210
/// Convert the given scalar value into a float value
211211
///
212-
/// This function is used for implementing `GraphQLType` for `f64` for all
212+
/// This function is used for implementing `GraphQLValue` for `f64` for all
213213
/// scalar values. Implementations should convert all supported integer
214214
/// types with 64 bit or less and all floating point values with 64 bit or
215215
/// less to a float if requested.
216216
fn as_float(&self) -> Option<f64>;
217217

218218
/// Convert the given scalar value into a boolean value
219219
///
220-
/// This function is used for implementing `GraphQLType` for `bool` for all
220+
/// This function is used for implementing `GraphQLValue` for `bool` for all
221221
/// scalar values.
222222
fn as_boolean(&self) -> Option<bool>;
223223
}

juniper_codegen/src/util/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl GraphQLTypeDefiniton {
12911291
);
12921292

12931293
let subscription_implementation = quote!(
1294-
impl#impl_generics #juniper_crate_name::GraphQLSubscriptionType<#scalar> for #ty #type_generics_tokens
1294+
impl#impl_generics #juniper_crate_name::GraphQLSubscriptionValue<#scalar> for #ty #type_generics_tokens
12951295
#where_clause
12961296
{
12971297
#[allow(unused_variables)]
@@ -1326,7 +1326,7 @@ impl GraphQLTypeDefiniton {
13261326
match field_name {
13271327
#( #resolve_matches_async )*
13281328
_ => {
1329-
panic!("Field {} not found on type {}", field_name, "GraphQLSubscriptionType");
1329+
panic!("Field {} not found on type {}", field_name, "GraphQLSubscriptionValue");
13301330
}
13311331
}
13321332
}

0 commit comments

Comments
 (0)