使用 Fastify 的 Apollo Graphql [英] Apollo Graphql with Fastify

查看:41
本文介绍了使用 Fastify 的 Apollo Graphql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Fastify 运行 Apollo Ghraphql 服务器.以下索引文件的代码片段:-

I have been trying to run Apollo Ghraphql server with Fastify. Code snippet for index file below:-

const { ApolloServer } = require('apollo-server-fastify');
const fastify = require('fastify')({ logger: true })
const config = require('./config')

const { typeDefs, resolvers } = require('./schema');
const server = new ApolloServer({
  typeDefs,
    resolvers
});

fastify.register(server.createHandler());
(async function () {
  await fastify.listen(config.port, config.host, (err) => {
    if(err) {
      console.log(err)
      process.exit(1)
    } else {
      console.log(`API server listening on port ${config.port} and host ${config.host}`)
    }

  })
})();

我的架构就像

const { gql } = require("apollo-server");

const typeDefs = gql`
    type Query {
        items: [Item!]!
        item: Item,
        hello: String!
    }
    type Item {
        id: ID!
        name: String!
        message: String!
    }
`;

const resolvers = {
    Query: {
        hello: () => "Hello World",
        items: () => {
      return {
        id: 1,
        name: 'test',
        message: 'test111'
      }
    },
    }
};

module.exports = {
    typeDefs,
    resolvers
};

当我启动服务器并尝试访问 http://localhost:3000/graphql 时,我收到消息缺少 GET 查询".在屏幕上.

When I start the server and try to access http://localhost:3000/graphql I am getting message "GET query missing." on the screen.

我指的是这个 - https://www.npmjs.com/package/apollo-server-fastify.我是否缺少任何步骤.请帮忙.

I am referring this - https://www.npmjs.com/package/apollo-server-fastify. Am I missing any step. Please help.

推荐答案

现在 fastify v3 已经在几周前发布了,但并不是所有的生态系统也都升级了.

Right now fastify v3 has been released few weeks ago and not all the ecosystem has been upgraded as well.

所以你需要安装 fastify v2 npm i fastify@2 因为现在最新的是 v3.

So you need to install fastify v2 npm i fastify@2 since right now the latest is v3.

这篇关于使用 Fastify 的 Apollo Graphql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