接口扩展了 GraphQL Schema 中的多个接口

Interface extends multiple interfaces in GraphQL Schema

在 GraphQL 中,一个接口扩展多个其他接口是否可行?
我需要这样的东西:

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
}

提供的解决方案指的是一个类型实现多个接口,而不是一个接口扩展多个接口

只有类型才能实现接口。一个接口不能实现另一个接口。您可以看到定义的接口语法 here, which distinctly lacks the ImplementsInterfaces definition shown here

这个问题被问到时的答案是不,接口不可能扩展(或实现)其他接口

今天这个问题的答案是是的,接口可以实现其他接口,但该功能尚未在任何开源 GraphQL 服务器中实现.

RFC allowing interfaces to implement other interfaces 于 2020 年 1 月 10 日合并。此规范的 graphql-js 实现于 2019 年 10 月 8 日合并,但尚未发布(最终将作为 graphql 发布-js@15.0.0).


对于某些用例,可以通过具有实现多个接口的类型来模拟此功能。例如,考虑这个架构:

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!
}

然后我们就可以实际写查询了

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

这是一个有效的查询,只要存在至少一个 Pet 的实现也实现了 Node(事实上,Pet 接口实际上并不需要有 id: ID! 字段)。