Skip to content

GraphQL Yoga plugin

This plugin integrates GraphQL Yoga with Elysia

Install with:

Terminal window
bun add @elysia/graphql-yoga@next

Then use it:

import { Elysia } from 'elysia'
import { yoga } from '@elysia/graphql-yoga'
const app = new Elysia()
.use(
yoga({
typeDefs: /* GraphQL */ `
type Query {
hi: String
}
`,
resolvers: {
Query: {
hi: () => 'Hello from Elysia'
}
}
})
)
.listen(3000)

Accessing /graphql in the browser (GET request) would show you a GraphiQL instance for the GraphQL-enabled Elysia server.

Optionally, you can install a custom version of optional peer dependencies as well:

Terminal window
bun add graphql graphql-yoga

Elysia uses Mobius to infer types from the typeDefs field automatically, allowing you to get full type-safety and auto-complete when typing resolver types.

You can add custom context to the resolver function by adding context

import { Elysia } from 'elysia'
import { yoga } from '@elysia/graphql-yoga'
const app = new Elysia()
.use(
yoga({
typeDefs: /* GraphQL */ `
type Query {
hi: String
}
`,
context: {
name: 'Mobius'
},
// Required for resolvers to infer the context type
useContext(_) {},
resolvers: {
Query: {
hi: async (parent, args, context) => context.name
}
}
})
)
.listen(3000)

This plugin extends GraphQL Yoga’s createYoga options, please refer to the GraphQL Yoga documentation by inlining the schema config at the root.

Below is a config which is accepted by the plugin

Default: /graphql

Endpoint to expose GraphQL handler

Type to search the documentation.

    Up arrowDown arrow to navigateEnter to selectEscape to close