如何重用GraphQL接口描述

如何重用GraphQL接口描述,graphql,Graphql,假设我有以下图形ql SDL: interface Person { # describes how the person is called name: String } type Student implements Person { # describes how the person is called name: String } 如何重用/避免注释行的重复?假设您使用的是GraphQL.js,如果您的类型定义是使用模板文字定义的,则可以执行以下操作: const

假设我有以下图形ql SDL:

interface Person {
  # describes how the person is called
  name: String
}

type Student implements Person {
  # describes how the person is called
  name: String
}

如何重用/避免注释行的重复?

假设您使用的是GraphQL.js,如果您的类型定义是使用模板文字定义的,则可以执行以下操作:

const personComment = '# describes how the person is called'

const typeDefs = `
interface Person {
  ${personComment}
  name: String
}

type Student implements Person {
  ${personComment}
  name: String
}
`
如果您要导入一个
gql
文件,您可能需要更具创造性,并使用类似
string template
的库

您可以使用相同的方法来减少字段的重复(例如,跨类型及其相应的输入类型)。尽管如此,您可能会发现这样做会降低模式的可读性,并且可能不值得仅仅为了保持内容干燥而这样做