Skip to content

Instantly share code, notes, and snippets.

@shrugs
Created October 21, 2019 09:26
Show Gist options
  • Save shrugs/2b14f581173cbb8dc40f7d3f6b9fc650 to your computer and use it in GitHub Desktop.
Save shrugs/2b14f581173cbb8dc40f7d3f6b9fc650 to your computer and use it in GitHub Desktop.
apollo + next

these are the relevant files from my next + apollo + prisma setup. the prisma bit isn't relevant and can just be removed from the context. likewise my resolvers + typedefs setup is hyper specific and there are infinitely many way to do this (lol javascript)

the only relevant bit that might be missing is the handler path that has to match your next.js route

// //pages/api/graphql.ts
import server from '~/api/server';
// must disable next's default body parsing since apollo-server does everything for us
// see: https://gist.github.com/mfellner/2119db3584023092d70118a8dabd146e#file-graphql-ts-L19-L23
// see: https://github.com/zeit/next.js/issues/7777
export const config = {
api: {
bodyParser: false,
},
};
// TODO: route well-known route to graphql for healthcheck handler
export default server.createHandler({ path: '/api/graphql' });
// //api/server.ts
import { Context as ApolloContext } from 'apollo-server-core';
import { ApolloServer, makeExecutableSchema } from 'apollo-server-micro';
import { applyMiddleware } from 'graphql-middleware';
import { Photon } from '@generated/photon';
import { Context } from './types';
// import permissions from './permissions';
import resolvers from './resolvers';
import typeDefs from './schema.graphql';
const photon = new Photon();
const context = async (ctx: ApolloContext): Promise<Context> => ({ ...ctx, photon });
const schema = applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }) /* , permissions */);
export default new ApolloServer({
schema,
introspection: true,
playground: true,
context,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment