Commit d60349b
Sylvain Lebresne
Reuse named fragment in subgraphs even if they only apply partially (#2639)
The focus of the code reusing fragment has been thus far on making
that if a given named fragment can be used in a given selection set,
then the code finds it. But in practice, the named fragments we try
to reuse are those of the input query, so defined against the
supergraph API schema, while we're trying to reuse them on subgraph
fetches, so the fragment may be defined on a type that a subgraph
does know, or request fields the subgraph does not define.
Currently, when asking if a given named fragment can be used against
a given subgraph, we only keep fragment that _fully_ apply to that
subgraph (that is, only if everything the fragment queries exists
in the subgraph).
This does limit the usefulness of named fragment reuse however, as one
of the point of federation is that each subgraph defines a subset
of the full supergraph API.
This commit improves this by instead computing the subset of each
named fragment that do apply to a given subgraph, and define the
fragment as only that fragment for the subgraph.
Note that it does so, this commit do not try reusing a named fragment
against a subgraph if the subset of the fragment on the subgraph
comes to only a single leaf field. This avoids spending time trying
to reuse a fragment that ends up being just:
```graphql
fragment MySuperFragment on X {
__typename
}
```
or even:
```graphql
fragment MySuperFragment on X {
id
}
```
as those aren't productive (the goal of fragment reuse is to try to
make the subgraph fetches smaller, but it is small to just request
`id`, even 10 times, than to request 10 times `...MySuperFragment`).1 parent 191dea1 commit d60349b
6 files changed
Lines changed: 875 additions & 74 deletions
File tree
- .changeset
- gateway-js/src/__tests__
- internals-js/src
- __tests__
- query-planner-js/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
744 | 744 | | |
745 | 745 | | |
746 | 746 | | |
747 | | - | |
| 747 | + | |
748 | 748 | | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
749 | 752 | | |
750 | 753 | | |
751 | 754 | | |
752 | 755 | | |
753 | 756 | | |
754 | 757 | | |
755 | | - | |
| 758 | + | |
756 | 759 | | |
757 | 760 | | |
758 | | - | |
| 761 | + | |
759 | 762 | | |
760 | 763 | | |
761 | 764 | | |
| |||
770 | 773 | | |
771 | 774 | | |
772 | 775 | | |
773 | | - | |
| 776 | + | |
774 | 777 | | |
775 | 778 | | |
776 | | - | |
| 779 | + | |
777 | 780 | | |
778 | 781 | | |
779 | 782 | | |
780 | 783 | | |
781 | | - | |
| 784 | + | |
782 | 785 | | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
783 | 790 | | |
784 | 791 | | |
785 | 792 | | |
| |||
0 commit comments