Skip to content

A union type cannot satisfy an interface even if each child type does#1488

Closed
derek-miller wants to merge 1 commit intographql:masterfrom
derek-miller:master
Closed

A union type cannot satisfy an interface even if each child type does#1488
derek-miller wants to merge 1 commit intographql:masterfrom
derek-miller:master

Conversation

@derek-miller
Copy link
Copy Markdown

Below is a simplified example of my schema that is failing to validate:

type Query {
  test: MyConnection
}

interface Node {
  id: ID
}

interface Connection {
  nodes: [Node]
}

type NodeA implements Node {
  id: ID
}
type NodeB implements Node {
  id: ID
}
union MyNode =
  | NodeA
  | NodeB

type MyConnection implements Connection {
  nodes: [MyNode]
}

With error message:

"Interface field Connection.nodes expects type [Node] but MyConnection.nodes is type [MyNode].

It appears that the union type does not properly carry through that it implements the Node interface even though all its child types do. Is this expected behavior or an issue with the validator?

@facebook-github-bot
Copy link
Copy Markdown

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@IvanGoncharov
Copy link
Copy Markdown
Member

@derek-miller It looks like GraphQL specification doesn't handle this case:
http://facebook.github.io/graphql/June2018/#sec-Objects

An object field type is a valid sub‐type if it is an Object type and the interface field type is either an Interface type or a Union type and the object field type is a possible type of the interface field type.

Spec doesn't define possible types here but according to other places it just an object types inherited from interface:
http://facebook.github.io/graphql/June2018/#sec-Interface

possibleTypes returns the list of types that implement this interface. They must be object types.

Proposed behaviour requires a non trivial change to the spec and should be accepted to the spec first: https://github.com/facebook/graphql
I would suggest to open an issue/PR there.

@IvanGoncharov
Copy link
Copy Markdown
Member

Actually even this part is enough to reject schema:

An object field type is a valid sub‐type if it is an Object type

@mjmahone
Copy link
Copy Markdown
Contributor

I'm going to close this: if you want to propose allowing Unions to be sub-types of Interfaces, that would be a different proposal, which would need to include a PR on the GraphQL Spec as well as updates here to showcase the implementation (as @IvanGoncharov mentioned).

@derek-miller thank you, though, for asking a detailed question with actual test cases! The presentation of your question as a PR made it easier to understand what was desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants