apollo - Next.JS + Apollo Server(bodyParser 问题)
问题描述
在 next.js api 路由函数上使用 apollo 服务器时,我们有:
export const config = {
api: {
bodyParser: false
},
};
这样 Apollo 就不会因 next.js 的请求而失败
但是如果我们删除这个配置,那么我想 Apollo 将无限等待获取未解析的主体(或未解析的请求流)。
在某种情况下,我也得到由(快速服务器或云功能)解析的请求,这将导致 Apollo 挂出并最终因超时错误而失败。
我该如何处理这个问题?
import Cors from 'micro-cors';
import { ApolloServer, gql } from 'apollo-server-micro';
import { NextApiRequest, NextApiResponse } from 'next';
type Todo = {
...
};
const todos: Todo[] = [
...
];
const todoResolvers = {
Query: {
...
},
Mutation: {
...
};
const todoTypes = gql`
...
`;
const cors = Cors({
origin: "*",
credentials: true,
});
const getApolloServerHandler = async (): Promise<any> => {
const apolloServer = new ApolloServer({
typeDefs: [todoTypes],
resolvers: [todoResolvers],
introspection: true,
playground: true,
});
const handler = apolloServer.createHandler({ path: '/api/graphql' });
const corsHandler = cors(handler);
return corsHandler;
};
export const config = {
api: {
bodyParser: false,
},
};
export default async (req: NextApiRequest, res: NextApiResponse): Promise<void> => {
const apolloServerHandler = await getApolloServerHandler();
return apolloServerHandler(req, res);
};
解决方案
推荐阅读
- c# - linq.dynamic.core join resultSelector InvalidCastException
- html - 在 Mac 上使用 @font-face 会导致文本高度为 0
- java - SpringData - 按方法名称获取单列查询
- oracle-cloud-infrastructure - 从 GenerateAutonomousDataWarehouseWalletDetails 中检索 credentials.zip 文件
- python-3.x - 如何使用 Google Cloud Function 将文件从 Cloud Storage 存储桶推送到实例中?
- bash - Bash:未在 sh 脚本中加载导出的变量
- angular - 如何每 1 秒重新发送一个可观察对象的最后一个值
- javascript - 在js中比较2个对象的最快方法
- objective-c - Objective C - 创建 PDF (Mac OSX / Cocoa)
- gem5 - gem5 stats 中的多列值