connect ECONNREFUSED, Apollo Federation

2.2k Views Asked by At

I decided to study the Apollo Federation by the example from their website. I have three services that I am trying to merge under a federation, but I catch this error:

Error checking for changes to service definitions: Couldn't load service definitions for "accounts" at http://localhost:4001: request to http://localhost:4001/ failed, reason: connect ECONNREFUSED 127.0.0.1:4001 This data graph is missing a valid configuration. Couldn't load service definitions for "accounts" at http://localhost:4001: request to http://localhost:4001/ failed, reason: connect ECONNREFUSED 127.0.0.1:4001

Code of services & apollo gateway:

accounts

const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');

const typeDefs = gql`
  extend type Query {
    me: User
  }

  type User @key(fields: "id") {
    id: ID!
    username: String
  }
`;

const resolvers = {
  Query: {
    me() {
      return { id: "1", username: "@ava" }
    }
  },
  User: {
    __resolveReference(user, { fetchUserById }){
      return fetchUserById(user.id)
    }
  }
}

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }]),
  introspection: true
});

server.listen(4001).then(({ url }) => {
    console.log(` Server ready at ${url}`);
});

products

const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');

const typeDefs = gql`
  extend type Query {
  topProducts: Product
}

type Product @key(fields: "upc") {
  upc: String!
  name: String!
  price: Int
}
`;

const resolvers = {
  Query: {
    topProducts() {
      return { upc: "one", name: "Computer", price: "3000" }
    }
  },
  Product: {
    __resolveReference(Product, { fetchProductByupc }){
      return fetchProductByupc(Product.upc)
    }
  }
}

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }]),
  introspection: true
});

server.listen(4002).then(({ url }) => {
    console.log(` Server ready at ${url}`);
});

reviews

const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');

const typeDefs = gql`
type Review {
  body: String
  author: User @provides(fields: "username")
  product: Product
}

extend type User @key(fields: "id") {
  id: ID! @external
  reviews: [Review]
}

extend type Product @key(fields: "upc") {
  upc: String! @external
  reviews: [Review]
}
`;

const resolvers = {
  Review: {
    body() {
      return { body: "Very good", author, product }
    }
  },
  User: {
    __resolveReference(body, { fetchUserByauthor }){
      return fetchUserByauthor(body.author)
    }
  }
}

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }]),
  introspection: true
});

server.listen(4003).then(({ url }) => {
    console.log(` Server ready at ${url}`);
});

apollo gateway

const { ApolloServer} = require('apollo-server');
const { ApolloGateway} = require('@apollo/gateway');
const gateway = new ApolloGateway({
  serviceList: [
    { name: 'accounts', url: 'http://localhost:4001' },
    { name: 'products', url: 'http://localhost:4002' },
    { name: 'reviews', url: 'http://localhost:4003' }
  ]
});

const server = new ApolloServer({ gateway, subscriptions: false });
server.listen(4004).then(({ url }) => {
    console.log(` Server ready at ${url}`);
});
1

There are 1 best solutions below

0
On

Try using the wait-on npm package. It will allow you to wait for specific resources before running your gateway server. See example:

const { ApolloServer } = require('apollo-server')
const { ApolloGateway } = require('@apollo/gateway')
const waitOn = require('wait-on')

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'accounts', url: 'http://localhost:4001' },
    { name: 'products', url: 'http://localhost:4002' },
    { name: 'reviews', url: 'http://localhost:4003' },
  ],
})

const options = {
  resources: ['tcp:4001', 'tcp:4002', 'tcp:4003'],
}

waitOn(options)
  .then(() => {
    const server = new ApolloServer({ gateway, subscriptions: false })
    server.listen(4004).then(({ url }) => {
      console.log(` Server ready at ${url}`)
    })
  })
  .catch((err) => console.log(err))