5

Is it poslible in GraphQL that one interface extends multiple other interfaces?
I need something like this:

interface A
{
   valueA: String
}

interface B
{
   valueB: String
}

interface C extend interface A & B
{
   valueA: String
   valueB: String
}

type D implements C{
   valueA: String
   valueB: String
}

The solution provided Is it possible to implement multiple interfaces in GraphQL? refers to one type implementing multiple interfaces, not one interface extending multiple interfaces

adnan.mujkic
  • 83
  • 1
  • 1
  • 6
  • The [answere](https://stackoverflow.com/questions/45227332/is-it-possible-to-implement-multiple-interfaces-in-graphql) here refers to one **type** implementing multiple interfaces...Please don't mark this as a duplicate, since this is a different type of problem..Please see my description carefully. I need that one **interface** extends multiple interfaces, not that one **type** imoplements multiple interfaces.Tnx – adnan.mujkic Sep 11 '19 at 12:08

2 Answers2

6

The answer to this question when it was asked was No, it is not possible for interfaces to extend (or implement) other interfaces.

The answer to this question today is Yes, it is possible for interfaces to implement other interfaces, but that functionality is not yet implemented in any open source GraphQL servers.

The RFC allowing interfaces to implement other interfaces was merged on January 10, 2020. An implementation of this spec for graphql-js was merged on October 8, 2019, but hasn't been released (it will ultimate be released as graphql-js@15.0.0).


For some use cases, this functionality can be emulated by having types that implement multiple interfaces. For example, consider this schema:

interface Node {
  id: ID!
}

# Ideally we'd like to write `interface Pet implements Node`
# but that's not possible (yet)
interface Pet {
  id: ID!
  name: String!
}

type Cat implements Node, Pet {
  id: ID!
  name: String!
  prefersWetFood: Boolean!
}

Then we can actually write the query

query {
  node(id: "sylviathecat") {
    ... on Pet {
      name
    }
  }
}

This is a valid query as long as there exists at least one implementation of Pet that also implements Node (in fact, the Pet interface doesn't actually need to have the id: ID! field at all).

Travis DePrato
  • 392
  • 3
  • 9
  • Your answer is detailed and useful, but you should edit it nonetheless. The question is about extending interfaces not implementing them. Maybe edit with something similar to "...The answer to this question today is still No, although it is possible for interfaces to implement other interfaces," would suffice? I would also suggest to add that graphql-js@15.0.0 was just released. – user3658510 Apr 04 '20 at 01:01
  • > The question is about extending interfaces not implementing them. The distinction between extending and implementing seems rather pedantic here. The original question used the word extending but the code sample given would actually be classified as interfaces _implementing_ other interfaces. – Travis DePrato Apr 05 '20 at 00:09
  • In some ways you are right, but there is already another question about implementing interfaces in graphql and adnan.mujkic specifically commented that he question is not a duplicate of it. – user3658510 Apr 05 '20 at 15:20
  • Although i have to agree with you in some part. I personally don't see the difference between implementing and extending interfaces in graphql. If it was elsewhere an interface cannot implement another one. It is normally for classes. And the question that he refers is about types implementing interfaces... All in all i probably was being pedantic as you say :) – user3658510 Apr 05 '20 at 15:30
  • Here in 2023 just to add that this works fine in graphql-js, I'm currently on 15.3.0 but 16.6.0 is the latest one. – JHH May 31 '23 at 12:10
0

Only types can implement an interface. An interface cannot implement another interface. You can see the syntax for interfaces defined here, which distinctly lacks the ImplementsInterfaces definition shown here.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • 2
    weird thing is that it's not possible to do `interface B extends AnotherInterface {}` and `type A implements B` – Sebastian Mar 05 '20 at 21:06