Skip to content

GraphQL Apollo plugin

Plugin for elysia for using GraphQL Apollo.

Install with:

Terminal window
bun add graphql @elysia/apollo@next @apollo/server

Then use it:

import { Elysia } from 'elysia'
import { apollo, gql } from '@elysia/apollo'
const app = new Elysia()
.use(
apollo({
typeDefs: gql`
type Book {
title: String
author: String
}
type Query {
books: [Book]
}
`,
resolvers: {
Query: {
books: () => {
return [
{
title: 'Elysia',
author: 'saltyAom'
}
]
}
}
}
})
)
.listen(3000)

Accessing /graphql should show the Apollo GraphQL playground to work with.

The context callback receives the Elysia context, so read request from it.

const app = new Elysia()
.use(
apollo({
typeDefs,
resolvers,
context: async ({ request }) => {
const authorization = request.headers.get('Authorization')
return {
authorization
}
}
})
)
.listen(3000)

This plugin extends Apollo’s ApolloServerOptions (which is ApolloServer’s constructor parameter).

Below are the extended parameters for configuring Apollo Server with Elysia.

Default: "/graphql"

Path to expose Apollo Server.

Default: process.env.ENV !== 'production'

Determine whether Apollo should provide Apollo Playground.

Type to search the documentation.

    Up arrowDown arrow to navigateEnter to selectEscape to close