gqlschemagen simplifies the process of generating GraphQL schemas from Go structs. With support for gqlgen directives, custom naming strategies, and extensive configuration options, this powerful tool adds efficiency to Go projects. Ideal for developers looking to integrate GraphQL seamlessly, it also offers a VSCode extension for a better development experience.
GraphQL Schema Generator
gqlschemagen is a powerful and flexible schema generator specifically designed for Go projects utilizing GraphQL. It analyzes Go structs to produce GraphQL schema files that seamlessly integrate with the gqlgen tool, offering features such as custom naming strategies and extensive configuration options.
Key Features
- Annotation-Based Control: Manage schema generation directly using comments for precise customization.
- Struct Tag Support: Implement fine-grained control over schema generation via struct tags.
- Flexible Field Naming: Choose from multiple case transformations including camel, snake, and pascal cases for field names.
- Generation Strategies: Opt for a single file, multiple files, or per-package files based on project needs.
- Namespace Organization: Easily organize schemas into subdirectories with the
@GqlNamespacedirective. - Integration with gqlgen: Automatic support for
@goModeland@goFielddirectives, enhancing the integration with gqlgen. - Input Type Generation: Automatically generate GraphQL input types from Go structs.
- Field Descriptions: Extract descriptions directly from struct tags or comments, improving schema documentation.
- Highly Configurable: Utilize CLI flags and customize generation on a per-struct basis.
- Embedded Struct Support: Automatically expand embedded struct fields into parent types.
- Extra Fields: Incorporate resolver-only fields into types and inputs with the help of
@GqlExtraField,@GqlTypeExtraField, and@GqlInputExtraFielddirectives.
Quick Start Guide
The GraphQL schema generator can operate in various modes, including:
- Standalone CLI Usage: Execute commands directly to analyze Go structs and generate corresponding GraphQL schemas.
- Integration with gqlgen: Run the
gqlschemagencommands prior to executing gqlgen, ensuring the generated schemas are current before resolver generation.
To generate schemas, simply run:
# Using the installed binary
# Generate schema files
gqlschemagen generate -p ./internal/domain/entities -o ./graph/schema
- Configuration Management: Create a default configuration file to set specific options, helping to streamline the schema generation process. Example:
# Initialize configuration file
gqlschemagen init
Annotation Control
The generator relies on a range of annotations to define which types and fields should be generated:
@GqlType: Defines a GraphQL type, with optional name and description parameters.@GqlInput: Specifies a GraphQL input type with custom naming capabilities.@GqlIgnoreAll: Excludes all fields by default unless explicitly included.- Use of embedded structs, advanced type manipulations, and integrating extra fields are possible through specialized annotations.
Overall, gqlschemagen delivers a comprehensive solution for Go developers needing an effective way to manage and generate GraphQL schemas, enhancing both productivity and code quality.
No comments yet.
Sign in to be the first to comment.