Skip to content

Decouple object safe part from GraphQLType #685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions integration_tests/juniper_tests/src/codegen/derive_input_object.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fnv::FnvHashMap;

use juniper::{
self, DefaultScalarValue, FromInputValue, GraphQLInputObject, GraphQLType, InputValue,
DefaultScalarValue, FromInputValue, GraphQLInputObject, GraphQLType, GraphQLValue, InputValue,
ToInputValue,
};

Expand Down Expand Up @@ -63,9 +63,6 @@ impl<'a> ToInputValue for &'a Fake {
}

impl<'a> GraphQLType<DefaultScalarValue> for &'a Fake {
type Context = ();
type TypeInfo = ();

fn name(_: &()) -> Option<&'static str> {
None
}
Expand All @@ -85,6 +82,15 @@ impl<'a> GraphQLType<DefaultScalarValue> for &'a Fake {
}
}

impl<'a> GraphQLValue<DefaultScalarValue> for &'a Fake {
type Context = ();
type TypeInfo = ();

fn type_name<'i>(&self, info: &'i Self::TypeInfo) -> Option<&'i str> {
<Self as GraphQLType>::name(info)
}
}

#[derive(GraphQLInputObject, Debug, PartialEq)]
#[graphql(scalar = DefaultScalarValue)]
struct WithLifetime<'a> {
Expand Down
4 changes: 4 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@

## Breaking Changes

