Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): refactor api-routes-graphql to GraphQL Yoga #36155

Merged
merged 15 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion examples/api-routes-graphql/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
# API routes with GraphQL server

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.
Next.js ships with [API routes](https://nextjs.org/docs/api-routes/introduction), which provide an easy solution to build your own `API`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a deployed example of this we can see?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, you'll find this branch deployed on Vercel here: https://api-routes-graphql-yoga.vercel.app/api/graphql?query=%7B%0A++greetings%0A%7D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example showcases how to build a lightweight and blazing fast GraphQL API with minimum configuration using GraphQL Yoga.

GraphQL Yoga comes with strong defaults:

- CORS is enabled by default
- Automatically masking unexpected errors and preventing sensitive information from leaking to clients.
- Shipped with GraphiQL

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).

More information on all available features are available [on the official documentation](https://www.graphql-yoga.com/docs/quick-start).

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.

Similar to Express middlewares allowing you to customize requests' behavior, Envelop applies the same idea to GraphQL requests.

More information on [Envelop documentation](https://www.envelop.dev/).

## Deploy your own

Expand Down
5 changes: 2 additions & 3 deletions examples/api-routes-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"start": "next start"
},
"dependencies": {
"apollo-server-micro": "^3.0.1",
"graphql": "^15.5.1",
"micro": "^9.3.4",
"@graphql-yoga/node": "^2.2.1",
"graphql": "^16.3.0",
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
42 changes: 11 additions & 31 deletions examples/api-routes-graphql/pages/api/graphql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApolloServer, gql } from 'apollo-server-micro'
import { createServer } from '@graphql-yoga/node'

const typeDefs = gql`
const typeDefs = /* GraphQL */ `
type Query {
users: [User!]!
}
Expand All @@ -17,33 +17,13 @@ const resolvers = {
},
}

const apolloServer = new ApolloServer({ typeDefs, resolvers })

const startServer = apolloServer.start()

export default async function handler(req, res) {
res.setHeader('Access-Control-Allow-Credentials', 'true')
res.setHeader(
'Access-Control-Allow-Origin',
'https://studio.apollographql.com'
)
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
)
if (req.method === 'OPTIONS') {
res.end()
return false
}

await startServer
await apolloServer.createHandler({
path: '/api/graphql',
})(req, res)
}

export const config = {
api: {
bodyParser: false,
const server = createServer({
schema: {
typeDefs,
resolvers,
},
}
endpoint: '/api/graphql',
// graphiql: false // uncomment to disable GraphiQL
})

export default server