`ctx.cookies` is always undefined in apollo-server-koa

493 Views Asked by At

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?

1

There are 1 best solutions below

1
Nodiril On

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.