Waog
Waog

Reputation: 7245

How to reuse GraphQL interface description

Assume I have the following GraphQL SDL:

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

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

How can I reuse/avoid duplication of the comment line?

Upvotes: 0

Views: 272

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

Assuming you're using GraphQL.js, if your type definitions are defined using a template literal, you can just do something like:

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

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

type Student implements Person {
  ${personComment}
  name: String
}
`

If you're importing a gql file, you may have to get more creative and use a library like string-template.

You can use the same approach for reducing duplication of fields (across a type and its corresponding input type, for example). For what it's worth, though, you may find that doing this reduces the readability of your schema and may not be worth it just to keep things DRY.

Upvotes: 2

Related Questions