如何使用graphql express-server构建Next.js应用程序[关闭]

dbf7pr2w  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(67)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

8天前关闭
Improve this question
我熟悉node.js express server with graphql express-server based API build technique.所以我想用next.js API路由来构建一个API。那么如何做这个API配置呢?
我也尝试从五月的资源。。我不高兴有技术。

bvjxkvbb

bvjxkvbb1#

首先,我们必须安装Graphql所需的基本包
第一个月
现在我们可以创建一个基本模式

// schema.js
import { buildSchema } from 'graphql';

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const resolvers = {
  Query: {
    hello: () => 'Hello World using GraphQL and Next.js!',
  },
};

export { schema, resolvers };

字符串
然后,我们只需在/pages/api中创建端点

// pages/api/graphql.js
import { ApolloServer } from 'apollo-server-micro';
import { schema, resolvers } from '../../models/schema.js';

const server = new ApolloServer({
  typeDefs: schema,
  resolvers,
  playground: true,
});

export const config = {
  api: {
    bodyParser: false,
  },
};

export default server.createHandler({ path: '/api/graphql' });

相关问题