GraphQL Yoga plugin
This plugin integrates GraphQL Yoga with Elysia
Install with:
bun add @elysia/graphql-yoga@nextThen 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:
bun add graphql graphql-yogaResolver
Section titled “Resolver”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.
Context
Section titled “Context”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)Config
Section titled “Config”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