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-yogaApollo 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#
| Option | Apollo | Yoga | Description |
|---|---|---|---|
| typeDefs | ✓ | ✓ | GraphQL schema (SDL string or DocumentNode) |
| resolvers | ✓ | ✓ | Resolvers object |
| path | ✓ | ✓ | Route path (default: /graphql) |
| enablePlayground / graphiql | ✓ | ✓ | IDE in non-production (default: true) |
| context | ✓ | — | Build 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 Server | Yoga | |
|---|---|---|
| Ecosystem | Larger, more plugins | Smaller, focused |
| Context | Built-in context builder | Manual |
| Performance | Good | Better (native fetch) |
| Bundle size | Larger | Smaller |
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.