如何在新版本的Next.js 13.2 API路由中启动GraphQL服务器?

huangapple go评论63阅读模式
英文:

How to start the graphql server in new version of next js 13.2 api route?

问题

无法启动 Next.js 13.2 中的服务器。

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateNextHandler } from '@as-integrations/next';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});

export async function GET(request: Request) {
  return startServerAndCreateNextHandler(server);
}

我尝试将 GraphQL 服务器集成到 Next.js 13.2 中的应用程序目录 API 路由,但对我来说无法正常工作。

英文:

Unable to start the server in next js 13.2.

import { ApolloServer } from '@apollo/server';
import { startServerAndCreateNextHandler } from '@as-integrations/next';

const resolvers = {
  Query: {
    hello: () => 'world',
  },
};

const typeDefs = `#graphql
  type Query {
    hello: String
  }
`;

const server = new ApolloServer({
  resolvers,
  typeDefs,
});


export async function GET(request: Request) {
  return startServerAndCreateNextHandler(server);
}

I tried to integrate the graphql server in next js 13.2 app directory api route but it's not working for me.

答案1

得分: 1

除非您有不同的工作流程要求,否则您似乎缺少默认设置。

export async function ... 替换为

export default startServerAndCreateNextHandler(server);

这是关于在 NextJS 上进行示例集成的参考链接:
https://github.com/apollo-server-integrations/apollo-server-integration-next

英文:

Unless you have a different workflow requirement, you seem to be missing the default?

Replace export async function ... to

export default startServerAndCreateNextHandler(server);

Here's the reference for sample integration on NextJS
https://github.com/apollo-server-integrations/apollo-server-integration-next

答案2

得分: 0

The @as-integrations/next package hasn't been updated to work with this feature yet, but in the issues someone has modified the code to work.

英文:

The @as-integrations/next package hasn't been updated to work with this feature yet, but in the issues someone has modified the code to work.

huangapple
  • 本文由 发表于 2023年2月27日 13:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定