我想使用TypeGraphQL用graphql建立一个新的Next JS项目。我认为跳过独立服务器并使用Next JS提供的API路由是一个好主意。 Here是我受启发的一个例子。不幸的是,我无法启动并运行graphql。
接下来的JS启动没有错误,但是在向api/graphql
发送请求后它立即抛出了一个错误
TypeError: resolver is not a function
at apiResolver (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/api-utils.js:6:7)
at DevServer.handleApiRequest (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:43:427)
at async Object.fn (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:35:764)
at async Router.execute (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/router.js:28:28)
at async DevServer.run (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:44:494)
at async DevServer.handleRequest (/home/vavra/Projects/project-united/node_modules/next/dist/next-server/server/next-server.js:13:133)
这是我的文件src / pages / api / graphql
import { Resolver, Query } from "type-graphql";
import { ApolloServer } from "apollo-server-micro";
import { buildSchema } from "type-graphql";
import "reflect-metadata";
@Resolver()
class HelloResolver {
@Query(() => String)
hello() {
return "hello";
}
}
export const config = {
api: {
bodyParser: false,
},
};
export default (async () => {
const schema = await buildSchema({
resolvers: [HelloResolver],
});
const apolloServer = new ApolloServer({ schema });
return apolloServer.createHandler({ path: "/api/graphql" });
})();
对此有任何帮助吗?
答案 0 :(得分:0)
here他们正在谈论一个类似的问题,我使用这种形式使用type-graphql处理nextjs。
pages / api / graphql
import 'reflect-metadata';
import { ApolloServer } from 'apollo-server-micro';
import { buildSchema } from 'type-graphql';
import youResolver from '../resolvers/youResolver';
import { NextApiRequest, NextApiResponse } from 'next';
let apolloServerHandler: (req: any, res: any) => Promise<void>;
const getApolloServerHandler = async () => {
if (!apolloServerHandler) {
const schema = await buildSchema({
resolvers: [youResolver],
});
apolloServerHandler = new ApolloServer({ schema }).createHandler({
path: '/api/graphql',
});
}
return apolloServerHandler;
};
export default async (req: NextApiRequest, res: NextApiResponse) => {
const apolloServerHandler = await getApolloServerHandler();
return apolloServerHandler(req, res);
};
export const config = { api: { bodyParser: false } };