- `GraphQLType` trait was split into 2 traits: ([#685](https://github.com/graphql-rust/juniper/pull/685))
- object safe `GraphQLValue` trait containing resolving logic;
- static `GraphQLType` trait containing GraphQL type information.

- `juniper::graphiql` has moved to `juniper::http::graphiql`.
- `juniper::http::graphiql::graphiql_source()` now requires a second parameter for subscriptions.

Expand Down
58 changes: 30 additions & 28 deletions juniper/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ use crate::{
},
model::{RootNode, SchemaType, TypeType},
},
types::{base::GraphQLType, name::Name},
types::{
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
base::{GraphQLType, GraphQLValue},
name::Name,
subscriptions::{GraphQLSubscriptionType, GraphQLSubscriptionValue},
},
value::{DefaultScalarValue, ParseScalarValue, ScalarValue, Value},
GraphQLError,
};
Expand Down Expand Up @@ -244,7 +249,7 @@ impl<S> IntoFieldError<S> for FieldError<S> {
#[doc(hidden)]
pub trait IntoResolvable<'a, S, T, C>
where
T: GraphQLType<S>,
T: GraphQLValue<S>,
S: ScalarValue,
{
#[doc(hidden)]
Expand All @@ -253,7 +258,7 @@ where

impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for T
where
T: GraphQLType<S>,
T: GraphQLValue<S>,
S: ScalarValue,
T::Context: FromContext<C>,
{
Expand All @@ -265,7 +270,7 @@ where
impl<'a, S, T, C, E: IntoFieldError<S>> IntoResolvable<'a, S, T, C> for Result<T, E>
where
S: ScalarValue,
T: GraphQLType<S>,
T: GraphQLValue<S>,
T::Context: FromContext<C>,
{
fn into(self, ctx: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
Expand All @@ -277,7 +282,7 @@ where
impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for (&'a T::Context, T)
where
S: ScalarValue,
T: GraphQLType<S>,
T: GraphQLValue<S>,
{
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
Ok(Some(self))
Expand All @@ -287,7 +292,7 @@ where
impl<'a, S, T, C> IntoResolvable<'a, S, Option<T>, C> for Option<(&'a T::Context, T)>
where
S: ScalarValue,
T: GraphQLType<S>,
T: GraphQLValue<S>,
{
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, Option<T>)>, S> {
Ok(self.map(|(ctx, v)| (ctx, Some(v))))
Expand All @@ -297,7 +302,7 @@ where
impl<'a, S, T, C> IntoResolvable<'a, S, T, C> for FieldResult<(&'a T::Context, T), S>
where
S: ScalarValue,
T: GraphQLType<S>,
T: GraphQLValue<S>,
{
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, T)>, S> {
self.map(Some)
Expand All @@ -308,7 +313,7 @@ impl<'a, S, T, C> IntoResolvable<'a, S, Option<T>, C>
for FieldResult<Option<(&'a T::Context, T)>, S>
where
S: ScalarValue,
T: GraphQLType<S>,
T: GraphQLValue<S>,
{
fn into(self, _: &'a C) -> FieldResult<Option<(&'a T::Context, Option<T>)>, S> {
self.map(|o| o.map(|(ctx, v)| (ctx, Some(v))))
Expand Down Expand Up @@ -369,7 +374,7 @@ where
'i: 'res,
'v: 'res,
'a: 'res,
T: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
T: GraphQLSubscriptionValue<S, Context = CtxT> + Send + Sync,
T::TypeInfo: Send + Sync,
CtxT: Send + Sync,
S: Send + Sync,
Expand All @@ -393,7 +398,7 @@ where
where
't: 'res,
'a: 'res,
T: crate::GraphQLSubscriptionType<S, Context = CtxT>,
T: GraphQLSubscriptionValue<S, Context = CtxT>,
T::TypeInfo: Send + Sync,
CtxT: Send + Sync,
S: Send + Sync,
Expand All @@ -405,7 +410,7 @@ where
pub fn resolve_with_ctx<NewCtxT, T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
where
NewCtxT: FromContext<CtxT>,
T: GraphQLType<S, Context = NewCtxT> + ?Sized,
T: GraphQLValue<S, Context = NewCtxT> + ?Sized,
{
self.replaced_context(<NewCtxT as FromContext<CtxT>>::from(self.context))
.resolve(info, value)
Expand All @@ -414,15 +419,15 @@ where
/// Resolve a single arbitrary value into an `ExecutionResult`
pub fn resolve<T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
where
T: GraphQLType<S, Context = CtxT> + ?Sized,
T: GraphQLValue<S, Context = CtxT> + ?Sized,
{
value.resolve(info, self.current_selection_set, self)
}

/// Resolve a single arbitrary value into an `ExecutionResult`
pub async fn resolve_async<T>(&self, info: &T::TypeInfo, value: &T) -> ExecutionResult<S>
where
T: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync + ?Sized,
T: GraphQLValueAsync<S, Context = CtxT> + Send + Sync + ?Sized,
T::TypeInfo: Send + Sync,
CtxT: Send + Sync,
S: Send + Sync,
Expand All @@ -439,7 +444,7 @@ where
value: &T,
) -> ExecutionResult<S>
where
T: crate::GraphQLTypeAsync<S, Context = NewCtxT> + Send + Sync,
T: GraphQLValueAsync<S, Context = NewCtxT> + Send + Sync,
T::TypeInfo: Send + Sync,
S: Send + Sync,
NewCtxT: FromContext<CtxT> + Send + Sync,
Expand All @@ -453,23 +458,20 @@ where
/// If the field fails to resolve, `null` will be returned.
pub fn resolve_into_value<T>(&self, info: &T::TypeInfo, value: &T) -> Value<S>
where
T: GraphQLType<S, Context = CtxT>,
T: GraphQLValue<S, Context = CtxT>,
{
match self.resolve(info, value) {
Ok(v) => v,
Err(e) => {
self.push_error(e);
Value::null()
}
}
self.resolve(info, value).unwrap_or_else(|e| {
self.push_error(e);
Value::null()
})
}

/// Resolve a single arbitrary value into a return value
///
/// If the field fails to resolve, `null` will be returned.
pub async fn resolve_into_value_async<T>(&self, info: &T::TypeInfo, value: &T) -> Value<S>
where
T: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync + ?Sized,
T: GraphQLValueAsync<S, Context = CtxT> + Send + Sync + ?Sized,
T::TypeInfo: Send + Sync,
CtxT: Send + Sync,
S: Send + Sync,
Expand Down Expand Up @@ -851,9 +853,9 @@ pub async fn execute_validated_query_async<'a, 'b, QueryT, MutationT, Subscripti
) -> Result<(Value<S>, Vec<ExecutionError<S>>), GraphQLError<'a>>
where
S: ScalarValue + Send + Sync,
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT::TypeInfo: Send + Sync,
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT::TypeInfo: Send + Sync,
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
SubscriptionT::TypeInfo: Send + Sync,
Expand Down Expand Up @@ -998,11 +1000,11 @@ where
'd: 'r,
'op: 'd,
S: ScalarValue + Send + Sync,
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT::TypeInfo: Send + Sync,
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT::TypeInfo: Send + Sync,
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
SubscriptionT: GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
SubscriptionT::TypeInfo: Send + Sync,
CtxT: Send + Sync + 'r,
{
Expand Down
20 changes: 10 additions & 10 deletions juniper/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ where
) -> GraphQLResponse<'a, S>
where
S: ScalarValue + Send + Sync,
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT::TypeInfo: Send + Sync,
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT::TypeInfo: Send + Sync,
SubscriptionT: GraphQLType<S, Context = CtxT> + Send + Sync,
SubscriptionT::TypeInfo: Send + Sync,
Expand Down Expand Up @@ -259,13 +259,13 @@ where
/// This is a simple wrapper around the `execute_sync` function exposed in GraphQLRequest.
pub fn execute_sync<'a, CtxT, QueryT, MutationT, SubscriptionT>(
&'a self,
root_node: &'a crate::RootNode<QueryT, MutationT, SubscriptionT, S>,
root_node: &'a RootNode<QueryT, MutationT, SubscriptionT, S>,
context: &CtxT,
) -> GraphQLBatchResponse<'a, S>
where
QueryT: crate::GraphQLType<S, Context = CtxT>,
MutationT: crate::GraphQLType<S, Context = CtxT>,
SubscriptionT: crate::GraphQLType<S, Context = CtxT>,
QueryT: GraphQLType<S, Context = CtxT>,
MutationT: GraphQLType<S, Context = CtxT>,
SubscriptionT: GraphQLType<S, Context = CtxT>,
{
match *self {
Self::Single(ref req) => {
Expand All @@ -285,15 +285,15 @@ where
/// GraphQLRequest
pub async fn execute<'a, CtxT, QueryT, MutationT, SubscriptionT>(
&'a self,
root_node: &'a crate::RootNode<'a, QueryT, MutationT, SubscriptionT, S>,
root_node: &'a RootNode<'a, QueryT, MutationT, SubscriptionT, S>,
context: &'a CtxT,
) -> GraphQLBatchResponse<'a, S>
where
QueryT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
QueryT::TypeInfo: Send + Sync,
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT: GraphQLTypeAsync<S, Context = CtxT> + Send + Sync,
MutationT::TypeInfo: Send + Sync,
SubscriptionT: crate::GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
SubscriptionT: GraphQLSubscriptionType<S, Context = CtxT> + Send + Sync,
SubscriptionT::TypeInfo: Send + Sync,
CtxT: Send + Sync,
S: Send + Sync,
Expand Down
1 change: 1 addition & 0 deletions juniper/src/integrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Provides GraphQLType implementations for some external types

#[doc(hidden)]
pub mod serde;

Expand Down
9 changes: 6 additions & 3 deletions juniper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ pub use crate::{
model::{RootNode, SchemaType},
},
types::{
async_await::GraphQLTypeAsync,
base::{Arguments, GraphQLType, TypeKind},
async_await::{GraphQLTypeAsync, GraphQLValueAsync},
base::{Arguments, GraphQLType, GraphQLValue, TypeKind},
marker::{self, GraphQLUnion},
scalars::{EmptyMutation, EmptySubscription, ID},
subscriptions::{GraphQLSubscriptionType, SubscriptionConnection, SubscriptionCoordinator},
subscriptions::{
GraphQLSubscriptionType, GraphQLSubscriptionValue, SubscriptionConnection,
SubscriptionCoordinator,
},
},
validation::RuleError,
value::{DefaultScalarValue, Object, ParseScalarResult, ParseScalarValue, ScalarValue, Value},
Expand Down
15 changes: 11 additions & 4 deletions juniper/src/macros/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,7 @@ macro_rules! graphql_interface {
) => {
$crate::__juniper_impl_trait!(
impl<$($scalar)* $(, $lifetimes)* > GraphQLType for $name {
type Context = $ctx;
type TypeInfo = ();

fn name(_ : &Self::TypeInfo) -> Option<&str> {
fn name(_ : &Self::TypeInfo) -> Option<&'static str> {
Some($($outname)*)
}

Expand Down Expand Up @@ -178,7 +175,17 @@ macro_rules! graphql_interface {
$(.description($desciption))*
.into_meta()
}
}
);

$crate::__juniper_impl_trait!(
impl<$($scalar)* $(, $lifetimes)* > GraphQLValue for $name {
type Context = $ctx;
type TypeInfo = ();

fn type_name(&self, _ : &Self::TypeInfo) -> Option<&'static str> {
Some($($outname)*)
}

#[allow(unused_variables)]
fn resolve_field(
Expand Down
14 changes: 7 additions & 7 deletions juniper/src/macros/subscription_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use futures::Stream;

use crate::{FieldError, GraphQLType, ScalarValue};
use crate::{FieldError, GraphQLValue, ScalarValue};

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

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

impl<T, I, S> ExtractTypeFromStream<StreamItem, S> for T
where
T: futures::Stream<Item = I>,
I: GraphQLType<S>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
Expand All @@ -74,7 +74,7 @@ where
impl<Ty, T, E, S> ExtractTypeFromStream<StreamResult, S> for Ty
where
Ty: futures::Stream<Item = Result<T, E>>,
T: GraphQLType<S>,
T: GraphQLValue<S>,
S: ScalarValue,
{
type Item = T;
Expand All @@ -83,7 +83,7 @@ where
impl<T, I, E, S> ExtractTypeFromStream<ResultStreamItem, S> for Result<T, E>
where
T: futures::Stream<Item = I>,
I: GraphQLType<S>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
Expand All @@ -92,7 +92,7 @@ where
impl<T, E, I, ER, S> ExtractTypeFromStream<ResultStreamResult, S> for Result<T, E>
where
T: futures::Stream<Item = Result<I, ER>>,
I: GraphQLType<S>,
I: GraphQLValue<S>,
S: ScalarValue,
{
type Item = I;
Expand Down
Loading