# Fumadocs (Framework Mode, Astro): GraphQL
URL: /docs/astro/integrations/graphql
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/graphql/index.mdx

Generating docs for GraphQL schema.

Generate API reference pages from GraphQL schemas, with a playground for queries, mutations and subscriptions. See the [demo](/docs/graphql).

## Setup [#setup]

Install the required packages.

```npm
npm i @fumadocs/graphql graphql
```

### Generate Styles [#generate-styles]

Add the following line:

```css title="Tailwind CSS"
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
/* [!code ++] */
@import '@fumadocs/graphql/css/preset.css';
```

### Configure Plugin [#configure-plugin]

Create the GraphQL server instance and the `<GraphQLPage />` component.

```ts tab="lib/graphql.ts"
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'],
});
```

```tsx tab="components/api-page.tsx"
'use client';
import { createGraphQLPage } from '@fumadocs/graphql/ui';

export const GraphQLPage = createGraphQLPage({
  playground: {
    // enable the interactive playground on operation pages (optional)
    url: 'https://api.example.com/graphql',
  },
});
```

See [`createGraphQL()`](/docs/astro/integrations/graphql/server) and [`createGraphQLPage()`](/docs/astro/integrations/graphql/api-page) for their options.

### Generate Pages [#generate-pages]

Pass `staticSource()` to your [`loader()`](/docs/headless/source-api/source). It generates pages without writing files.

```ts title="src/lib/source.ts"
import { getCollection } from 'astro:content';
import { loader } from 'fumadocs-core/source';
import { toFumadocsSource } from '@fumadocs/astro/source';
import { graphql } from '@/lib/graphql'; // [!code ++]

export const source = loader(
  // [!code ++:6]
  {
    docs: toFumadocsSource(await getCollection('docs'), await getCollection('meta')),
    graphql: await graphql.staticSource({
      baseDir: 'graphql',
    }),
  },
  {
    baseUrl: '/docs',
    // [!code ++:2] 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](/docs/astro/integrations/graphql/server#includeoperations--includetypes).

**Pages of `source` now have two types, `docs` and `graphql`.** Update every place that uses `source`.

Render GraphQL pages in an island of their own, which wraps `<GraphQLPage />` with the docs layout:

```astro tab="src/pages/docs/[...slug].astro"
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { Docs } from '@/components/docs';
import { GraphQLDocs } from '@/components/graphql-docs'; // [!code ++]
import { source } from '@/lib/source';

export const prerender = true;

export const getStaticPaths = (() => {
  return source.getPages().map((page) => ({
    params: { slug: decodeURI(page.slugs.join('/')) || undefined },
    props: { page },
  }));
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;

const { page } = Astro.props;
const pageTree = await source.serializePageTree(source.getPageTree());
---

{/* [!code ++:13] */}
{page.type === 'graphql' && (
  <Layout title={page.data.title ?? 'My App'}>
    <GraphQLDocs
      client:load
      pathname={Astro.url.pathname}
      params={Astro.params}
      tree={pageTree}
      title={page.data.title}
      props={page.data.getGraphQLPageProps()}
    />
  </Layout>
)}
```

```tsx tab="src/components/graphql-docs.tsx"
import { type SerializedPageTree, useFumadocsLoader } from 'fumadocs-core/source/client';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { GraphQLPage } from '@/components/api-page';
import { Provider, type ProviderProps } from '@/components/provider';
import { baseOptions } from '@/lib/layout.shared';
import type { GraphQLPageProps } from '@fumadocs/graphql/ui';

export function GraphQLDocs({
  tree,
  title,
  props,
  ...rest
}: ProviderProps & { tree: SerializedPageTree; title?: string; props: GraphQLPageProps }) {
  const data = useFumadocsLoader({ tree });

  return (
    <Provider {...rest}>
      <DocsLayout {...baseOptions()} tree={data.tree}>
        <DocsPage full>
          <DocsTitle>{title}</DocsTitle>
          <DocsBody>
            <GraphQLPage {...props} />
          </DocsBody>
        </DocsPage>
      </DocsLayout>
    </Provider>
  );
}
```

<Callout type="warn" title="Ensure the migration is complete!">
Run a type check before continuing:

```npm
npm run types:check
```
</Callout>