【问题标题】:Building GraphQL services in isolation with apollo-federation使用 apollo-federation 独立构建 GraphQL 服务
【发布时间】:2020-09-30 05:21:36
【问题描述】:

我目前正在尝试测试 1 个服务的 graphql 端点,该端点最终将成为 apollo-federation/gateway graphql 服务器的一部分。此服务将扩展现有联合图中现有服务的类型。

如果我想使用 apollo-federation & gateway 单独测试我的服务,有没有办法在我的 graphql 架构中仍然使用 @extends@external 时做到这一点?目前网关抛出:UnhandledPromiseRejectionWarning: Error: Unknown type: "SomeTypeInAnotherServer",这是有道理的,因为没有要扩展的类型,但我可以忽略这个验证吗?

【问题讨论】:

  • 把它扔在一个答案中,我会选择你!我不知道为什么我自己找不到这个。在发布此之前,我肯定做了一些研究。非常感谢!

标签: graphql apollo-federation


【解决方案1】:

正如@xadm 在评论中发布的那样,您可以使用https://github.com/xolvio/federation-testing-tool 来解决我的问题。

【讨论】:

    【解决方案2】:

    您的问题看起来像是在尝试进行开发,但您给出的答案看起来像是在专门进行测试。我不知道这是否是由于工具而导致的结果,或者这是否是您的实际问题,但这是我为开发人员提供的答案:

    如果您只是运行其中一项服务,您仍然可以对其进行查询,只需按照 ApolloGateway 的方式进行即可。比如说,你有一个 person-service 和一个 place-service,而且 People 可以访问很多地方:

    个人服务

    type Person @key(fields: "id") {
      id: ID!
      name: String
    }
    
    type Query {
      person(id: ID): Person # Assuming this is the only entry-point
    }
    

    地方服务

    type Place {
      id: ID!
      name: String
    }
    
    extend type Person @key(fields: "id") {
      id: ID!
      placesVisited: [Place]
    }
    

    现在您可以对 place-service 进行以下查询:

    query ($_representations: [_Any!]!) {
      _entities(representations:$_representations) {
        ... on Person {
          id
          placesVisited {
            id
            name
          }
        }
      }
    }
    

    这是您的意见:

    {
      "_representations": [{
        "__typename": "Person",
        "id": "some-id-of-some-person"
      }]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2021-03-24
      • 2017-11-29
      • 2017-05-06
      • 2020-12-02
      • 2010-10-25
      • 2021-05-02
      • 2020-06-28
      • 2021-08-15
      相关资源
      最近更新 更多