apollographql / federation

šŸŒ Ā Build and scale a single data graph across multiple services with Apollo's federation gateway.
https://apollographql.com/docs/federation/
Other
659 stars 248 forks source link

Allow federated entity resolution null handling configuration for lists #1915

Open roookeee opened 2 years ago

roookeee commented 2 years ago

Consider the following federated GraphQL type structure:

type BaseType {
    items: [SomeType!]!
}

type SomeType {
  id: ID!
  someRelation: SomeOtherTypeFromAnotherGraph! // this is resolved by a federated type resolver through another graph
}

type SomeOtherTypeFromAnotherGraph {
   id: ID!
}

When the federated type resolver for SomeOtherTypeFromAnotherGraph returns null for one SomeType in BaseType.items the whole request fails as items elements are non-nullable. In the context of graceful degradation it might be preferable to just drop items in a list that could not resolved because a federated type resolver returned null for a contained entity: imagine you have a list of teaser images you want to show but 1 is currently broken because of eventual consistency across your services - you still want to show the remaining images.

Currently there is no way to configure this behaviour and the only workaround is to make the inner type of items nullable and have the client do the filtering manually, which feels quite cumbersome.

It would be nice to have a directive to configure this behaviour. The biggest downside in this regard is the high specificity of this feature requests: this only works on lists / arrays of elements that contain a federated / combined type.

In the old age of REST this would be done in a custom aggregation service / BFF which is mostly obsolete in the context of federated GraphQL which sadly lacks a feature to compensate.

P.S: I also feel like the documentation of the current handling is lacking or did I skip over it in the official docs?

richardtappenden commented 3 months ago

Given the _entities allows a null response, it does seem like something that federation needs to be able to deal with one way or another.