graphql-http
官方 graphql-http
包 提供了一种创建完全兼容的 GraphQL 服务器的简单方法。它有一个针对 Node.js 原生 http
的处理程序,以及针对 Express、Fastify 和 Koa 等知名框架的处理程序;以及不同运行时(如 Deno 和 Bun)的处理程序。
¥The official graphql-http
package provides a simple way to create a fully compliant GraphQL server. It has a handler for Node.js native http
, together with handlers for well-known frameworks like Express, Fastify and Koa; as well as handlers for different runtimes like Deno and Bun.
Express
import { createHandler } from "graphql-http/lib/use/express" // ES6
const { createHandler } = require("graphql-http/lib/use/express") // CommonJS
createHandler
function createHandler({
schema,
rootValue,
context,
formatError,
validationRules,
}: {
rootValue?: any,
context?: any,
formatError?: Function,
validationRules?: any[],
}): Handler
基于 GraphQL 结构构造 Express 处理程序。
¥Constructs an Express handler based on a GraphQL schema.
有关示例用法,请参阅 tutorial。
¥See the tutorial for sample usage.
请参阅 GitHub 自述文件 以获取更详细的文档,包括如何将 graphql-http
与其他服务器框架和运行时结合使用。
¥See the GitHub README for more extensive documentation, including how to use graphql-http
with other server frameworks and runtimes.