adnan.mujkic
adnan.mujkic

Reputation: 103

Interface extends multiple interfaces in GraphQL Schema

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

Upvotes: 7

Views: 13011

Answers (2)

Travis DePrato
Travis DePrato

Reputation: 402

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 [email protected]).


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).

Upvotes: 7

Daniel Rearden
Daniel Rearden

Reputation: 84687

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.

Upvotes: 0

Related Questions