阿波罗服务器集成以快速化
导言
阿波罗服务器集成,用于快取.
这是一个简单的软件包,可以方便的将自己的Fastify服务器执行与阿波罗服务器实例连接.
所需资源
- 节点.js v20 或稍后时间
- 速报 v 5.3.0 或稍后时间
- 图QL.js v16 或稍后时间
- 阿波罗服务器 v4 或稍后时间
安装
npm install @as-integrations/fastify @apollo/server graphql fastify
使用量
设置 快捷化 打开 阿波罗服务器 就像你通常会,然后通过使用 fastifyApollo 插件 :
import Fastify from "fastify";
import { ApolloServer, BaseContext } from "@apollo/server";
import fastifyApollo, { fastifyApolloDrainPlugin } from "@as-integrations/fastify";
// ...
const fastify = Fastify();
const apollo = new ApolloServer<BaseContext>({
typeDefs,
resolvers,
plugins: [fastifyApolloDrainPlugin(fastify)],
});
await apollo.start();
// ...
await fastify.register(fastifyApollo(apollo));
说明: 你必须打电话等待 apollo.start() 在使用整合之前.
或者您可以使用导出函数 fastifyApolloHandler 可以传入任何 快速路由处理器。 。 。 .
这使得您可以明确设置所有路径选项,比如URL路径和被接受的方法.
实例如下:
import { fastifyApolloHandler } from "@as-integrations/fastify";
// ... setup Fastify & Apollo
fastify.post("/graphql", fastifyApolloHandler(apollo));
// OR
fastify.get("/api", fastifyApolloHandler(apollo));
// OR
fastify.route({
url: "/graphql",
method: ["POST", "OPTIONS"],
handler: fastifyApolloHandler(apollo),
});
请看 实例。 。 。 .
A. 背景情况
阿波罗服务器 v4 已将上下文设置移出 ApolloServer 建筑师.
定义您自己的上下文函数并将其传递到 context 选项。 此函数接受两个参数 - 请求和答复 。 它们是一类的 FastifyRequest 财务报告和审定财务报表 FastifyReply 页:1 无论从函数中返回到什么 都会传给 context 在你的解决者中争论.
例如:
import { ApolloServer } from "@apollo/server";
import fastifyApollo, {
fastifyApolloHandler,
ApolloFastifyContextFunction,
} from "@as-integrations/fastify";
// ...
interface MyContext {
authorization: JWTPayload | false;
}
const apollo = new ApolloServer<MyContext>(...);
const myContextFunction: ApolloFastifyContextFunction<MyContext> = async (request, reply) => ({
authorization: await isAuthorized(request.headers.authorization),
});
await fastify.register(fastifyApollo(apollo), {
context: myContextFunction,
});
// OR
await fastify.post("/graphql", fastifyApolloHandler(apollo, {
context: myContextFunction,
}));
// Access the context in your resolvers
export const resolvers = {
Query: {
helloWorld: (parent, args, context, info) => {
if (!context.authorization) {
throw new Error("Not authorized");
}
return "Hello world :)";
},
},
};
API 密码
除在 ApolloServer 举个例子.
fastifyApollo
function fastifyApollo<Context extends BaseContext = BaseContext>(
apollo: ApolloServer<Context>,
): FastifyPluginAsync<ApolloFastifyPluginOptions<Context>>;
fastifyApolloHandler
function fastifyApolloHandler<Context extends BaseContext = BaseContext>(
apollo: ApolloServer<Context>,
options?: ApolloFastifyHandlerOptions<Context>,
): RouteHandlerMethod;
ApolloFastifyContextFunction
type ApolloFastifyContextFunction<Context> = (
request: FastifyRequest,
reply: FastifyReply,
) => Promise<Context>;
ApolloFastifyHandlerOptions
interface ApolloFastifyHandlerOptions<Context extends BaseContext = BaseContext> {
context?: ApolloFastifyContextFunction<Context>; // default: async () => ({})
}
ApolloFastifyPluginOptions
interface ApolloFastifyPluginOptions<Context extends BaseContext = BaseContext>
extends ApolloFastifyHandlerOptions<Context> {
path?: string; // default: "/graphql"
method?: HTTPMethod | HTTPMethod[]; // default: ["GET", "POST", "OPTIONS"]
}
HTTPMethod 从 Fastify 中导出.
HTTPS/HTTP2 高频
所有功能和类型都允许您通过或推断 a Server 从快取.