Apollo-server: how to get ctx from graphqlKoa in resolve

Created on 22 Jan 2018  路  3Comments  路  Source: apollographql/apollo-server

import { graphqlKoa, graphiqlKoa } from 'apollo-server-koa';
router.post('/graphql', koaBody(), graphqlKoa({ schema: schemas.UserSchema, formatError }));

i want to get ctx using following method:

async resolve (root, params, options, ctx) {}

how can i do that?

Most helpful comment

I have solved this fucking problem with a fucking strange method:

router.post('/graphql', koaBody(), (ctx, next) => graphqlKoa({ schema: schemas.EmailSchema, formatError, context: ctx })(ctx, next));

Then I can use ctx in fn resolve:

async resolve (root, params, ctx) {
// use ctx
}

All 3 comments

Is resolve a resolver inside your schema?
I asume that you are using graphql-tools, therefore https://www.apollographql.com/docs/graphql-tools/resolvers.html#Resolver-function-signature should be relevant to you.

I have solved this fucking problem with a fucking strange method:

router.post('/graphql', koaBody(), (ctx, next) => graphqlKoa({ schema: schemas.EmailSchema, formatError, context: ctx })(ctx, next));

Then I can use ctx in fn resolve:

async resolve (root, params, ctx) {
// use ctx
}

Oh, I did not know you mean the koa context object 馃槄
Glad you figured it out!

Was this page helpful?
0 / 5 - 0 ratings