Help with @requires

I’m trying to migrate to federation and as part of it i have a type in service A

type Product @key(fields: "id") {
      id: String!
      contentId: String
      ...rest
 }

Where then in service B I’m trying to extend it with

type Content {
  id: String!
  name: String!
  ... rest
}

extend type Product @key(fields: "id") {
  id: String! @external
  contentId: String @external
  content: Content @requires(fields: "contentId")
}

Where service B will resolve the details of content using contentId

But when composing this through the gateway i’m getting an error i don’t quite understand

Error: A valid schema couldn't be composed. The following composition errors were found:
    	The following supergraph API query:
    {
      products(id: "A string value") {
        content {
          ...
        }
      }
    }
    cannot be satisfied by the subgraphs because:
    - from subgraph "A": cannot find field "Product.content".
    - from subgraph "B": cannot satisfy @require conditions on field "Product.content" (please ensure that this is not due to key field "id" being accidentally marked @external).

Any help/advice on this would be greatly appreciated.

Hello! This error is most likely occurring because you’re using Federation 2 composition, in which your entity @key fields should not be marked as @external. Additionally with Federation 2, you don’t need to extend entity types that appear in more than one subgraph.

Try updating your service B Product definition to the following:

type Product @key(fields: "id") {
  id: String!
  contentId: String @external
  content: Content @requires(fields: "contentId")
}

Note the lack of extend, along with the lack of @external on id, as discussed in this section of the docs.

Hope this helps! Feel free to follow up if it doesn’t.

1 Like

Ah i didn’t realise this, changing my schema in service b to what you’ve suggested has solved this problem.

Thanks very much :slight_smile:

1 Like