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

為 GraphQL schema 產生文件。

從 GraphQL schema 產生 API 參考頁面，並提供互動測試區，可用來測試查詢 (query) 、變更 (mutation) 與訂閱 (subscription) 。請參閱[示範](/docs/graphql)。

## 設定 [#setup]

請安裝所需的套件。

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

### 產生樣式 [#generate-styles]

請加入下面這一行：

```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]

建立 GraphQL 伺服器實例與 `<GraphQLPage />` 元件。

```ts tab="lib/graphql.ts"
import { createGraphQL } from '@fumadocs/graphql/server';

// 注意：這是伺服器端 API
export const graphql = createGraphQL({
  // GraphQL schema，可接受：
  // SDL 檔案／URL（包含 `extend type`）、SDL 文字、
  // introspection 查詢結果，以及 `GraphQLSchema` 實例。
  input: ['./schema.graphql'],
});
```

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

export const GraphQLPage = createGraphQLPage({
  playground: {
    // 在 operation 頁面上啟用互動測試區（選用）
    url: 'https://api.example.com/graphql',
  },
});
```

各自的選項請參閱 [`createGraphQL()`](/docs/waku/integrations/graphql/server) 與 [`createGraphQLPage()`](/docs/waku/integrations/graphql/api-page)。

### 產生頁面 [#generate-pages]

將 `staticSource()` 傳入您的 [`loader()`](/docs/headless/source-api/source)，即可在不寫入檔案的情況下產生頁面。

```ts title="src/lib/source.ts"
import { loader } from 'fumadocs-core/source';
import { defineDocs } from 'fumadocs-mdx/macro';
import { graphql } from '@/lib/graphql'; // [!code ++]

const docs = defineDocs({
  dir: 'content/docs',
});

export const source = loader(
  // [!code ++:6]
  {
    docs: docs.toFumadocsSource(),
    graphql: await graphql.staticSource({
      baseDir: 'graphql',
    }),
  },
  {
    baseUrl: '/docs',
    // [!code ++:2] 選用：為頁面樹中的每個頁面項目加上 query/mutation/subscription 徽章
    plugins: [graphql.loaderPlugin()],
  },
);
```

`staticSource()` 會為每個 operation 產生一個頁面，並依種類分組到資料夾中 (例如 `queries/`) 。具名型別的頁面則需[手動啟用](/docs/waku/integrations/graphql/server#includeoperations--includetypes)。

**`source` 的頁面現在有兩種類型：`docs` 與 `graphql`。** 請更新所有使用到 `source` 的地方。

請在您的文件頁面中使用 `<GraphQLPage />` 算繪 GraphQL 頁面：

```tsx title="src/pages/docs/[...slugs].tsx"
import { GraphQLPage } from '@/components/api-page'; // [!code ++]

export default function Page({ slugs }: PageProps<'/docs/[...slugs]'>) {
  const page = source.getPage(slugs);
  if (!page) unstable_notFound();

  // [!code ++:10]
  if (page.type === 'graphql') {
    return (
      <DocsPage full>
        <DocsTitle>{page.data.title}</DocsTitle>
        <DocsBody>
          <GraphQLPage {...page.data.getGraphQLPageProps()} />
        </DocsBody>
      </DocsPage>
    );
  }

  // ...
}
```

<Callout type="warn" title="請確認遷移已全部完成！">
繼續之前，請先執行型別檢查：

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