GraphQL
DolphJS provides a seamless, out-of-the-box integration for GraphQL APIs through the official @dolphjs/graphql package, which is powered by Apollo Server under the hood.
Installation
Section titled “Installation”To get started with GraphQL, install the required packages:
pnpm install @dolphjs/graphql graphqlIf you plan to use a schema builder like type-graphql, you can install it alongside:
pnpm install type-graphqlInitializing a GraphQL Server
Section titled “Initializing a GraphQL Server”Unlike a standard REST API where you pass an array of routing components to the DolphFactory, for GraphQL applications you pass a configuration object containing your GraphQL schema and an optional context function.
import { DolphFactory } from '@dolphjs/dolph';import { buildSchema } from 'type-graphql';import { UserResolver } from './resolvers/user.resolver';
async function bootstrap() { // Build your executable schema (using type-graphql as an example) const schema = await buildSchema({ resolvers: [UserResolver], });
// Initialize DolphJS with the GraphQL adapter config const dolph = new DolphFactory({ graphql: true, // Enables the Apollo Server integration schema, context: ({ req, res }) => { // Return whatever you want to be accessible in your resolvers' context return { headers: req.headers, user: req.user }; }, });
dolph.start();}
bootstrap();How It Works
Section titled “How It Works”When you pass { graphql: true } into the DolphFactory configuration, DolphJS automatically dynamically loads the @dolphjs/graphql package, spins up an Apollo Server instance using your schema and context, and applies it directly as middleware to the underlying Express engine. This means you don’t have to manually bootstrap Apollo Server—DolphJS handles the heavy lifting for you while still giving you full access to the Apollo context!