Skip to content

Bug: @requires directive behaves unexpectedly #7657

@ya-at

Description

@ya-at

Describe the bug

Under certain circumstances, the @requires directive behaves unexpectedly. The router incorrectly asks subgraph for both:

  1. The target field annotated with @requires
  2. The dependent fields specified in the @requires directive

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

  1. router sends request to user subgraph and asks for { myUser { id productStats { id } }.
  2. Then, the entity resolver for ProductStats is used to obtain cost from stats subgraph.
  3. And then the entity resolver for User is used to calculate suitableProductsCount.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions