GraphQL
Generating docs for GraphQL schema.
Generate API reference pages from GraphQL schemas, with a playground for queries, mutations and subscriptions. See the demo.
Setup
Install the required packages.
npm i @fumadocs/graphql graphqlGenerate Styles
Add the following line:
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import '@fumadocs/graphql/css/preset.css';Configure Plugin
Create the GraphQL server instance and the <GraphQLPage /> component.
import { createGraphQL } from '@fumadocs/graphql/server';
// note: this is a server-side API
export const graphql = createGraphQL({
// the GraphQL schema, it accepts:
// SDL files/URLs (including `extend type`), SDL text,
// introspection results, and `GraphQLSchema` instances.
input: ['./schema.graphql'],
});See createGraphQL() and createGraphQLPage() for their options.
Generate Pages
Pass staticSource() to your loader(). It generates pages without writing files.
import { loader } from 'fumadocs-core/source';
import { defineDocs } from 'fumadocs-mdx/macro';
import { graphql } from '@/lib/graphql';
const docs = defineDocs({
dir: 'content/docs',
});
export const source = loader(
{
docs: docs.toFumadocsSource(),
graphql: await graphql.staticSource({
baseDir: 'graphql',
}),
},
{
baseUrl: '/docs',
// optional: adds a query/mutation/subscription badge to each page item in page tree
plugins: [graphql.loaderPlugin()],
},
);staticSource() generates a page per operation, grouped into folders by kind (e.g. queries/). Pages of named types are opt-in.
Pages of source now have two types, docs and graphql. Update every place that uses source.
Render GraphQL pages with <GraphQLPage /> in your docs page:
import { GraphQLPage } from '@/components/api-page';
export default function Page({ slugs }: PageProps<'/docs/[...slugs]'>) {
const page = source.getPage(slugs);
if (!page) unstable_notFound();
if (page.type === 'graphql') {
return (
<DocsPage full>
<DocsTitle>{page.data.title}</DocsTitle>
<DocsBody>
<GraphQLPage {...page.data.getGraphQLPageProps()} />
</DocsBody>
</DocsPage>
);
}
// ...
}Ensure the migration is complete!
Run a type check before continuing:
npm run types:checkHow is this guide?
Last updated on
