GraphQL eXpansion is a library designed to make GraphQL schema authoring less of a hassle by automating the repetitive bits using macro directives.
Try it in playground linked below to get a sense on how it works.
Install the package through your preferred Node.js package manager.
npm install @txe/graphql-x
Run the command
npx graphql-x \
--schema=./path-to-initial/schema.graphql \
--output=./path-to-expanded/schema.graphql
directive @create on OBJECT
directive @update on OBJECT
# Exclude field as field of input type
# generated by @create or @delete.
directive @readonly on FIELD_DEFINITION
directive @delete on OBJECT
directive @item on OBJECT
directive @list(field: String!) on OBJECT
directive @relatedList on FIELD_DEFINITION
Initial schema
directive @create on OBJECT
directive @readonly on FIELD_DEFINITION
scalar DateTime
type Post @create {
id: ID!
createdAt: DateTime @readonly
text: String
}
Expanded schema
directive @create on OBJECT
directive @readonly on FIELD_DEFINITION
scalar DateTime
type Post @create {
id: ID!
createdAt: DateTime @readonly
text: String
}
# start: @create Post
type Mutation {
createPost(input: CreatePostInput!): CreatePostOutput!
}
input CreatePostInput {
data: CreatePostDataInput
dryRun: Boolean
}
input CreatePostDataInput {
text: String
}
union CreatePostOutput
@signature(fields: ["issues", "result"])
@member(type: "CreatePostResult", signature: "result")
@member(type: "CreatePostValidation", signature: "issues") =
| CreatePostResult
| CreatePostValidation
type CreatePostResult {
result: Post!
}
type CreatePostValidation {
issues: CreatePostValidationIssues!
}
scalar CreatePostValidationIssues @issues(input: "CreatePostInput")
# end: @create Post
# start: globals
directive @signature(fields: [String!]!) on UNION
directive @member(type: String!, signature: String!) repeatable on UNION
directive @issues(input: String!) on SCALAR
# end: globals