Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Plugins Ecosystem/GraphQL

GraphQL

Mount a complete schema and verify a query.

Complete GraphQL example

The plugin uses the graphql package. Install it if your application does not already include it:

bash
bun add graphql
index.ts
import { Volt, graphqlPlugin } from 'voltts';

const app = new Volt();
app.plugin(graphqlPlugin({
  path: '/graphql',
  typeDefs: 'type Query { greeting(name: String): String! }',
  resolvers: {
    Query: {
      greeting: ({ name }: { name?: string }) => `Hello ${name ?? 'World'}`,
    },
  },
  graphiql: true,
}));
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Execute a query

bash
bun run index.ts
curl -i -X POST http://localhost:3000/graphql -H 'Content-Type: application/json' -d '{"query":"{ greeting(name: \"Chansy\") }"}'

Expect {"data":{"greeting":"Hello Chansy"}}. Open /graphql in a browser requesting HTML for GraphiQL.

Schema and context options

Provide schema or typeDefs. rootValue and resolvers.Query/resolvers.Mutation supply root resolver functions. context(c) can build a request-local context for resolvers. POST supports a single query or an array of query objects. Introspection, query complexity limits, and authorization require application-specific configuration; enabling GraphiQL is not an authentication policy.