首页 > 解决方案 > 当实体是另一个实体的本机时,为什么我会在一个服务中看到 _entities 请求?

问题描述

我正在努力实现与 Apollo GraphQL 联邦兼容的服务;我提供的服务是用 Lacinia(Clojure 的 GraphQL 库)编写的。

我有一项定义用户的服务:

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

type Query {
  user_by_id(id:String!) : User
}

schema { query: Query }

还有一个定义产品和扩展用户的第二个:

type User @extends @key(fields: "id") {
  id: String! @external
  favorite_products: [Product]
}

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

type Query {
    product_by_upc(upc: String!) : Product
}

schema { query: Query }

当我执行跨越服务的查询时:

{
  user_by_id(id: "me") {
    id
    name
    favorite_products {
      upc
      name
      price
    }
  }
}

我失败了;以下请求被发送到products服务:

INFO  products.server - {:query "query($representations:[_Any!]!){_entities(representations:$representations){...on User{favorite_products{upc name price}}}}", :vars {:representations [{:__typename "User", :id "me"}]}, :line 52}

这失败了,因为据我所知,产品服务不应该提供等效于__resolveReferencefor 类型User(它扩展了);只需键入Product.

这在文档中非常不清楚,我将尝试在 Product 中为用户的存根提供一种存根引用解析器。

标签: apollo-federation

解决方案


是的,确实,您必须为__resolveReference服务模式扩展的每种类型提供(或等效的)。回想起来,这是有道理的,因为它提供了原始值的“内核”以向下传递到解析器树。


推荐阅读