Search code examples
apollo-serverapollo-federation

Apollo federation can't start gateway


Hello I want to use federation.I followed this tutorial. I can start my subgraph but when I start my gateway, I get this error:

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.

I even extended Query and Mutation but got another error.

my gateway code:

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);
  }
})();

my schema in subgraph:

type Query {
  getUsers: [User]!
}

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

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

Solution

  • Most likely you're using graphql version 16. Following https://www.apollographql.com/docs/federation/gateway/ you must use 15 for now (December 2021). Try:

    yarn add graphql@15
    

    Another detail you can do on latest versions is simply pass the gateway to ApolloServer, like this:

    const server = new ApolloServer({ gateway });