GraphQL Yoga Plugin

This plugin integrate GraphQL yoga with Elysia

Install with:

bun add @elysiajs/graphql-yoga

Then use it:

import { Elysia } from "elysia";
import { yoga } from "@elysiajs/graphql-yoga";

const app = new Elysia()
  .use(
    yoga({
      typeDefs: /* GraphQL */ `
        type Query {
          hi: String
        }
      `,
      resolvers: {
        Query: {
          hi: () => "Hello from Elysia",
        },
      },
    })
  )
  .listen(8080);

Accessing /swagger would show you a Swagger endpoint with generated endpoint from Elysia server.

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

bun add graphql graphql-yoga

Resolver

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

Context

You can add custom context to resolver function by adding context

import { Elysia } from 'elysia'
import { yoga } from '@elysiajs/graphql-yoga'

const app = new Elysia()
    .use(
        yoga({
            typeDefs: /* GraphQL */`
                type Query {
                    hi: String
                }
            `,
            context: {
                name: 'Mobius'
            },
            // If context is a function on this doesn't present
            // for some reason it won't infer context type
            useContext(_) {}
            resolvers: {
                Query: {
                    hi: async (parent, args, context) => context.name
                }
            }
        })
    )
    .listen(8080)

Config

This plugin extends GraphQL Yoga's createYoga options, please refers to the GraphQL Yoga documentation with inlining schema config to root.

Below is a config which is accepted by the plugin

path

@default /graphql

Endpoint to expose GraphQL handler