CLI Example
This example demonstrates how to use gqlscanner as a standalone CLI tool.
Setup
- Install gqlscanner:
go install github.com/pablor21/gqlscanner@latest
Usage
Using Configuration File
The gqlscanner.yml file contains all the configuration:
gqlscanner
This will:
- Read models from
./models
- Generate GraphQL schema to
./schema
- Use single file strategy (all types in one
schema.graphql)
- Use camel case for naming
- Strip suffixes like "DTO", "Entity", "Model"
Using CLI Flags
Override configuration with flags:
gqlscanner -i ./models -o ./schema -s single -c camel
Available Flags
-i, --input: Input directory (default: "./models")
-o, --output: Output directory (default: "./schema")
-s, --strategy: Generation strategy: single or separate (default: "single")
-c, --case: Name case: camel, pascal, snake (default: "camel")
--strip-prefix: Comma-separated prefixes to strip
--strip-suffix: Comma-separated suffixes to strip
-f, --config: Config file path (default: "gqlscanner.yml")
Models
The models/ directory contains example Go structs with gql annotations:
user.go - User, UserRole, UserProfile
post.go - Post, PostStatus, Comment
Annotation Examples
Type Definition:
/**
* @gqlType(name:"User",description:"Represents a user")
*/
type User struct {
ID string `gql:"id,type:ID,required"`
}
Generate Input Types:
/**
* @gqlInput(name:"CreateUserInput")
* @gqlInput(name:"UpdateUserInput")
*/
type User struct {
// ...
}
Enum Types:
/**
* @gqlEnum(description:"User role")
*/
type UserRole string
Field Options:
type: GraphQL type (ID, String, Int, DateTime, etc.)
required: Mark field as non-nullable
optional: Mark field as nullable (pointer fields are auto-optional)
description: Field description
forceResolver: Exclude from input types (for computed fields)
include: Include field even in @gqlIgnoreAll types
Output
Running gqlscanner generates schema/schema.graphql with:
type User {
id: ID!
email: String!
username: String!
firstName: String
lastName: String
bio: String
avatar: String
isActive: Boolean!
role: UserRole!
createdAt: DateTime!
updatedAt: DateTime!
}
input CreateUserInput {
email: String!
username: String!
firstName: String
lastName: String
bio: String
avatar: String
isActive: Boolean!
role: UserRole!
}
# ... more types