1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! GraphQL executors.
//!
//! In order to choose the strategy for executing GraphQL queries,
//! the following GraphQL executors are provided:
//!
//! * [`Nonblocking`]
//!   It spawns the task for executing GraphQL queries by using the Tokio's
//!   default executor.  It also notify the start of the blocking section to
//!   the runtime by using the blocking API provided by `tokio_threadpool`.
//!
//! * [`CurrentThread`]
//!   It executes the GraphQL queries on the current thread.
//!
//! * [`WithSpawner`]
//!   It spawn the task for executing GraphQL queries by using the specified
//!   task executor. Unlike to `Nonblocking`, it does not notify the start of
//!   blocking section to the runtime.
//!
//! [`Nonblocking`]: ./struct.Nonblocking.html
//! [`CurrentThread`]: ./struct.CurrentThread.html
//! [`WithSpawner`]: ./struct.WithSpawner.html

mod current_thread;
mod nonblocking;
mod with_spawner;

pub use self::current_thread::{current_thread, CurrentThread};
pub use self::nonblocking::{nonblocking, Nonblocking};
pub use self::with_spawner::{with_spawner, WithSpawner};

pub use self::schema::Schema;
pub use self::shared::SharedSchema;

// ====

mod schema {
    use juniper::{GraphQLType, RootNode};
    use std::rc::Rc;
    use std::sync::Arc;

    /// Trait representing a GraphQL schema.
    pub trait Schema: SchemaImpl {}

    impl<QueryT, MutationT, CtxT> Schema for RootNode<'static, QueryT, MutationT>
    where
        QueryT: GraphQLType<Context = CtxT>,
        MutationT: GraphQLType<Context = CtxT>,
    {}
    impl<S: Schema> Schema for Box<S> {}
    impl<S: Schema> Schema for Rc<S> {}
    impl<S: Schema> Schema for Arc<S> {}

    pub trait SchemaImpl {
        type Context;
        type Query: GraphQLType<Context = Self::Context>;
        type Mutation: GraphQLType<Context = Self::Context>;
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation>;
    }

    impl<QueryT, MutationT, CtxT> SchemaImpl for RootNode<'static, QueryT, MutationT>
    where
        QueryT: GraphQLType<Context = CtxT>,
        MutationT: GraphQLType<Context = CtxT>,
    {
        type Context = CtxT;
        type Query = QueryT;
        type Mutation = MutationT;

        #[inline]
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation> {
            self
        }
    }

    impl<T: Schema> SchemaImpl for Box<T> {
        type Context = T::Context;
        type Query = T::Query;
        type Mutation = T::Mutation;

        #[inline]
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation> {
            (**self).as_root_node()
        }
    }

    impl<T: Schema> SchemaImpl for Rc<T> {
        type Context = T::Context;
        type Query = T::Query;
        type Mutation = T::Mutation;

        #[inline]
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation> {
            (**self).as_root_node()
        }
    }

    impl<T: Schema> SchemaImpl for Arc<T> {
        type Context = T::Context;
        type Query = T::Query;
        type Mutation = T::Mutation;

        #[inline]
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation> {
            (**self).as_root_node()
        }
    }
}

mod shared {
    use super::Schema;
    use juniper::{GraphQLType, RootNode};

    /// A helper trait for representing a `Schema` which can be shared between threads.
    #[allow(missing_docs)]
    pub trait SharedSchema: SharedSchemaImpl {}

    impl<S> SharedSchema for S
    where
        S: Schema + Send + Sync + 'static,
        S::Context: Send + 'static,
        S::Query: Send + Sync + 'static,
        S::Mutation: Send + Sync + 'static,
        <S::Query as GraphQLType>::TypeInfo: Send + Sync + 'static,
        <S::Mutation as GraphQLType>::TypeInfo: Send + Sync + 'static,
    {}

    pub trait SharedSchemaImpl: Send + Sync + 'static {
        type Context: Send + 'static;
        type QueryTypeInfo: Send + Sync + 'static;
        type MutationTypeInfo: Send + Sync + 'static;
        type Query: GraphQLType<Context = Self::Context, TypeInfo = Self::QueryTypeInfo>
            + Send
            + Sync
            + 'static;
        type Mutation: GraphQLType<Context = Self::Context, TypeInfo = Self::MutationTypeInfo>
            + Send
            + Sync
            + 'static;
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation>;
    }

    impl<S> SharedSchemaImpl for S
    where
        S: Schema + Send + Sync + 'static,
        S::Context: Send + 'static,
        S::Query: Send + Sync + 'static,
        S::Mutation: Send + Sync + 'static,
        <S::Query as GraphQLType>::TypeInfo: Send + Sync + 'static,
        <S::Mutation as GraphQLType>::TypeInfo: Send + Sync + 'static,
    {
        type Context = S::Context;
        type Query = S::Query;
        type Mutation = S::Mutation;
        type QueryTypeInfo = <S::Query as GraphQLType>::TypeInfo;
        type MutationTypeInfo = <S::Mutation as GraphQLType>::TypeInfo;

        #[inline]
        fn as_root_node(&self) -> &RootNode<'static, Self::Query, Self::Mutation> {
            self.as_root_node()
        }
    }
}