mirror of
https://github.com/vale981/apollo-server
synced 2025-03-06 10:11:40 -05:00

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.
3.2 KiB
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 withapolloExpress
. apolloExpress
no longer accepts shorthand type definitionsapolloExpress
doesn't have theresolvers
,mocks
andconnectors
options.apolloExpress
doesn't include GraphiQL any morecontext
: if you use connectors in your schema, don't forget to setup defaultcontext
to at least an empty object, it can't beundefined
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:
- generating the schema is now done with
graphql-tools
, Apollo Server only uses the finished schema. bodyParser
has to be used to parse requests before passing them toexpressApollo
- 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`,
),
);