A Next.js 12 integration of GraphQL Server.

Overview

This is a Next.js 12 integration of GraphQL Server. It is early work and has not been extensively tested in production.

It is based on samples provided by @glasser and the official apollo-server-lambda integration.

Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. Read the docs. Read the CHANGELOG.

Getting Started

This section assumes that you already have a running Next.js project.

npm install apollo-server-nextjs graphql

Create a file named pages/api/graphql.js, place the following code:

"Hello world!", }, }; const server = new ApolloServer({ typeDefs, resolvers, // By default, the GraphQL Playground interface and GraphQL introspection // is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`). // // If you'd like to have GraphQL Playground and introspection enabled in production, // install the Playground plugin and set the `introspection` option explicitly to `true`. introspection: true, plugins: [ApolloServerPluginLandingPageGraphQLPlayground()], }); export default server.createHandler(); // Disable Next.js body parsing so that Apollo Server can access it entirely export const config = { api: { bodyParser: false, }, };">
import { ApolloServer, gql } from "apollo-server-nextjs";
import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,

  // By default, the GraphQL Playground interface and GraphQL introspection
  // is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`).
  //
  // If you'd like to have GraphQL Playground and introspection enabled in production,
  // install the Playground plugin and set the `introspection` option explicitly to `true`.
  introspection: true,
  plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});

export default server.createHandler();

// Disable Next.js body parsing so that Apollo Server can access it entirely
export const config = {
  api: {
    bodyParser: false,
  },
};

Then open http://localhost:3000/api/graphql

Modifying the Response (Enable CORS)

To enable CORS the response HTTP headers need to be modified. To accomplish this use the cors option.

"Hello world!", }, }; const server = new ApolloServer({ typeDefs, resolvers, }); export default server.createHandler({ expressGetMiddlewareOptions: { cors: { origin: "*", credentials: true, }, }, });">
import { ApolloServer, gql } from "apollo-server-lambda";

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

export default server.createHandler({
  expressGetMiddlewareOptions: {
    cors: {
      origin: "*",
      credentials: true,
    },
  },
});

To enable CORS response for requests with credentials (cookies, http authentication) the allow origin header must equal the request origin and the allow credential header must be set to true.

"Hello world!", }, }; const server = new ApolloServer({ typeDefs, resolvers, }); export default server.createHandler({ expressGetMiddlewareOptions: { cors: { origin: true, credentials: true, }, }, });">
import { ApolloServer, gql } from "apollo-server-lambda";

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => "Hello world!",
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

export default server.createHandler({
  expressGetMiddlewareOptions: {
    cors: {
      origin: true,
      credentials: true,
    },
  },
});

Cors Options

The options correspond to the express cors configuration with the following fields(all are optional):

  • origin: boolean | string | string[]
  • methods: string | string[]
  • allowedHeaders: string | string[]
  • exposedHeaders: string | string[]
  • credentials: boolean
  • maxAge: number

Principles

GraphQL Server is built with the following principles in mind:

  • By the community, for the community: GraphQL Server's development is driven by the needs of developers
  • Simplicity: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
  • Performance: GraphQL Server is well-tested and production-ready - no modifications needed

Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!

You might also like...

Crud with GraphQL + Express + Json Server

How to run the project ### Install dependecies $ yarn install ### Run GraphQL server $ yarn dev ### Run Json Server $ yarn run json:server ### The

Jun 22, 2022

Blog-sercannaya - react-next-sass-graphql

Next.js + Tailwind CSS Example This example shows how to use Tailwind CSS (v3.0) with Next.js. It follows the steps outlined in the official Tailwind

Jan 3, 2022

👜 Next-gen e-commerce built using Remix, Chakra UI, GraphQL and web3

👜 Future Store About Here at New Store, we have the best in women's, men's, bags, shoes, accessories and more. Unmissable discounts and installments.

Dec 26, 2022

Decentralized Twitter prototype built with Polygon, GraphQL, Next.js, Ceramic, Arweave, and Bundlr

Decentralized Twitter prototype built with Polygon, GraphQL, Next.js, Ceramic, Arweave, and Bundlr

Titter - Web3 Social chat beta as fuck 🛠 Built with Next.js, Arweave, Bundlr, Ceramic, GraphQL, & Polygon How it works This is a working prototype of

Dec 14, 2022

next-gql-dogs code for the NextJS + GraphQL + Typescript Blueprint video

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Nov 26, 2022

✨ My portfolio built with TypeScript, Next.js, Stitches and GraphQL.

Technologies | How to Use | Cloning or Forking 🚀 Technologies This project was developed with the following technologies: TypeScript Next.js GraphQL

