阿波罗联盟无法启动网关

Apollo federation can't start gateway

你好,我想用 federation.I 跟着这个 tutorial。我可以启动我的子图,但是当我启动我的网关时,我得到这个错误:

Error: A valid schema couldn't be composed. The following composition errors were found:
        Cannot extend type "Query" because it is not defined. Did you mean "User"?

Cannot extend type "Mutation" because it is not defined.

我什至扩展了 Query 和 Mutation,但又遇到了另一个错误。

我的网关代码:

import fastify from "fastify";
import { ApolloServer } from "apollo-server-fastify";
import { ApolloGateway } from "@apollo/gateway";

const PORT = process.env.PORT || 4000;
const IP = "0.0.0.0";

const app = fastify({ trustProxy: true });

const gateway = new ApolloGateway({
  serviceList: [{ name: "amazon", url: "http://localhost:4001/graphql" }],
});

(async () => {
  try {
    const { schema, executor } = await gateway.load();
    const server = new ApolloServer({ schema, executor });

    server.start().then(() => {
      app.register(server.createHandler({ path: "/graphql" }));

      app.listen(PORT, IP, (err) => {
        if (err) {
          console.error(err);
        } else {
          console.log("Server is ready at port 4000");
        }
      });
    });
  } catch (error) {
    console.log("dick");
    console.log("err", error);
  }
})();

我在子图中的模式:

type Query {
  getUsers: [User]!
}

type Mutation {
  createUser(name: String!): Boolean!
}

type User @key(fields: "id") {
  id: ID!
  name: String!
}

很可能您使用的是 graphql 版本 16。在 https://www.apollographql.com/docs/federation/gateway/ 之后,您现在必须使用 15(2021 年 12 月)。尝试:

yarn add graphql@15

您可以在最新版本上做的另一个细节是简单地将网关传递给 ApolloServer,如下所示:

const server = new ApolloServer({ gateway });