GraphQL
為 GraphQL schema 產生文件。
從 GraphQL schema 產生 API 參考頁面,並提供互動測試區,可用來測試查詢 (query) 、變更 (mutation) 與訂閱 (subscription) 。請參閱示範。
設定
請安裝所需的套件。
npm i @fumadocs/graphql graphql產生樣式
請加入下面這一行:
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import '@fumadocs/graphql/css/preset.css';設定外掛
建立 GraphQL 伺服器實例與 <GraphQLPage /> 元件。
import { createGraphQL } from '@fumadocs/graphql/server';
// 注意:這是伺服器端 API
export const graphql = createGraphQL({
// GraphQL schema,可接受:
// SDL 檔案/URL(包含 `extend type`)、SDL 文字、
// introspection 查詢結果,以及 `GraphQLSchema` 實例。
input: ['./schema.graphql'],
});各自的選項請參閱 createGraphQL() 與 createGraphQLPage()。
產生頁面
將 staticSource() 傳入您的 loader(),即可在不寫入檔案的情況下產生頁面。
由於 staticSource() 會讀取檔案,source.ts 將變成僅限伺服器端使用。請將 docs 移至 collections.ts,供用戶端程式碼匯入。
import { loader } from 'fumadocs-core/source';
import { defineDocs } from 'fumadocs-mdx/macro';
import { docs } from '@/lib/collections';
import { graphql } from '@/lib/graphql';
export const docs = defineDocs({
dir: 'content/docs',
docs: {
async: true,
},
});
// 由於 `staticSource()` 會讀取檔案,現在僅限伺服器端使用
export const source = loader(
{
docs: docs.toFumadocsSource(),
graphql: await graphql.staticSource({
baseDir: 'graphql',
}),
},
{
baseUrl: '/docs',
// 選用:為頁面樹中的每個頁面項目加上 query/mutation/subscription 徽章
plugins: [graphql.loaderPlugin()],
},
);staticSource() 會為每個 operation 產生一個頁面,並依種類分組到資料夾中 (例如 queries/) 。具名型別的頁面則需手動啟用。
source 的頁面現在有兩種類型:docs 與 graphql。 請更新所有使用到 source 的地方。
請在您的文件頁面中使用 <GraphQLPage /> 算繪 GraphQL 頁面:
import { docs, source } from '@/lib/source';
import { docs } from '@/lib/collections';
import { source } from '@/lib/source';
import { GraphQLPage } from '@/components/api-page';
import type { OperationExample } from '@fumadocs/graphql';
// server function 結果的型別層級檢查無法驗證 operation 的範例,
// 因此需將其註冊為可序列化(若無法解析 `@tanstack/router-core`,請先安裝)
declare module '@tanstack/router-core' {
interface SerializableExtensions {
graphqlExample: OperationExample;
}
}
export const Route = createFileRoute('/docs/$')({
component: Page,
loader: async ({ params }) => {
const slugs = params._splat?.split('/') ?? [];
const data = await serverLoader({ data: slugs });
if (data.type === 'docs') await docs.getPage(data.path)?.preload();
return data;
},
});
const serverLoader = createServerFn({
method: 'GET',
})
.validator((slugs: string[]) => slugs)
.handler(async ({ data: slugs }) => {
const page = source.getPage(slugs);
if (!page) throw notFound();
const pageTree = await source.serializePageTree(source.getPageTree());
if (page.type === 'graphql') {
return {
type: 'graphql',
title: page.data.title,
props: page.data.getGraphQLPageProps(),
pageTree,
};
}
return { type: 'docs', path: page.path, pageTree };
});
function Page() {
const data = useFumadocsLoader(Route.useLoaderData());
return (
<DocsLayout {...baseOptions()} tree={data.pageTree}>
{data.type === 'graphql' && (
<DocsPage full>
<DocsTitle>{data.title}</DocsTitle>
<DocsBody>
<GraphQLPage {...data.props} />
</DocsBody>
</DocsPage>
)}
<Suspense>
{data.type === 'docs' && <Content path={data.path} />}
</Suspense>
</DocsLayout>
);
}請確認遷移已全部完成!
繼續之前,請先執行型別檢查:
npm run types:checkHow is this guide?
最後更新於
