跳至主要内容

服务器

GraphQL 架构最常见的用途是通过 HTTP API。通过 HTTP 提供 Grafast 架构与通过 HTTP 提供常规 GraphQL 架构非常相似,主要区别在于您应该使用 Grafastexecute/grafast 函数来执行请求,而不是 graphql-js 的 execute/graphql 函数,尽管将文档的解析(理想情况下还有验证)进行记忆以确保在将来的请求中可以再次使用相同的操作计划也非常重要。

您可以使用任何允许您用 Grafastexecute 方法替换 execute 方法的 GraphQL 服务器;我们有自己的原生 Grafserv 服务器,或者您可以使用 Envelop 支持的任何 GraphQL 服务器以及我们的 useGrafast envelop 插件。

注意

如果您在 JavaScript/TypeScript 中维护一个 GraphQL 服务器,并且想要添加 Grafast 支持,请随时与我们联系!

Grafserv

Grafserv 是 Grafast 的配套服务器;它可能是 Node.js 中最快的通用 GraphQL 服务器,但由于它还很年轻,还没有其他服务器那样庞大的扩展生态系统。Grafserv 自动实现我们能想到的所有优化,用于通过 HTTP 提供 GraphQL 架构,包括直接序列化为字符串而无需在内存中构建对象的优化!

阅读更多内容,请访问 Grafserv 文档

Envelop

Envelop 是由 The Guild 的优秀团队编写的一个出色的 GraphQL 运行时插件系统;它可以与大多数主要的 Node.js GraphQL 服务器(以及其他环境中的某些服务器)一起使用,因此是将 Grafast 集成到现有项目中的好方法。我们捆绑了一个 useGrafast() envelop 插件,您可以将其与 useParserCache() 一起使用,以非常轻松地获得优化的执行管道,以下是一个代码片段

import { envelop, useExtendContext, useSchema } from "@envelop/core";
import { useGrafast, useMoreDetailedErrors } from "grafast/envelop";
import { schema } from "./schema";

const getEnveloped = envelop({
plugins: [
// Use our executable schema
useSchema(schema),

// Caching the parser results is critical for Grafast, otherwise it
// must re-plan every GraphQL request!
useParserCache(),
// And we might as well cache validation whilst we're at it:
useValidationCache(),

// Pass your GraphQL context here:
useExtendContext(() => ({
/* ... */
})),

// This replaces graphql-js' `execute` with Grafast's own
useGrafast(),
],
});

有关更多上下文,请参阅 Envelop 文档