Function juniper_warp::make_graphql_filter[][src]

pub fn make_graphql_filter<Query, Mutation, Subscription, CtxT, S>(
    schema: RootNode<'static, Query, Mutation, Subscription, S>,
    context_extractor: BoxedFilter<(CtxT,)>
) -> BoxedFilter<(Response<Vec<u8>>,)> where
    Query: GraphQLTypeAsync<S, Context = CtxT> + Send + 'static,
    Query::TypeInfo: Send + Sync,
    Mutation: GraphQLTypeAsync<S, Context = CtxT> + Send + 'static,
    Mutation::TypeInfo: Send + Sync,
    Subscription: GraphQLSubscriptionType<S, Context = CtxT> + Send + 'static,
    Subscription::TypeInfo: Send + Sync,
    CtxT: Send + Sync + 'static,
    S: ScalarValue + Send + Sync + 'static, 
Expand description

Make a filter for graphql queries/mutations.

The schema argument is your juniper schema.

The context_extractor argument should be a filter that provides the GraphQL context required by the schema.

In order to avoid blocking, this helper will use the tokio_threadpool threadpool created by hyper to resolve GraphQL requests.

Example:

type UserId = String;
struct AppState(Vec<i64>);
struct ExampleContext(Arc<AppState>, UserId);

struct QueryRoot;

#[graphql_object(context = ExampleContext)]
impl QueryRoot {
    fn say_hello(context: &ExampleContext) -> String {
        format!(
            "good morning {}, the app state is {:?}",
            context.1,
            context.0
        )
    }
}

let schema = RootNode::new(QueryRoot, EmptyMutation::new(), EmptySubscription::new());

let app_state = Arc::new(AppState(vec![3, 4, 5]));
let app_state = warp::any().map(move || app_state.clone());

let context_extractor = warp::any()
    .and(warp::header::<String>("authorization"))
    .and(app_state)
    .map(|auth_header: String, app_state: Arc<AppState>| {
        let user_id = auth_header; // we believe them
        ExampleContext(app_state, user_id)
    })
    .boxed();

let graphql_filter = make_graphql_filter(schema, context_extractor);

let graphql_endpoint = warp::path("graphql")
    .and(warp::post())
    .and(graphql_filter);