带有 Fastify 的 Apollo Graphql

Apollo Graphql with Fastify

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

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 query missing”。在屏幕上。

我指的是这个 - https://www.npmjs.com/package/apollo-server-fastify。我错过了任何一步吗?请帮忙。

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

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