This document shows practical examples of how to use the GQL Operations CLI tool.
npm install
npm run buildnode dist/cli.js --helpnode dist/cli.js initThis creates a gqlopera.config.json file with default settings.
node dist/cli.js generatenode dist/cli.js generate --endpoint http://localhost:4000/graphql --output graphqlnode dist/cli.js generate \
--endpoint https://api.github.com/graphql \
--headers '{"Authorization":"Bearer YOUR_GITHUB_TOKEN"}'node dist/cli.js generate --watch --verbosenode dist/cli.js validate{
"endpoint": "http://localhost:4000/graphql",
"output": "graphql",
"headers": {
"Authorization": "Bearer YOUR_TOKEN"
},
"excludeTypes": ["InternalType"],
"watch": false,
"maxDepth": 5,
"maxFields": 50,
"skipCircularRefs": true
}{
"endpoint": "https://api.example.com/graphql",
"output": "extracted-graphql",
"headers": {
"Authorization": "Bearer your-token-here",
"X-API-Key": "your-api-key",
"User-Agent": "GQLOpera"
},
"excludeTypes": ["PrivateType", "InternalData"],
"watch": false,
"maxDepth": 3,
"maxFields": 30,
"skipCircularRefs": true
}After running the tool, you'll get individual operation files:
graphql/
├── query/
│ ├── getUsers.graphql
│ ├── getUserProfile.graphql
│ ├── searchPosts.graphql
│ └── getComments.graphql
├── mutation/
│ ├── createUser.graphql
│ ├── updateProfile.graphql
│ ├── deletePost.graphql
│ └── addComment.graphql
└── subscription/
├── messageUpdates.graphql
└── postNotifications.graphql
Each file contains a complete, executable GraphQL operation:
# query/getUsers.graphql
query GetUsers($offset: Int!, $limit: Int!) {
getUsers(offset: $offset, limit: $limit) {
items {
id
username
email
profile {
firstName
lastName
avatar
}
}
totalCount
}
}node dist/cli.js generate \
--endpoint http://localhost:4000/graphql \
--output my-graphql \
--watchnode dist/cli.js generate \
--endpoint https://api.github.com/graphql \
--headers '{"Authorization":"Bearer YOUR_TOKEN"}' \
--output github-graphqlAdd these scripts to your package.json:
{
"scripts": {
"extract": "gqlopera generate",
"extract:watch": "gqlopera generate --watch",
"extract:validate": "gqlopera validate",
"extract:init": "gqlopera init"
}
}Then use:
npm run extract # Extract operations
npm run extract:watch # Watch mode
npm run extract:validate # Validate setupPerfect integration with GraphQL Code Generator:
# codegen.yml
overwrite: true
schema: "http://localhost:4000/graphql"
documents: "./graphql/**/*.graphql"
generates:
src/generated/graphql.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typescript-react-apollo"
config:
withHooks: true
withComponent: false
withHOC: falseTo make this installable globally:
- Update
package.jsonwith your details - Build the project:
npm run build - Publish:
npm publish - Install globally:
npm install -g gqlopera - Use anywhere:
gqlopera generate
Permission Denied
chmod +x dist/cli.jsModule Not Found
npm install
npm run buildAuthentication Errors
- Check your headers format (must be valid JSON)
- Verify your tokens are valid
- Ensure endpoint URL is correct
Schema Introspection Disabled
- GraphQL introspection must be enabled on the target endpoint
- This is usually enabled in development but disabled in production
Empty Output
- Check if your GraphQL API has any operations defined
- Verify the endpoint is accessible
- Run with
--verboseflag for detailed logging
Operations Too Large/Complex
- Reduce
maxDepthfor simpler operations (try 3 instead of 5) - Reduce
maxFieldsto limit field count per type - Use
excludeTypesto skip complex types - Enable
skipCircularRefsto see where circular references are handled
Performance Issues
- Lower
maxDepthandmaxFieldsfor faster generation - Use
excludeTypesto skip heavy types - Consider targeting specific operations instead of full schema extraction