2

I have two services (implemented using DGS Netflix) which I want to federate using apollo federation. First one is the user service which model looks something like this:

type User @key(fields: "sku") {
    id: ID!
    sku: Long
    score: Float
}

and each can have a product that comes from another service:

type User @key(fields: "sku") @extends {
    sku: Long! @external
    product: Product!
}

And I want to get all the users with their products, but it can happen that a user has no product.

Is there a way to avoid returning a user when its product is null?

users: [User]

Currently my response looks like this:

"errors": [
    {
      "message": "The field at path '/_entities[0]/product' was declared as a non null type, but the code involved in retrieving data has wrongly returned a null value.  The graphql specification requires that the parent field be set to null, or if that is non nullable that it bubble up null to its parent and so on. The non-nullable type is 'Product' within parent type 'User'",
} ...
    "data": {
        "users": [
          null,
          null,
          {
            "sku": 9002490100490,
            "score": 0.72,
            "product": {
              "title": "Sabritas limón 160 g",
              "name": "Sabritas limón"
            }
          },
          null,
          null,
          null,
          {
            "sku": 99176480310,
            "score": 0.99,
            "product": {
              "title": "Cerveza Boca Negra pilsner 355 ml",
              "name": "Cerveza Boca Negra pilsner"
            }
          },
          null,
          {
            "sku": 8712000030605,
            "score": 0.21,
            "product": {
              "title": "Cerveza Victoria lata 355 ml x12",
              "name": "Cerveza Victoria lata"
            }
          }
        ]
      }

I'd like to have the same list but without the nulls in the list and without the error. Is it possible? I was thinking to add some kind of custom directive that removes the null elements but could not succeed.

4

1 回答 1

0

我还研究了指令作为一种潜在的解决方案,并得出结论认为 GraphQL 不具备这种能力,因此您必须在代码中的某处执行此操作,例如,您可以使用过滤器过滤掉解析器中的响应:

return users.filter(Boolean)
于 2021-07-06T15:02:37.990 回答