Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use interface implementation as a valid type #116

Closed
charypar opened this issue Nov 5, 2015 · 2 comments
Closed

Cannot use interface implementation as a valid type #116

charypar opened this issue Nov 5, 2015 · 2 comments

Comments

@charypar
Copy link

charypar commented Nov 5, 2015

Let's say I have a schema for some translated content with a common interface for entries:

interface Entry {
  id: ID
  slug: String
  translations: [Entry]
}

type Page implements Entry {
  id: ID
  slug: String
  translations: [Page] # Page implements Entry, this should be ok
}

In graphql-js this errors, saying the translations on Page should be of type [Entry]. Since Page implements Entry and translations of a Page will only ever be Pages, this seems ok to me.

Tried the same with union types with the same result. It's not a disaster, I can widen it to the interface type and use a type fragment in queries, but it seems a little unwieldy.

Am I missing a reason why it's not valid to narrow down an interface type when implementing an interface which specifies a field of an interface type? (uh, sorry, can't make that sentence any simpler)

@leebyron
Copy link
Collaborator

leebyron commented Jan 9, 2016

Closing this since it was fixed in the Dec spec and has since been implemented in graphql-js!

We refer to this as covariant interface implementation. That is you have implemented the interface, but you just supplied a covariant type (Page is an Entry) instead of an invariant type (must be Entry and only Entry).

We started with invariant interface implementation mostly as a simplifying force. Adding this kind of type system rule can add complexity to the rest of the system by introducing new kinds of cases that validation needs to consider, but in this case the complexity added was not untenable and it is worth it to enable this kind of query.

@leebyron leebyron closed this as completed Jan 9, 2016
@charypar
Copy link
Author

charypar commented Jan 9, 2016

Yay, awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants