title: connect ECONNREFUSED, Apollo Federation
body: <p>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:</p>
  <p><em>Error checking for changes to service definitions: Couldn't load service definitions for &quot;accounts&quot; 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 &quot;accounts&quot; at http://localhost:4001: request to http://localhost:4001/ failed, reason: connect ECONNREFUSED 127.0.0.1:4001</em></p>
  <p>Code of services &amp; apollo gateway:</p>
  <p><strong>accounts</strong></p>
  <pre><code>const { ApolloServer, gql } = require('apollo-server');
  const { buildFederatedSchema } = require('@apollo/federation');
  
  const typeDefs = gql`
    extend type Query {
      me: User
    }
  
    type User @key(fields: &quot;id&quot;) {
      id: ID!
      username: String
    }
  `;
  
  const resolvers = {
    Query: {
      me() {
        return { id: &quot;1&quot;, username: &quot;@ava&quot; }
      }
    },
    User: {
      __resolveReference(user, { fetchUserById }){
        return fetchUserById(user.id)
      }
    }
  }
  
  const server = new ApolloServer({
    schema: buildFederatedSchema([{ typeDefs, resolvers }]),
    introspection: true
  });
  
  server.listen(4001).then(({ url }) =&gt; {
      console.log(` Server ready at ${url}`);
  });
  </code></pre>
  <p><strong>products</strong></p>
  <pre><code>const { ApolloServer, gql } = require('apollo-server');
  const { buildFederatedSchema } = require('@apollo/federation');
  
  const typeDefs = gql`
    extend type Query {
    topProducts: Product
  }
  
  type Product @key(fields: &quot;upc&quot;) {
    upc: String!
    name: String!
    price: Int
  }
  `;
  
  const resolvers = {
    Query: {
      topProducts() {
        return { upc: &quot;one&quot;, name: &quot;Computer&quot;, price: &quot;3000&quot; }
      }
    },
    Product: {
      __resolveReference(Product, { fetchProductByupc }){
        return fetchProductByupc(Product.upc)
      }
    }
  }
  
  const server = new ApolloServer({
    schema: buildFederatedSchema([{ typeDefs, resolvers }]),
    introspection: true
  });
  
  server.listen(4002).then(({ url }) =&gt; {
      console.log(` Server ready at ${url}`);
  });
  </code></pre>
  <p><strong>reviews</strong></p>
  <pre><code>const { ApolloServer, gql } = require('apollo-server');
  const { buildFederatedSchema } = require('@apollo/federation');
  
  const typeDefs = gql`
  type Review {
    body: String
    author: User @provides(fields: &quot;username&quot;)
    product: Product
  }
  
  extend type User @key(fields: &quot;id&quot;) {
    id: ID! @external
    reviews: [Review]
  }
  
  extend type Product @key(fields: &quot;upc&quot;) {
    upc: String! @external
    reviews: [Review]
  }
  `;
  
  const resolvers = {
    Review: {
      body() {
        return { body: &quot;Very good&quot;, 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 }) =&gt; {
      console.log(` Server ready at ${url}`);
  });
  </code></pre>
  <p><strong>apollo gateway</strong></p>
  <pre><code>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 }) =&gt; {
      console.log(` Server ready at ${url}`);
  });
  </code></pre>