federation icon indicating copy to clipboard operation
federation copied to clipboard

Unable to add an interface extending an entity interface without breaking dependent subgraphs

Open bluepichu opened this issue 1 year ago • 0 comments

Issue Description

Consider these subgraphs:

ProductsReviews
extend schema @link(
  url: "https://specs.apollo.dev/federation/v2.3"
  import: ["@key", "@interfaceObject"]
)

type Query {
  products: [Product]
}

interface Product @key(fields: "id") {
  id: String!
  name: String
}

interface PhysicalProduct implements Product @key(fields: "id") {
  id: String!
  name: String
  weight: Int
}

type Book implements PhysicalProduct & Product @key(fields: "id") {
  id: String!
  name: String
  weight: Int
}

type Pen implements PhysicalProduct & Product @key(fields: "id") {
  id: String!
  name: String
  weight: Int
}

type Movie implements Product @key(fields: "id") {
  id: String!
  name: String
}
extend schema @link(
  url: "https://specs.apollo.dev/federation/v2.3"
  import: ["@key", "@interfaceObject"]
)

type Query {
  reviews: [Review]
}

type Review {
  id: String!
  content: String
}

type Product @key(fields: "id") @interfaceObject {
  id: String!
  reviews: [Review]
}

I would expect this to compose ok, because the gateway should understand that all PhysicalProduct objects are also Product objects, so resolving reviews on them should be possible via the reviews subgraph. However, attempting to compose the above results in an error:

Interface field "Product.reviews" is declared in  but type "PhysicalProduct", which implements "Product" only in subgraph "products" does not have field "reviews".

This can be worked around by instead declaring the following in the reviews subgraph:

type Product @key(fields: "id") @interfaceObject {
  id: String!
  reviews: [Review] @shareable
}

type PhysicalProduct @key(fields: "id") @interfaceObject {
  id: String!
  reviews: [Review] @shareable
}

But this solution is pretty undesirable, since it means that we can't actually separate concerns between the two subgraphs (as the reviews subgraph needs to be aware of all interfaces extending Product in the products subgraph instead of just the Product interface).

Link to Reproduction

https://codesandbox.io/p/devbox/stoic-jennings-dmtcpt?workspaceId=f349a0f5-d280-4992-b1d5-8f95a3bc8dda

bluepichu avatar Feb 14 '24 22:02 bluepichu