GraphQL#

BunTok supports GraphQL via Apollo Server and Yoga plugins. Both lazy-import peer dependencies — zero startup cost if not used.

Installation#

bash
# For Apollo
bun add graphql @apollo/server

# For Yoga
bun add graphql graphql-yoga

Apollo Server#

typescript
import { apolloPlugin } from "@buntok/core/plugins/graphql/apollo";

app.plugin(apolloPlugin({
  typeDefs: `type Query { hello: String }`,
  resolvers: { Query: { hello: () => "Hello from Apollo!" } },
}));

GraphQL Yoga#

typescript
import { yogaPlugin } from "@buntok/core/plugins/graphql/yoga";

app.plugin(yogaPlugin({
  typeDefs: `type Query { hello: String }`,
  resolvers: { Query: { hello: () => "Hello from Yoga!" } },
}));

Options#

OptionApolloYogaDescription
typeDefsGraphQL schema (SDL string or DocumentNode)
resolversResolvers object
pathRoute path (default: /graphql)
enablePlayground / graphiqlIDE in non-production (default: true)
contextBuild GraphQL context from request

Full Example with Yoga#

typescript
import { App } from "@buntok/core";
import { yogaPlugin } from "@buntok/core/plugins/graphql/yoga";

const app = new App();

app.plugin(yogaPlugin({
  typeDefs: `
    type Query {
      users: [User]
      user(id: ID!): User
    }
    type User {
      id: ID!
      name: String!
      email: String!
    }
  `,
  resolvers: {
    Query: {
      users: () => db.user.findMany(),
      user: (_, { id }) => db.user.findUnique({ where: { id } }),
    },
  },
}));

app.listen(1212);

Apollo with Context#

typescript
app.plugin(apolloPlugin({
  typeDefs,
  resolvers,
  context: async ({ request }) => {
    const token = request.headers.get("Authorization")?.split(" ")[1];
    const user = token ? await verifyToken(token) : null;
    return { user };
  },
}));

Playground#

Both plugins enable a GraphQL IDE in development:

  • Apollo: GraphiQL at /graphql
  • Yoga: GraphiQL at /graphql

Disable with enablePlayground: false (Apollo) or graphiql: false (Yoga).

Which to Choose?#

Apollo ServerYoga
EcosystemLarger, more pluginsSmaller, focused
ContextBuilt-in context builderManual
PerformanceGoodBetter (native fetch)
Bundle sizeLargerSmaller
Info
Both plugins lazy-import their peer dependencies. The Apollo plugin wraps @apollo/server with Bun-compatible body parsing. The Yoga plugin uses the native graphql-yoga fetch adapter.