graphql-community / koa-graphql

Create a GraphQL HTTP server with Koa.
MIT License
843 stars 61 forks source link

Session value example #10

Closed stanonyime closed 8 years ago

stanonyime commented 8 years ago

In the express-graphql repo, there is an example in the Readme demonstrating session usage. How will that look like with koa-graphql? Basically, I want to access session variables and use it in my schema

santialbo commented 8 years ago

this library needs to expose the koa context on the options function, not just the original node request.

chentsulin commented 8 years ago

It's undocumented, but this lib already expose the koa context on the options function now. https://github.com/chentsulin/koa-graphql/blob/master/src/index.js#L188

chentsulin commented 8 years ago

Example added. See https://github.com/chentsulin/koa-graphql/pull/22/files

prewk commented 8 years ago

Have I understood correctly that this:

app.use(mount('/api', graphqlHTTP((request, context) => ({
    schema: Schema,
    context: { foo: 'bar' },
}))));

...should make argument 3 on my schema's resolve equal { foo: 'bar' }? I currently get a schema object there instead, what am I missing? (Some fundamental fact about the graphql resolve method's "context" I assume)

{
  fieldName: 'viewer',
    fieldASTs: [ ... ],
    returnType: ....,
    parentType: ....,
    schema: ...,
    ...etc...
}

Thanks!

chentsulin commented 8 years ago

context only be supported after v0.5.0. You may use the old version of koa-graphql (< 0.5) and graphql (< 0.5). Here is the resolve's 3rd argument example before 0.5.0:

new GraphQLObjectType({
  name: 'MyType',
  fields: {
    myField: {
      type: GraphQLString,
      resolve(parentValue, _, { rootValue }) {
        //
      }
    }
  }
});
prewk commented 8 years ago

Ah, that explains it! I used the old version due to some conflicting peerDeps, but I'll sort em out and try the newer version. Thanks.

prewk commented 8 years ago

..since I'm still deep in peerDep problems (I think it's because isomorphic-relay hasn't upgraded yet), did I understand you correctly that there is no sensible way of getting the context data in 0.4?

chentsulin commented 8 years ago

@prewk You can achieve your goal using rootValue in 0.4. Please checkout old README here: https://github.com/chentsulin/koa-graphql/tree/v0.4.13#advanced-options

Blackening999 commented 6 years ago

@chentsulin can't get it working following your example here https://github.com/chentsulin/koa-graphql/pull/22/files

First of all, routeValue is undefined in the resolve's context argument. If I try to store in session it doesn't store in the next session middleware call :\

// inside mutation

  mutateAndGetPayload: async ({ email, password }, context) => {
    console.log(`session: ${context.session}`)
    console.log(`mutateAndGetPayload`)

    const user = await getUserWithCredentials(email, password)

    // set session token so that user is authenticated on next requests via a cookie
    if (user) {
      /* eslint-disable no-param-reassign */
      context.session.token = createToken(user)
      console.log(`context.session.token: ${context.session.token}`)
      /* eslint-enable no-param-reassign */
    }
    return { user }
  },

// inside session middleware

export default async function sessionMiddleware(ctx, next) {
  console.log(`sessionMiddleware`)
  console.log(`ctx.session.token: ${ctx.session.token}`) // <<<<< UNDEFINED :(
}
Blackening999 commented 6 years ago

More info here: https://github.com/Secbone/koa-session2/issues/46