-
Notifications
You must be signed in to change notification settings - Fork 575
docs: add integration guide for GraphQL Yoga with Nuxt 4 #3829
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
Draft
productdevbook
wants to merge
1
commit into
graphql-hive:main
Choose a base branch
from
productdevbook:feat-nuxt-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
website/src/content/docs/integrations/integration-with-nuxt.mdx
This file contains hidden or 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,139 @@ | ||
--- | ||
description: | ||
Integrate GraphQL Yoga with Nuxt 4 to build powerful, type-safe GraphQL APIs with a modern | ||
developer experience and built-in Apollo Sandbox interface. | ||
--- | ||
|
||
# Integration with Nuxt 4 | ||
|
||
[GraphQL Yoga](https://the-guild.dev/graphql/yoga-server) is a fully-featured GraphQL server with focus on easy setup, performance and great developer experience. This guide shows how to integrate GraphQL Yoga with Nuxt 4 to create powerful backend APIs for your Nuxt applications. | ||
|
||
## Installation | ||
|
||
```sh npm2yarn | ||
npm i graphql-yoga graphql | ||
``` | ||
|
||
## Server Setup | ||
|
||
Create the necessary server files to integrate GraphQL Yoga with Nuxt's Nitro server: | ||
|
||
```ts | ||
// server/services/index.ts | ||
import { createSchema } from 'graphql-yoga' | ||
|
||
export const schema = createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String | ||
ping: String | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'world', | ||
ping: () => 'pong', | ||
}, | ||
}, | ||
}) | ||
``` | ||
|
||
```ts | ||
// server/plugins/yoga.ts | ||
import { createYoga } from 'graphql-yoga' | ||
import { defineEventHandler, sendWebResponse, toWebRequest } from 'h3' | ||
import { schema } from '../services' | ||
|
||
const routePath = '/api/graphql' | ||
const healthCheckPath = '/api/graphql/health' | ||
|
||
const yoga = createYoga({ | ||
graphqlEndpoint: routePath, | ||
healthCheckEndpoint: healthCheckPath, | ||
schema: schema, | ||
}) | ||
|
||
export default defineNitroPlugin((nitroApp) => { | ||
nitroApp.router.use(routePath, defineEventHandler(async (event) => { | ||
const response = await yoga.fetch(toWebRequest(event)) | ||
return sendWebResponse(event, response) | ||
})) | ||
|
||
nitroApp.router.use(healthCheckPath, defineEventHandler(async () => { | ||
return { status: 'ok' } | ||
})) | ||
}) | ||
``` | ||
|
||
> You can check the complete example in the GitHub repository | ||
> [here](https://github.com/productdevbook/nuxt-4-graphql-yoga) | ||
|
||
## Usage | ||
|
||
Once your server is running, you can access the Apollo Sandbox by navigating to `/api/graphql` and test your API with a simple query: | ||
|
||
```graphql | ||
query { | ||
hello | ||
ping | ||
} | ||
``` | ||
|
||
## Features | ||
|
||
- ⚡ Quick integration with Nuxt 4's Nitro server | ||
- 🧪 Interactive Apollo Sandbox for testing queries | ||
- 🔄 Hot Module Replacement during development | ||
- 📋 Type-safe schema definition and resolvers | ||
- 🛡️ Health check endpoint at `/api/graphql/health` | ||
|
||
## Advanced Configuration | ||
|
||
### Custom Sandbox UI | ||
|
||
You can customize the Apollo Sandbox interface by modifying the `renderGraphiQL` function: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a different feature covered here; |
||
|
||
```ts | ||
renderGraphiQL: () => { | ||
return ` | ||
<!DOCTYPE html> | ||
<html> | ||
<body style="margin: 0; overflow-x: hidden; overflow-y: hidden"> | ||
<div id="sandbox" style="height:100vh; width:100vw;"></div> | ||
<script src="https://embeddable-sandbox.cdn.apollographql.com/v2/embeddable-sandbox.umd.production.min.js"></script> | ||
<script> | ||
new window.EmbeddedSandbox({ | ||
target: "#sandbox", | ||
initialEndpoint: window.location.href, | ||
hideCookieToggle: false, | ||
initialState: { | ||
includeCookies: true | ||
} | ||
}); | ||
</script> | ||
</body> | ||
</html>` | ||
} | ||
``` | ||
|
||
### Adding Authentication | ||
|
||
You can easily add authentication to your GraphQL API by accessing the Nuxt event context: | ||
|
||
```ts | ||
const yoga = createYoga<{ | ||
event?: H3Event<object> | ||
}>({ | ||
// ... other options | ||
context: ({ event }) => { | ||
// Access event.context for auth data | ||
return { | ||
user: event?.context.user, | ||
// other context values | ||
} | ||
}, | ||
}) | ||
``` | ||
|
||
> You can also check a full example on our GitHub repository | ||
> [here](https://github.com/productdevbook/nuxt-4-graphql-yoga) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if Apollo Sandbox is related?