[][src]Function juniper_warp::make_graphql_filter

pub fn make_graphql_filter<Query, Mutation, Context, S>(
    schema: RootNode<'static, Query, Mutation, S>,
    context_extractor: BoxedFilter<(Context,)>
) -> BoxedFilter<(Response<Vec<u8>>,)> where
    S: ScalarValue + Send + Sync + 'static,
    &'b S: ScalarRefValue<'b>,
    Context: Send + 'static,
    Query: GraphQLType<S, Context = Context, TypeInfo = ()> + Send + Sync + 'static,
    Mutation: GraphQLType<S, Context = Context, TypeInfo = ()> + Send + Sync + 'static, 

Make a filter for graphql endpoint.

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;

#[juniper::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());

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::post2())
    .and(graphql_filter);