Nov 10, 2022

Example-browserstack-reporting - This repository contains an example of running Selenium tests and reporting BrowserStack test results, including full CI pipeline integration.

BrowserStack reporting and Selenium test result example This repository contains an example of running Selenium tests and reporting BrowserStack test

Jan 1, 2022

Runs various integration tests for the composable picasso parachain.

Picasso Integration Tester Picasso Integration Tester is a collection of different implementation tests for the Picasso Polkadot Parachain. Installati

Jan 11, 2022

Playstation integration for Homebridge / HOOBS.

Playstation integration for Homebridge / HOOBS.

Homebridge Playstation Playstation integration for Homebridge / HOOBS. Hey Siri, turn on Playstation finally possible! This integration exposes a Swit

Jan 1, 2023
Comments
  • Apollo Server 4 / apollo-server-integrations community

    Apollo Server 4 / apollo-server-integrations community

    Hi @apuyou, your work on integrating next with Apollo Server is much appreciated! As a part of our v4 work, we put together a GH org and NPM scope for community integrations. If you'd like to join us over there you're more than welcome to. There is of course no expectation to do so, but I just wanted to extend the invite in case that's interesting to you. Cheers!

    https://github.com/apollo-server-integrations

    opened by trevor-scheer 2
  • how about graphql-upload

    how about graphql-upload

    Hi Thanks for this example. I would be curious to see if you could add upload functionality to the apollo-server-express implementation for nextjs 12.

    Thx

    enhancement 
    opened by tsvetann 1
Releases(v3.10.3)
  • v3.10.3(Oct 19, 2022)

    This release updates the dependency on apollo-server-core and apollo-server-express from 3.6.X to 3.10.X. Please refer to these packages for the appropriate release notes.

    This includes security fixes and may require changes on your side. Most notably, you should read about:

    This will be the last release for Apollo Server 3.

    The 4.0.0 release of this package will only be compatible with Apollo Server 4.

    Source code(tar.gz)
    Source code(zip)
Owner
Arthur Puyou
Arthur Puyou
Connect Web Integration illustrates the integration of Connect-Web in various JS frameworks and tooling

Connect Web Integration Connect Web Integration is a repository of example projects using Connect-Web with various JS frameworks and tooling. It provi

Buf 43 Dec 29, 2022
NextJS with GraphQL using type-graphl and graphql-codegen adoptable dogs example code

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Jack Herrington 2 Mar 31, 2022
GraphQL-first boilerplate that scales with TypeScript + Node Express + Apollo GraphQL APIs.

graphql-typescript-boilerplate A boilerplate project for quickly building Graphql APIs and with typescript ?? Installation Install the dependencies: y

Youssef Hajjari 6 May 15, 2022
GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schemas (Federation, Stitching)

GraphQL Hive GraphQL Hive provides all the tools the get visibility of your GraphQL architecture at all stages, from standalone APIs to composed schem

Kamil Kisiela 184 Dec 21, 2022
Swetrix Tracking integration for Next.js

Swetrix Next.js integration Official Swetrix Analytics integration for Next.js. Integration Install Run the following command to install in your proje

Swetrix 13 Sep 9, 2022
A developer directory built on Next.js and MongoDB Atlas, deployed on Vercel with the Vercel + MongoDB integration.

MongoDB Starter – Developer Directory A developer directory built on Next.js and MongoDB Atlas, deployed on Vercel with the Vercel + MongoDB integrati

Vercel 246 Dec 20, 2022
Decentralized Social Media. Built using Next.js. Web3 integration with Moralis, Metamask and Ethers.js. Also uses Lens Protofcol to get the profile data.

DecentraGram Decentralized Social Media. Built using Next.js. Web3 integration with Moralis, Metamask and Ethers.js. Also uses Lens Protofcol to get t

Didier Peran Ganthier 8 Dec 20, 2022
Monolithic repo for api server, image server, web server

Onsecondary Market Deployed at https://market.onsecondary.com Monolithic repo for api server, image server, web server TODO -use a script to cull expi

Admazzola 2 Jan 11, 2022
🛸 Socfony GraphQL server

Socfony is an open source social application, and basic security modules and social function modules have been preset in the software. Whether it is t

Socfony 8 Nov 2, 2022
Create a maintainable and scalable Node.js GraphQL API with TypeScript, Express, Mongoose and Apollo Server.

Set up and build a Node.js GraphQL API using Typescript, Express, Mongoose with a maintainable and scalable structure

Adam Khomsi 7 Nov 4, 2022