How do you extend types in GraphQL?

后端 未结 2 2034
悲哀的现实
悲哀的现实 2021-02-11 16:36

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

type Animal {
  species: String
}

type Pet extends A         


        
2条回答
  •  走了就别回头了
    2021-02-11 17:19

    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

提交回复
热议问题