Docs / Features / Cookies Cookies GraphQL Yoga v3 documentation (superseded by v5): Cookies
v5latest v4 v3 v2
This is the documentation for the old GraphQL Yoga v3 We recommend upgrading to the latest GraphQL Yoga v5.
Migrate to GraphQL Yoga v5 .
An HTTP cookie is a small piece of data that holds an information to let the server recognize the
client. You can learn more about the cookies
here .
There is a whatwg-node plugin to handle cookies using the web standard
CookieStore . So it allows you to get
cookies from the client’s request and set cookies back to the client in the response
You can learn more about the
CookieStore API here in the MDN documentation .
Installation
npm yarn pnpm bun
npm i @whatwg-node/server-plugin-cookies yarn add @whatwg-node/server-plugin-cookies pnpm add @whatwg-node/server-plugin-cookies bun add @whatwg-node/server-plugin-cookies
Usage
import { createSchema, createYoga } from 'graphql-yoga'
import { useCookies } from '@whatwg-node/server-plugin-cookies'
const yoga = createYoga ({
schema: createSchema ({
typeDefs: /* GraphQL */ `
type Query {
cookie(name: String): String
}
type Mutation {
setCookie(name: String, value: String): String
}
` ,
resolvers: {
Query: {
async cookie ( root , args , ctx : YogaInitialContext ) {
const cookie = await ctx.request.cookieStore?. get (args.name)
return cookie?.value
}
},
Mutation: {
async setCookie ( root , args , ctx : YogaInitialContext ) {
await ctx.request.cookieStore?. set (args.name, args.value)
return args.value
}
}
}
}),
plugins: [ useCookies ()]
})
const server = createServer (yoga)
server. listen ( 4000 , () => {
console. log ( 'Server is running on http://localhost:4000' )
}) ← REST API Apollo Federation →