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.
Apollo Server
Section titled “Apollo Server”The GraphQL Apollo Plugin integrates Apollo Server 4 into Shokupan.
Installation
Section titled “Installation”bun add @apollo/server graphqlimport { 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);Configuration (Apollo)
Section titled “Configuration (Apollo)”| Option | Type | Default | Description |
|---|---|---|---|
typeDefs | any | Required | GraphQL Type Definitions |
resolvers | any | Required | GraphQL Resolvers |
path | string | '/graphql' | URL path to mount the GraphQL endpoint |
apolloConfig | ApolloServerOptions | {} | Additional configuration passed to ApolloServer constructor |
GraphQL Yoga
Section titled “GraphQL Yoga”The GraphQL Yoga Plugin integrates GraphQL Yoga, offering a lightweight and feature-rich GraphQL server.
Installation
Section titled “Installation”bun add graphql-yoga graphqlimport { 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);Configuration (Yoga)
Section titled “Configuration (Yoga)”| Option | Type | Default | Description |
|---|---|---|---|
path | string | '/graphql' | URL path to mount the GraphQL endpoint |
yogaConfig | YogaServerOptions | Required | Configuration passed to createYoga. Must include schemas/resolvers. |
Accessing Context
Section titled “Accessing Context”In both plugins, the Shokupan Context is passed to your resolvers.
// Apolloconst resolvers = { Query: { currentUser: (parent, args, context) => { // Access Shokupan Context const ctx = context.shokupan; return ctx.state.user; }, },};
// Yogaconst resolvers = { Query: { currentUser: (parent, args, context) => { // Context is merged directly return context.state.user; } }}