-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update most Apollo Server examples to Apollo Server 3
Previously, most of the examples used Apollo Server 2. As a maintainer of Apollo Server, we frequently found that users would start with one of these examples, try to upgrade to Apollo Server 3, and get confused when it didn't work. Apollo Server 3 has a more explicit lifecycle that requires awaiting a `start` method on startup, and users often put this in the wrong place. The `api-routes-graphql` package did use Apollo Server 3 and did call `start`, but only awaits it when the first request comes in rather than at the top level. Additionally, all the examples use `apollo-server-micro`. While this package is technically a maintained part of the Apollo Server project, it is not as fully featured or battle-tested as the most popular package, `apollo-server-express`. For example, it doesn't have the same default CORS behavior has the other Apollo Server framework integrations, and doesn't have a way to drain servers gracefully on shutdown. (Future Apollo Server features may target `apollo-server-express` before other integrations as well.) Because Next.js can easily use Express middleware with just a few lines of integration code (documented at https://nextjs.org/docs/api-routes/api-middlewares#connectexpress-middleware-support), Next.js users would be best served by using the most mainstream ApolloServer implementation rather than the least maintained one. So this PR: - Changes all examples from using `apollo-server-micro` v2 to `apollo-server-express` v3 - Uses top level await (enabled in next.config.js) for proper startup handling - Upgrades other packages like `graphql` and `@graphql-tools/schema`, and installs them where relevant - Removes special CORS handling from `examples/api-routes-graphql` because `apollo-server-express` has better CORS defaults. (If the default is not good enough for this example, pass a `cors` option to `getMiddleware` instead of doing CORS manually. The value of the `cors` option is equivalent to the argument to the npm `cors` package's middleware.) This leaves the `with-apollo-neo4j-graphql` example alone, as I could not get it to work properly before or after upgrading Apollo Server.
- Loading branch information
Showing
20 changed files
with
164 additions
and
51 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
examples/api-routes-apollo-server-and-client-auth/apollo/resolvers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
examples/api-routes-apollo-server-and-client-auth/apollo/schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
examples/api-routes-apollo-server-and-client-auth/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
webpack: (config, options) => { | ||
config.experiments = { | ||
topLevelAwait: true, | ||
} | ||
return config | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
examples/api-routes-apollo-server-and-client-auth/pages/api/graphql.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
import { ApolloServer } from 'apollo-server-micro' | ||
import { ApolloServer } from 'apollo-server-express' | ||
import { schema } from '../../apollo/schema' | ||
|
||
function runMiddleware(req, res, fn) { | ||
return new Promise((resolve, reject) => { | ||
fn(req, res, (result) => { | ||
if (result instanceof Error) { | ||
return reject(result) | ||
} | ||
|
||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
|
||
const apolloServer = new ApolloServer({ | ||
schema, | ||
context(ctx) { | ||
return ctx | ||
}, | ||
}) | ||
await apolloServer.start() | ||
const apolloMiddleware = apolloServer.getMiddleware({ | ||
path: '/api/graphql', | ||
}) | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
} | ||
|
||
export default apolloServer.createHandler({ path: '/api/graphql' }) | ||
export default async function handler(req, res) { | ||
await runMiddleware(req, res, apolloMiddleware) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
webpack: (config, options) => { | ||
config.experiments = { | ||
topLevelAwait: true, | ||
} | ||
return config | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
examples/api-routes-apollo-server-and-client/pages/api/graphql.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import { ApolloServer } from 'apollo-server-micro' | ||
import { ApolloServer } from 'apollo-server-express' | ||
import { schema } from '../../apollo/schema' | ||
|
||
function runMiddleware(req, res, fn) { | ||
return new Promise((resolve, reject) => { | ||
fn(req, res, (result) => { | ||
if (result instanceof Error) { | ||
return reject(result) | ||
} | ||
|
||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
|
||
const apolloServer = new ApolloServer({ schema }) | ||
await apolloServer.start() | ||
const apolloMiddleware = apolloServer.getMiddleware({ | ||
path: '/api/graphql', | ||
}) | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
} | ||
|
||
export default apolloServer.createHandler({ path: '/api/graphql' }) | ||
export default async function handler(req, res) { | ||
await runMiddleware(req, res, apolloMiddleware) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
webpack: (config, options) => { | ||
config.experiments = { | ||
topLevelAwait: true, | ||
} | ||
return config | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
webpack: (config, options) => { | ||
config.experiments = { | ||
topLevelAwait: true, | ||
} | ||
return config | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,10 @@ module.exports = { | |
use: 'yaml-loader', | ||
}) | ||
|
||
config.experiments = { | ||
topLevelAwait: true, | ||
} | ||
|
||
return config | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import { ApolloServer } from 'apollo-server-micro' | ||
import { ApolloServer } from 'apollo-server-express' | ||
import { schema } from '../../lib/schema' | ||
|
||
function runMiddleware(req, res, fn) { | ||
return new Promise((resolve, reject) => { | ||
fn(req, res, (result) => { | ||
if (result instanceof Error) { | ||
return reject(result) | ||
} | ||
|
||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
|
||
const apolloServer = new ApolloServer({ schema }) | ||
await apolloServer.start() | ||
const apolloMiddleware = apolloServer.getMiddleware({ | ||
path: '/api/graphql', | ||
}) | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
} | ||
|
||
export default apolloServer.createHandler({ path: '/api/graphql' }) | ||
export default async function handler(req, res) { | ||
await runMiddleware(req, res, apolloMiddleware) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters