Skip to content

Commit 874957e

Browse files
charlypolyijjk
andauthored
feat(examples): refactor api-routes-graphql to GraphQL Yoga (#36155)
GraphQL Yoga provides a more extensible experience ([Envelop plugins](https://envelop.dev/)) and lighter bundle than the previously showcased `apollo-server-micro` ([64.5kB](https://bundlephobia.com/package/@graphql-yoga/node@2.2.1) vs [196.2kB](https://bundlephobia.com/package/apollo-server-micro@3.6.7) - min & GZiped) Similar to Next.js, GraphQL Yoga comes with strong defaults and out-of-the-box support for modern GraphQL features (File uploads, subscriptions, and more). Strong of many years of existence and a large community, GraphQL Yoga 2.0 comes with Next.js support with no additional package or configuration 📦 Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
1 parent cf0158c commit 874957e

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

examples/api-routes-graphql/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
# API routes with GraphQL server
22

3-
Next.js ships with [API routes](https://nextjs.org/docs/api-routes/introduction), which provide an easy solution to build your own `API`. This example shows their usage alongside [apollo-server-micro](https://github.com/apollographql/apollo-server/tree/main/packages/apollo-server-micro) to provide simple GraphQL server consumed by Next.js app.
3+
Next.js ships with [API routes](https://nextjs.org/docs/api-routes/introduction), which provide an easy solution to build your own `API`.
4+
This example showcases how to build a lightweight and blazing fast GraphQL API with minimum configuration using GraphQL Yoga.
5+
6+
GraphQL Yoga comes with strong defaults:
7+
8+
- CORS is enabled by default
9+
- Automatically masking unexpected errors and preventing sensitive information from leaking to clients.
10+
- Shipped with GraphiQL
11+
12+
Yoga also brings support (with no additional dependency) for subscriptions, file uploads, and your favorite schema-building library (GraphQL Tools, Pothos, Nexus, TypeGraphQL, SDL first schema-design approaches, graphql-js, Apollo Tools).
13+
14+
More information on all available features are available [on the official documentation](https://www.graphql-yoga.com/docs/quick-start).
15+
16+
Finally, GraphQL Yoga is built on top of Envelop. Envelop is a library that helps build GraphQL API faster and flexibly with plugin-based architecture.
17+
18+
Similar to Express middlewares allowing you to customize requests' behavior, Envelop applies the same idea to GraphQL requests.
19+
20+
More information on [Envelop documentation](https://www.envelop.dev/).
421

522
## Deploy your own
623

examples/api-routes-graphql/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
"start": "next start"
77
},
88
"dependencies": {
9-
"apollo-server-micro": "^3.0.1",
10-
"graphql": "^15.5.1",
11-
"micro": "^9.3.4",
9+
"@graphql-yoga/node": "^2.2.1",
10+
"graphql": "^16.3.0",
1211
"next": "latest",
1312
"react": "^17.0.2",
1413
"react-dom": "^17.0.2",
Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ApolloServer, gql } from 'apollo-server-micro'
1+
import { createServer } from '@graphql-yoga/node'
22

3-
const typeDefs = gql`
3+
const typeDefs = /* GraphQL */ `
44
type Query {
55
users: [User!]!
66
}
@@ -17,33 +17,13 @@ const resolvers = {
1717
},
1818
}
1919

20-
const apolloServer = new ApolloServer({ typeDefs, resolvers })
21-
22-
const startServer = apolloServer.start()
23-
24-
export default async function handler(req, res) {
25-
res.setHeader('Access-Control-Allow-Credentials', 'true')
26-
res.setHeader(
27-
'Access-Control-Allow-Origin',
28-
'https://studio.apollographql.com'
29-
)
30-
res.setHeader(
31-
'Access-Control-Allow-Headers',
32-
'Origin, X-Requested-With, Content-Type, Accept'
33-
)
34-
if (req.method === 'OPTIONS') {
35-
res.end()
36-
return false
37-
}
38-
39-
await startServer
40-
await apolloServer.createHandler({
41-
path: '/api/graphql',
42-
})(req, res)
43-
}
44-
45-
export const config = {
46-
api: {
47-
bodyParser: false,
20+
const server = createServer({
21+
schema: {
22+
typeDefs,
23+
resolvers,
4824
},
49-
}
25+
endpoint: '/api/graphql',
26+
// graphiql: false // uncomment to disable GraphiQL
27+
})
28+
29+
export default server

0 commit comments

Comments
 (0)