nicolasdao / graphql-s2s

Add GraphQL Schema support for type inheritance, generic typing, metadata decoration. Transpile the enriched GraphQL string schema into the standard string schema understood by graphql.js and the Apollo server client.
Other
187 stars 15 forks source link

Interface inheritance #18

Closed MikeVeerman closed 5 years ago

MikeVeerman commented 6 years ago

Dear Nicolas,

Inheritance is really missing from GraphQL, so this library is a great contribution to the ecosystem!

I'm currently running into a use case where we want to inherit with an interface. This currently works by doing something like this :

type NodeParent {
    id: ID!
}

interface Node {
    id: ID!
}

type Person inherits NodeParent implements Node {
    firstname: String
    lastname: String
}

This is a bit verbose, but we also end up with a type that we will never use (NodeParent). This "abstract class" will be visible to the user through GraphQL introspection.

I have an alternative in mind that allows developers to still hand pick interface implementation, but allows them to inherit when they want to. It would allow inheriting from interfaces.

What I was thinking of was code that transpiles this :

interface Node {
    id: ID!
}

type Person inherits Node implements Node {
    firstname: String
    lastname: String
}

type Student inherits Person {
    nickname: String
}

type Query {
  students: [Student]
}

into this :

interface Node {
    id: ID!
}

// Person has inherited the "id" field and implements the interface
type Person implements Node {
    id: ID!
    firstname: String
    lastname: String
}

// Student has inherited the fields from Person, but the 
// developer chose not to implement the interface
type Student {
    id: ID!
    firstname: String
    lastname: String
    nickname: String
}
type Query {
    students: [Student]
}

I've built this locally with minimal code changes and was wondering if you would accept a PR that allowed this use case.

Kind regards, Mike

nicolasdao commented 6 years ago

Hi @MikeVeerman ,

Thanks so much for your suggestion. Your suggestion makes total sense. I will gladly accept your PR as long as you add a unit test following the instructions of the README.md file here https://github.com/nicolasdao/graphql-s2s#contribute.

Let me know if you need help.

Thanks a lot again,

Nic