Integration with Next.js
Next.js is a web framework that allows you to build websites very quickly and GraphQL Yoga can be integrated with Next.js easily as an API Route.
Installation
yarn add graphql
yarn add graphql-yoga@three
Example
pages/api/graphql.ts
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import { createYoga } from 'graphql-yoga'
import type { NextApiRequest, NextApiResponse } from 'next'
export const config = {
api: {
// Disable body parsing (required for file uploads)
bodyParser: false
}
}
export default createYoga<{
req: NextApiRequest
res: NextApiResponse
}>({
// Needed to be defined explicitly because our endpoint lives at a different path other than `/graphql`
graphqlEndpoint: '/api/graphql'
})
You can also check a full example on our GitHub repository here
Last updated on October 6, 2022