apollographql / apollographql/federation
Support case where nullable entity can not be resolved
- Dominant language
- TypeScript
- Stars
- 725
- Forks
- 276
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 1
Description
### Issue Description
We have a case where we have one subgraph that might have references to entities that don't exist or no longer exist. We're ok with this ending up `null` in the response and our schema represents that. The data is coming back in the form with want (with appropriate nulls), but there are errors that we don't care about.
In our reproduction, we're fine if the product comes back null.
```graphql
type Review {
id: String!
content: String
product: Product # nullable
}
```
This seems like #2374, but the errors I'm seeing are on the base errors and not extensions. Additionally, I don't understand how that author is successfully returning null from the __resolveReference.
### Link to Reproduction
https://codesandbox.io/p/sandbox/empty-dawn-1qwky5?file=%2Fsrc%2Freviews.ts%3A9%2C32
### Reproduction Steps
Run this query on port 4000 (the supergraph):
```graphql
query ExampleQuery {
reviews {
id
product {
id
name
__typename
... on Book {
author
}
... on Movie {
director
}
}
}
}
```
There are two scenarios in the `reviewData` array and it probably makes sense to flip between them by commenting one of them out at a time.
The first case is where there is a productId that doesn't exist over in the product subgraph (id: 3). I've tried various ways of handling this, though ideally I would just return null from the __resolveReference and call it a day. But that raises an error `Abstract type \"Product\" __resolveReference method must resolve to an Object type at runtime.` I'm not sure how to handle this one. One thing I tried is a generic `UnsupportedProduct`, and that mostly worked, but honestly I'd rather have a null.
The second case is when the review does not have a productId. In this case, it never even tries to call the product __resolveReference. It returns the null in the data as desired, but there's an error `Cannot return null for non-nullable field Product.id.`. This seems like #2374, but it's on response.errors not extensions.
Thanks for the help!
Contributor guide
Research direction
Start with the linked CodeSandbox reproduction, especially src/reviews.ts and the supergraph query on port 4000. Compare the two reviewData scenarios: a missing product entity and a review without productId. Done means the nullable product behavior is reproduced and the resulting response errors are understood and covered by the relevant federation tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100