3

When using GraphQL federated resolvers to resolve an array, and one of the resolved items cannot be found:

  • Observed behaviour: null data returned for entire query, no error message.
  • Desired behaviour: the item which cannot be resolved is silently dropped from results array.

What works

Using Apollo Server federation, we are successfully able to resolve the following query:

query {
  products {
    name
    reviews {
      id
      score
    }
  }
}

where products comes from the Product subgraph and score is resolved by the Review subgraph.

This works fine when all the review ids passed to the Review subgraph are resolvable.

Our Problem

Sometimes the Review subgraph deems that a review should not be returned. The business case could be, for example, it was automatically marked as spam and is blocked until a manual check is done.

In this situation, the Review subgraph returns null for this ID. Example query:

query {
  _entities(representations: [
    {id: 1, __typename: "Review"},
    {id: 2, __typename: "Review"}
  ]) {
    ...on Review {
      id
      score
    }
  }
}

Result

{
  "data": {
    "_entities": [
      {
        "id": "1",
        "score": "94"
      },
      null
    ]
  }
}

When this happens, we would want the federated results to contain the Product and only review 1, the non-resolveable review id 2 having been dropped from the array. Like this:

{
  "data": {
    "products": {
      "name": "Phone cover",
      "reviews": [
        {
          "id": "1",
          "score": "94"
        }
      ]
    }
  }
}

However, Apollo Server returns null for the entire query, with no error message:

{
  "data": null
}

Is this expected behaviour? Is there any way to gain the result I want, or otherwise control how Apollo reacts when one item in a reference array is not resolved?

Fletch
  • 4,829
  • 2
  • 41
  • 55
  • I would recommend filtering null entities from the result in the subgraph component which is processing the query. I understand you want Apollo to handle the results, but the approach handling them in a component you or your team-mates develop is just faster – Andrei Ivantsov Jun 20 '22 at 09:51
  • @AndreiIvantsov that doesn't work, unfortunately. If a resolver should resolve a list of 3 ids, for example, then it needs to return three items - either resolved items or "null". That makes sense - if you were to get e.g. 2 items back, then I guess you wouldn't know which item to correlate with which ID. – Fletch Aug 03 '22 at 06:09

0 Answers0