Skip to content

[Proposal] Interface implementation violate "Don't Repeat Yourself" #500

@yaquawa

Description

@yaquawa

Problem

I found it's very inefficient to implement interface in GraphQL.

interface Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
}

type Human implements Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
  starships: [Starship]
  totalCredits: Int
}

In GraphQL, the word implements means "copy all the fields from interface to the target type".
The more types you get, you have to repeat your self more and more... If you add or delete a field in the interface, then you have to delete all the types that implement the interface, which is repeat yourself again.

In GraphQL it's all about "copy and paste", no real implementation there. So why should we repeat ourself like this way?

Proposal

Introducing a simple extends operator just like most of the program languages would be an excellent solution.

type Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
}

type Human extends Character {
  starships: [Starship]
  totalCredits: Int
}

For consistency,Interface also can be extended using the extends operator.

interface Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
}

interface Human extends Character {
  starships: [Starship]
  totalCredits: Int
}

This approach won't introduce any breaking changes, and it's natural to have such an operator because it's not something new concept and fit with GraphQL very well too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions