apollo-server/docs/source/migration.md
Jesse Rosenberger 3a0608ebc5
Tick off Prettier checkmarks, which were present on CI but not locally. (#880)
For reasons unbeknownst to me, the changes below were only being complained
about in the CI environment but _not_ when I ran `npm run lint` locally.

It seems partially related to the OS its ran on, so I imagine there might be
some other sub-dependency at play here.  In an effort to fix this, I just
spawned a Ubuntu docker image, checked out this repository and ran the same
`npm run lint`.  This produced identical results to Travis, so I ran
`npm run lint-fix`, then saved the `git diff` results and have applied this
locally.

This should allow us to re-enable `prettier` in CI, though I have plans to
separate that from the actual `npm test` runs.  This should result in a better
workflow for managing PRs.
2018-03-16 10:25:07 +02:00

3.2 KiB

title description
Migrating to v0.2 How to migrate from an older version of Apollo Server

Version 0.2.0 of Apollo Server contains several breaking changes in the API. The most notable changes are:

  • the apolloServer function no longer exists and was replaced with apolloExpress.
  • apolloExpress no longer accepts shorthand type definitions
  • apolloExpress doesn't have the resolvers, mocks and connectors options.
  • apolloExpress doesn't include GraphiQL any more
  • context: if you use connectors in your schema, don't forget to setup default context to at least an empty object, it can't be undefined in this case
  • Apollo Server no longer accepts GET requests or parameters in the URL
  • apolloExpress no longer parses the HTTP body automatically

In order to make updating from an older version of Apollo Server easier, this guide shows how to use graphql-tools together with apolloExpress and graphiqlExpress to replace the old apolloServer function.

The three main differences between the old and the new approach are:

  1. generating the schema is now done with graphql-tools, Apollo Server only uses the finished schema.
  2. bodyParser has to be used to parse requests before passing them to expressApollo
  3. GraphiQL now has to be served on a separate path

The following code snippet in Apollo Server 0.1.x

import express from 'express';
import { apolloServer } from 'apollo-server';
import Schema from './data/schema';
import Mocks from './data/mocks';
import Resolvers from './data/resolvers';
import Connectors from './data/connectors';

const GRAPHQL_PORT = 8080;

const graphQLServer = express();

graphQLServer.use(
  '/graphql',
  apolloServer({
    graphiql: true,
    schema: Schema,
    resolvers: Resolvers,
    connectors: Connectors,
    mocks: Mocks,
  }),
);

graphQLServer.listen(GRAPHQL_PORT, () =>
  console.log(
    `Apollo Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`,
  ),
);

... should be written as follows in Apollo Server 0.2.x and above:

import express from 'express';

import Schema from './data/schema';
import Mocks from './data/mocks';
import Resolvers from './data/resolvers';
import Connectors from './data/connectors';

// NEW or changed imports:
import { apolloExpress, graphiqlExpress } from 'apollo-server';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import bodyParser from 'body-parser';

const GRAPHQL_PORT = 8080;

const graphQLServer = express();

const executableSchema = makeExecutableSchema({
  typeDefs: Schema,
  resolvers: Resolvers,
  connectors: Connectors,
});

addMockFunctionsToSchema({
  schema: executableSchema,
  mocks: Mocks,
  preserveResolvers: true,
});

// `context` must be an object and can't be undefined when using connectors
graphQLServer.use(
  '/graphql',
  bodyParser.json(),
  apolloExpress({
    schema: executableSchema,
    context: {}, //at least(!) an empty object
  }),
);

graphQLServer.use(
  '/graphiql',
  graphiqlExpress({
    endpointURL: '/graphql',
  }),
);

graphQLServer.listen(GRAPHQL_PORT, () =>
  console.log(
    `Apollo Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`,
  ),
);