RFC X / Rejected

Relax SameResponseShape algorithm to be compatible with covariant fields

Opened on
Closed on

At a glance

Project
GraphQL Spec
Identifier
#883
Stage
RFC X / Rejected
Champion
@IvanGoncharov
Updated
2025-06-12
Latest activity
Advanced to RFC 1 on
Source

Pull request description

At the moment covariance rules for interfaces are incompatible with validation rule for overlapping fragments. For example, if we have schema like this:

interface AddressInterface {
  country_code: String
}

type Port implements AddressInterface {
  country_code: String!
}

type Warehouse implements AddressInterface {
  country_code: String
}

type Query {
  addressInterface: AddressInterface
}

This query is valid:

query {
  addressInterface {
    country_code
  }
}

But if you expand interface field into inline fragments will cause a validation error:

query {
  addressInterface {
    ... on Port {
      country_code
    }
    ... on Warehouse {
      country_code
    }
  }
}

This PR fixes this issue.

Timeline

June 2025

January 2022

September 2021

  • Advanced to RFC 1 on by IvanGoncharov
  • Top comment edited on by IvanGoncharov
  • Pull request created on by IvanGoncharov
  • @IvanGoncharov committed "Relax SameResponseShape algorith to be compatible with covariant fields" on
  • Advanced to RFC 0 on by IvanGoncharov
  • Discussed in GraphQL WG

    Relax rule on Field Selection Merging to allow merging covariant fields Spec PR (15m, Ivan)

    Meeting notes · GraphQL WG · 2021-09-02

    Relax rule on Field Selection Merging to allow merging covariant fields Spec PR (15m, Ivan)

    • [19:20 GMT]
    • https://github.com/graphql/graphql-spec/pull/883
    • Apollo Federation suffers from this issue due to a transform; it was discovered by a client.
    • Michael: it's one of the most complex rules in the spec.
    • Ivan: indeed; I think people may have discovered it and just looked for a workaround.
    • Michael: Sacha from Twitter wants to relax the rules and this would simplify this part of the spec. Lee explained why they made it so strict, but with clients now using __typename so much it's not so important.
    • [19:27 GMT]
    • Lee: you want to be able to accurately model the type of the response shape. Languages which don't have the capability of union types: they can say this is a string, this is an int, but they can't say this is a string or an int. Modern languages can handle this, but older languages cannot.
    • Michael: but the question is can we push this requirement onto the client that has these restrictions rather than making it a requirement for the spec itself?
    • Lee: I think that's interesting and it'd simplify this. There's another argument to be made - a simplified understanding of the response. If you're using an untyped language then a human has to make a guess as to what the type is - e.g. Ruby/Python, given a query, given the shape, can I rely on it? With overlapping fields of different types they might end up doing integer math on strings/etc.
    • Lee: this PR is a very specific change to only the nullable union type, which most languages handle in a different way (and can handle).
    • Michael: in C# a nullable int is different from a non-nullable int, so they are compile-time different. But I'm not against this change.
    • Lee: part of the intent in this for the human legibility is that you should be able to look at the field in the query and know what type you're going to get back in the response in that position. A human might look at this and see Port.country_code is non-nullable so country_code cannot be null but actually if you get a Warehouse.country_code it could be nullable -> confusion.
    • Stephen: is it worth the complexity?
    • Matt: any time you have a fragment spread where you may or may not get the spread fulfilled then every field in it kind of has to be treated as nullable. Something indicating what fragments are fulfilled would help here ;)
    • Michael: the RFC for changing the nullability of fields from the client could be helpful.
    • Ivan: keyed fragments is more of a feature request, whereas this PR is more of a bug fix. If we decide to replace this mechanism at all then there should be no reason not to temporarily patch it. If we recognise it as a bug then we can fix it in a reasonable timeframe rather than doing something vendor specific/finding a complex solution.
    • Michael: what if it's a union? Different fields can have different meaning - client might not think it's confusing.
    • Lee: if I alias two fields to have the same alias where one is String and the other String! should that work?
    • Lee: the actual spec change proposal here has nothing to do with interfaces, it's about relaxing the response shape algorithm to allow nullable/non-nullable to be treated the same.
    • [19:40 GMT]
    • Ivan: restricting this to interfaces would make the validation rule more complex.
    • Matt: I don't think it should be limited to interfaces. My argument is that clients already have to deal with this with fragment spreads where one has a field and one doesn't so the field access has to be nullable in some way - it might not be included in the response. "It might be included in the response but null" doesn't seem like a meaningful difference.
    • Benjie: it is in JavaScript!
    • Lee: and JSON.
    • Matt: sure.
    • Benjie: seems like this strips the nullability comparison deeply even through lists, but is that what we do currently for field compatibility in interfaces?
    • Ivan: yes, interfaces make [A] and [A!]! compatible.
    • Lee: nods
    • Michael: the GitHub schema makes these kinds of changes - nullable on the interface but non-nullable in the implementations.
    • Lee: going back to your question: "is this a bug?" - I don't think so. It's a surprising constraint, but not a bug. Querying a field on an interface and querying a field on a subtype of that interface does not give you the "same type" due to covariance. It's certainly unintuitive - we're not patching something not working as advertised; it's working as advertised but it's not clear. Paths forward:
      • null case: no change
      • response shape is too strict, make it more covariant response shape - make nullability not part of "same" comparison
      • make specific rules for two fields coming from the same interface
    • Ivan: I think not recognising this as an issue would add it to the list of non-intuitive things.
    • Lee: I loosely favour the first option: make no change. Primary impact this will have on downstream tools doing type generation - this'll loosen an assumed constraint that these tools may have which could lead to NPEs for existing code.
    • Ivan: queries that use this feature would be invalid right now so it won't break existing queries.
    • Matt: the concern is more existing clients on new queries.
    • Lee: "I assume the query is valid because a tool upstream would have invalidated it; so given it's valid I know this will be <type X>" and may make an assumption that it gets wrong. Those bugs can be fixed; but it's not a "cleanly safe" change because it involves fixing assumptions in other software.
    • Michael: is the change sufficiently impactful that we must do it?
    • Lee: how frequently does this confusion point surface, how complex is it to resolve? This isn't the first time it's been discussed (ref Sacha's conversation).
    • Michael: I'd also like to go in that direction - exploring what makes sense to change, widen it further, come to a decision but not in a rush.
    • Ivan: when I was presented with this I was certain it was a bug, then I thought the interface implementation rules should match merging - without context on how spec was written I thought it was intentional they were so close to each other, thought it must be a bug. But now I realise it's not a bug which makes things harder to decide direction.
    • [19:55 GMT]
    • Lee: I thought it was a bug when I first saw it too, but when I looked in more I realised it was a constraint working as expected. But it is unintuitive which is why I'm only loosely favouring no change. Michael's guidance here seems smart - rather than "there is a bug with how we think about inline fragments for implemented interfaces" think "what if we generalised changes to the shape compatibility" [paraphrased].
    • Ivan: should I go really far down Sacha's route and fully generalise, or go step by step?
    • Lee: I'd be dubious of completely removing it, whereas your proposed change here is sharp and smart and specifically narrows in on nullability being a different class of response shape definition. What's the range of removing the constraint - what does it involve now? Doing one doesn't prevent us from doing the other later.
    • Ivan: for next time I'll prepare a GraphQL.js PR and speak to people working on clients - it's easier for me to do this now because Apollo have Android and iOS clients.
    • Ivan: can we move it to Stage 1 to agree this is a problem to see if we want to see how to fix.
    • Lee: yeah, I think that's okay.
    • [19:59 GMT]
    • Lee: that wraps us up almost completely on time, I'm going to drop that last agenda item but recommend that you check out the discussion on Discord.
    Meeting notes · GraphQL WG · 2021-09-02