Function juniper_warp::make_graphql_filter[][src]

pub fn make_graphql_filter<Query, Mutation, Context>(
    schema: RootNode<'static, Query, Mutation>,
    context_extractor: BoxedFilter<(Context,)>
) -> BoxedFilter<(Response<Vec<u8>>,)> where
    Context: Send + 'static,
    Query: GraphQLType<Context = Context, TypeInfo = ()> + Send + Sync + 'static,
    Mutation: GraphQLType<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 create a CpuPool to resolve GraphQL requests.

If you want to pass your own threadpool, use make_graphql_filter_with_thread_pool instead.

Example:

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

struct QueryRoot;

graphql_object! (QueryRoot: ExampleContext |&self| {
    field say_hello(&executor) -> String {
        let context = executor.context();

        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);