apollographql / federation

🌐  Build and scale a single data graph across multiple services with Apollo's federation gateway.
https://apollographql.com/docs/federation/
Other
664 stars 249 forks source link

Allow reference to field without optional arguments in @external definition #2964

Open smyrick opened 6 months ago

smyrick commented 6 months ago

Lets say I have this schema today which does compose ✅

Subgraph 1

type Query {
  locations: [Location]
}

type Location @key(fields: "id") {
    id: ID!
    name: String!
    photo: String!
}

Subgraph 2

type Location @key(fields: "id") {
    id: ID!
    photo: String! @external
    banner: String @requires(fields: "photo")
}

Now lets say I want to add a new optional argument to the Location.photo field. If I make this optional or specify a default this is not a breaking change for clients and would be accepted in a monograph as valid, however with composition this now fails

Subgraph 1

type Query {
  locations: [Location]
}

type Location @key(fields: "id") {
    id: ID!
    name: String!
    photo(smallVersion: Boolean): String! # New optional arg shouldn't break existing clients
}

Subgraph 2 ❌ (fails composition)

type Location @key(fields: "id") {
    id: ID!
    photo: String! @external
    banner: String @requires(fields: "photo")
}

In order to fix this I need to go to every subgraph reference and update those as well

Subgraph 1

type Query {
  locations: [Location]
}

type Location @key(fields: "id") {
    id: ID!
    name: String!
    photo(smallVersion: Boolean): String! # New optional arg shouldn't break existing clients
}

Subgraph 2 ✅ (passes composition)

type Location @key(fields: "id") {
    id: ID!
    photo(smallVersion: Boolean): String! @external # Need to update reference here and in every subgraph (blocking composition pipeline)
    banner: String @requires(fields: "photo")
}

What I would like to happen

Instead it would be nice if composition recognized when we are only referencing fields with optional arguments and not including them will not break any operations.

Caveat

There is a bit of a work around today which I am not sure is intentional or not, but you can actually add the reference first to the @external field only and this will not break composition, then you can go and add it to the source field.

smyrick commented 6 months ago

Slightly related, this came up when debugging the issues here: https://github.com/apollographql/federation/issues/2276