跳转到内容
搜索文档

GraphQL

最后更新 查看 MarkdownAgent 设置

GraphQL Pages Plugin 创建 GraphQL 服务器,可响应 application/jsonapplication/graphqlPOST 请求。对于 GET 请求,它返回 GraphQL Playground

安装

npm i @cloudflare/pages-plugin-graphql

用法

import graphQLPlugin from "@cloudflare/pages-plugin-graphql";
import {
	graphql,
	GraphQLSchema,
	GraphQLObjectType,
	GraphQLString,
} from "graphql";

const schema = new GraphQLSchema({
	query: new GraphQLObjectType({
		name: "RootQueryType",
		fields: {
			hello: {
				type: GraphQLString,
				resolve() {
					return "Hello, world!";
				},
			},
		},
	}),
});

export const onRequest: PagesFunction = graphQLPlugin({
	schema,
	graphql,
});

此 Plugin 仅暴露单个路由,因此在挂载的任何位置都可用。在上面的示例中,因为它挂载在 functions/graphql.ts 中,服务器将在 Pages 项目的 /graphql 上可用。

这篇文档对您有帮助吗?