Apollo Server Express config with nextjs and Nexus

Viewed 418

I'm trying to follow this tutorial, but substituting apollo-server-micro with apollo-server-express.

When I try to configure the server, the tutorial docs provide the following approach:

import { ApolloServer } from 'apollo-server-micro';

// we'll create these in a second!
import { schema } from '../../graphql/schema';
import { createContext } from './../../graphql/context';

const apolloServer = new ApolloServer({
  context: createContext,
  schema,
  tracing: process.env.NODE_ENV === 'development'
});

export const config = {
  api: {
    bodyParser: false
  }
};

export default apolloServer.createHandler({
  path: '/api/graphql'
});

The apollo docs use the following approach to configure the server:

import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import express from 'express';
import http from 'http';

async function startApolloServer(typeDefs, resolvers) {
  const app = express();
  const httpServer = http.createServer(app);
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
  });
  await server.start();
  server.applyMiddleware({ app });
  await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
  console.log(` Server ready at http://localhost:4000${server.graphqlPath}`);
}

When I try to merge them, I've made a mess. I can't find a way to include the bodyParser (I am struggling to make sense of this documentation), the schema, the context or the path.

import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import express from 'express';
import http from 'http';

import { schema } from '../../graphql/schema';
import { createContext } from './../../graphql/context';

async function startApolloServer(typeDefs, resolvers) {
    const app = express();
    const httpServer = http.createServer(app);
    const apolloServer = new ApolloServer({
    typeDefs,
    resolvers,
    plugins: [ApolloServerPluginDrainHttpServer({httpServer})],
    context: createContext,
    schema,
    tracing: process.env.NODE_ENV === 'development'
});

await apolloServer.start();
apolloServer.applyMiddleware({ app });
await new Promise<void>(resolve => httpServer.listen({ port: 4000 }, resolve));
console.log(`that worked server started ${apolloServer.graphqlPath} `)

// export const config = { 
//     api: {
//         bodyParser: false
//     }
// };

// export default apolloServer.createHandler({
//     path: 'api/graphql'
// });

Is there a way to use express middleware with Nexus/Next.js or am I trying to merge conflicting ideas by switching to apollo-server-express?

0 Answers
Related