Plugin GraphQL Apollo
Plugin para elysia que te permite usar GraphQL Apollo.
Instálalo con:
bun add graphql @elysia/apollo@next @apollo/serverDespués, úsalo así:
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)Si abres /graphql, deberías ver el playground de Apollo GraphQL para trabajar con él.
Contexto
Sección titulada «Contexto»El callback context recibe el contexto de Elysia, así que puedes leer request desde ahí.
const app = new Elysia() .use( apollo({ typeDefs, resolvers, context: async ({ request }) => { const authorization = request.headers.get('Authorization')
return { authorization } } }) ) .listen(3000)Configuración
Sección titulada «Configuración»Este plugin extiende las ApolloServerOptions de Apollo (el parámetro del constructor de ApolloServer).
Estos son los parámetros adicionales para configurar Apollo Server con Elysia.
Por defecto: "/graphql"
Ruta en la que se expone Apollo Server.
enablePlayground
Sección titulada «enablePlayground»Por defecto: process.env.ENV !== 'production'
Indica si Apollo debe ofrecer Apollo Playground.