How to Inherit or Extend typeDefs in GraphQL

你。 提交于 2019-12-01 02:00:28

问题


I have a type User. Users can also be a type TeamMember. The only difference between a User and TeamMember is an added field teamRole: String. So, I’d love to do something like the following to avoid having to redundantly define all the user's fields…

  type User {
    id: ID!,
    name: String,
    (many other field defs)
  }

  type TeamMember extends User  {
    teamRole: String,
  }

Anyone aware of a syntax for this? I thought extend would be the answer, but it seems more like javascript’s prototype


回答1:


extend is great if you have a base schema and want to build two or more usable schemas based on it. You can, for example, define a root Query type with queries shared by all schemas, and then extend it within each individual schema to add queries specific to that schema. It's meant as a mechanism to add functionality to existing types, rather than creating new types.

GraphQL does not inherently support inheritance. There is a library out there, however, that does add this functionality, if you're using GraphQL-JS. Link here. It adds both generic types and a way for types to inherit from another type.



来源:https://stackoverflow.com/questions/47523384/how-to-inherit-or-extend-typedefs-in-graphql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!