Skip to content

GraphQL

Shokupan provides first-class support for GraphQL through two powerful plugins: Apollo Server and GraphQL Yoga. Choose the one that best fits your needs.

The GraphQL Apollo Plugin integrates Apollo Server 4 into Shokupan.

Terminal window
bun add @apollo/server graphql
import { Shokupan, GraphQLApolloPlugin } from 'shokupan';
const app = new Shokupan();
const typeDefs = `#graphql
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'world',
},
};
app.register(new GraphQLApolloPlugin({
typeDefs,
resolvers,
path: '/graphql' // Optional
}));
await app.listen(3000);
OptionTypeDefaultDescription
typeDefsanyRequiredGraphQL Type Definitions
resolversanyRequiredGraphQL Resolvers
pathstring'/graphql'URL path to mount the GraphQL endpoint
apolloConfigApolloServerOptions{}Additional configuration passed to ApolloServer constructor

The GraphQL Yoga Plugin integrates GraphQL Yoga, offering a lightweight and feature-rich GraphQL server.

Terminal window
bun add graphql-yoga graphql
import { Shokupan, GraphQLYogaPlugin } from 'shokupan';
const app = new Shokupan();
app.register(new GraphQLYogaPlugin({
path: '/graphql',
yogaConfig: {
schema: {
typeDefs: /* GraphQL */ `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello from Yoga!',
},
},
},
}
}));
await app.listen(3000);
OptionTypeDefaultDescription
pathstring'/graphql'URL path to mount the GraphQL endpoint
yogaConfigYogaServerOptionsRequiredConfiguration passed to createYoga. Must include schemas/resolvers.

In both plugins, the Shokupan Context is passed to your resolvers.

// Apollo
const resolvers = {
Query: {
currentUser: (parent, args, context) => {
// Access Shokupan Context
const ctx = context.shokupan;
return ctx.state.user;
},
},
};
// Yoga
const resolvers = {
Query: {
currentUser: (parent, args, context) => {
// Context is merged directly
return context.state.user;
}
}
}