router
router copied to clipboard
Bug: @requires directive behaves unexpectedly
Describe the bug
Under certain circumstances, the @requires directive behaves unexpectedly. The router incorrectly asks subgraph for both:
- The target field annotated with
@requires - The dependent fields specified in the
@requiresdirective
To Reproduce
User subgraph:
type Query {
myUser: User
}
type User @key(fields: "id") {
id: Int!
productStats: ProductStats
suitableProductsCount: Int @requires(fields: "productStats { productMeta { cost } }")
}
type ProductStats @key(fields: "id", resolvable: false) {
id: Int!
productMeta: ProductMeta @shareable
}
type ProductMeta {
cost: Int @external
userRating: Int
}
Stats subgraph:
type ProductStats @key(fields: "id") {
id: Int!
productMeta: ProductMeta @shareable
}
type ProductMeta {
cost: Int @shareable
}
Request:
query MyQuery {
myUser {
suitableProductsCount
}
}
What router sends to user subgraph:
query MyQuery__user__0 { myUser { productStats { productMeta { cost } } suitableProductsCount } }
Expected behavior
- router sends request to user subgraph and asks for
{ myUser { id productStats { id } }. - Then, the entity resolver for
ProductStatsis used to obtaincostfrom stats subgraph. - And then the entity resolver for
Useris used to calculatesuitableProductsCount.
Output
I tried to fix this by markingproductMeta: ProductMeta as @external in user subgraph and writing @provides(fields: "productMeta { userRating }"). However, it didn't work.
If I move userRating to another place and mark productMeta: ProductMeta as @external, then it works.
If productMeta is not marked as @external, then it still doesn't work.
Desktop (please complete the following information):
- OS: macOS
- Version 15