1

I'm using apollo-server-koa and created a simple Koa app + ApolloServer but when I want to access ctx.cookies in the GraphQL resolvers, it's always undefined. Only request and response are there.

const app = new Koa()
const server = new ApolloServer({
  schema,
  context: ({ ctx }) => ctx
})

server.applyMiddleware({
  app,
  cors: {
    origin: 'http://localhost:3000',
    credentials: true,
  }
})

It confuses me because when I do app.use((ctx) => ...), cookies is available. Is this correct and do I need a separate cookie middleware like cookie-parser in express?

Marvin
  • 41
  • 4

1 Answers1

1

Unlike express, the cookie functionality for koa is in a separate package 'koa-cookie'.

If you do:

import cookie from 'koa-cookie';
const app = new Koa();
app.use(cookie());

You should be able to set and get cookies through ctx.cookies.set and ctx.cookies.get.

Flair
  • 2,609
  • 1
  • 29
  • 41
Nodiril
  • 21
  • 3
  • This package seems greatly out of date and unmaintained. Is there any alternative? Seems like https://github.com/pillarjs/cookies would be a better alternative, while not so intuitive? – JakobAttk Jul 14 '22 at 21:54