在GraphQL中,您如何进行基本扩展类型?

时间:2018-02-22 00:43:24

标签: graphql apollo react-apollo apollo-client apollo-server

例如,PetAnimalownername

type Animal {
  species: String
}

type Pet extends Animal {
  owner: Owner
  name: String
}

4 个答案:

答案 0 :(得分:1)

这在GraphQL中是不可能的,但是有一个实验包可能对此有用。

https://github.com/Sydsvenskan/node-graphql-partials

参见示例:

partial LinkFields {
  links(
    rel: String
    type: String
  ): [Link]
}

partial DocumentFields using LinkFields {
  uuid: ID!

  # The document type, such as x-im/article
  type: String
  # If specified, then a list of the products to which this document's availability is limited
  products: [String]
  # The human readable name of the document, often used publicly to identify the document
  title: String

  # The specific path on the web page where this document is publicly available
  path: String

  # A single metadata block
  metaBlock(
    # The specific metadata block type to get
    type: String
  ): MetadataBlock
}

interface Document using DocumentFields {}

type AuthorDocument implements Document using DocumentFields {}

结果是:

type AuthorDocument implements Document {
  links(
    rel: String
    type: String
  ): [Link]

  uuid: ID!

  # The document type, such as x-im/article
  type: String
  # If specified, then a list of the products to which this document's availability is limited
  products: [String]
  # The human readable name of the document, often used publicly to identify the document
  title: String

  # The specific path on the web page where this document is publicly available
  path: String

  # A single metadata block
  metaBlock(
    # The specific metadata block type to get
    type: String
  ): MetadataBlock
}

你也可以做什么,因为这些只是字符串是创建一些帮助函数来修改字符串并插入必要的字段。

如果您参与Github的讨论,可以看一下以下问题。

https://github.com/graphql/graphql-js/issues/703

答案 1 :(得分:1)

June2018 stable version of the GraphQL spec开始,一个对象类型可以扩展另一种对象类型:

  

对象类型扩展用于表示从某种原始类型扩展过来的类型。例如,它可能用于表示本地数据

在您的示例中,

List

这本身不是继承;您只能扩展基本类型,而不能基于它创建新类型。注意,新类型没有名称。现有的List类型得到扩展。

graphql.org documentation没有提及关于type Animal { species: String } extend type Animal { owner: Owner name: String } 的任何内容,但是文档是admittedly lackluster,并且是transitioned,从Facebook的所有权到Linux基金会。 JavaScript参考实现doesn't fully support extensions,但是由于您已标记问题,因此可以使用graphql-toolsdoes

Animal

有关实际类型继承,请参见graphql-s2s library

答案 2 :(得分:1)

其他答案正确地提到这不是专门化的类型扩展。

注意,类型扩展似乎有两种语法。根据 Apollo Federation specification,graphql-java 支持 @extend 语法。

在 Apollo 文档中,语法似乎是:

type Animal @key(fields: "id") @extends

但根据联邦规范,还支持另一种语法:

@

某些框架似乎只支持 {{1}} 语法。

答案 3 :(得分:1)

虽然您不能创建子类/子类型,但您可以使用接口进行某种形式的继承:https://graphql.org/learn/schema/#interfaces

以上链接中的示例:

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
}
 
type Droid implements Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
  primaryFunction: String
}

查询时可以为不同的实现指定特定的字段

hero(episode: $ep) {
    name
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      totalCredits
    }
  }
相关问题