In GraphQL, how do you do basic extend types?

2019-02-25 07:21发布

For example, a Pet is an Animal with an owner and name.

type Animal {
  species: String
}

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

1条回答
女痞
2楼-- · 2019-02-25 07:43

This is currently not possible in GraphQL, however there is an experimental package out there that might be useful for this purpose.

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

See example:

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

Which results in:

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
}

What you can also do, since these are just strings is to create some helper functions that modify the string and insert the necessary fields.

If you are intereseted in following the discussion on Github, you can have a look at the following issue.

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

查看更多
登录 后发表回答