graphql yoga access http headers code example

Example: graphql yoga access http headers

import { GraphQLServer } from 'graphql-yoga'
// ... or using `require()`
// const { GraphQLServer } = require('graphql-yoga')

const typeDefs = `
  type Query {
    showHeader(headerName: String): String!
  }
`

const resolvers = {
  Query: {
    showHeader: (_, { headerName }, ctx) => ctx.request.get(headerName),
  },
}

const server = new GraphQLServer({ typeDefs, resolvers, context: req => ({ ...req }) })
server.start(() => console.log('Server is running on localhost:4000'))

Tags:

Misc Example