1

我决定通过他们网站上的例子来研究阿波罗联盟。我试图在联合下合并三个服务,但我发现了这个错误:

检查服务定义更改时出错:无法在 http://localhost:4001 加载“帐户”的服务定义:对 http://localhost:4001/ 的请求失败,原因:连接 ECONNREFUSED 127.0.0.1:4001 此数据图缺少有效的配置。无法在 http://localhost:4001 加载“帐户”的服务定义:对 http://localhost:4001/ 的请求失败,原因:连接 ECONNREFUSED 127.0.0.1:4001

服务代码和阿波罗网关:

帐户

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

产品

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

评论

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

阿波罗网关

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}`);
});
4

1 回答 1

1

尝试使用等待 npm 包。它将允许您在运行网关服务器之前等待特定资源。参见示例:

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))
于 2021-06-18T13:09:51.143 回答