Registry / testing / gql-generator

gql-generator

JSON →
library2.0.0jsnpmunverified

gql-generator is a utility designed to automatically generate GraphQL queries, mutations, and subscriptions from a GraphQL schema file. Its primary use case is to streamline the process of writing API tests by removing the need for manual query construction, thereby reducing boilerplate and potential human error. The current stable version is 2.0.0. While a specific release cadence isn't explicitly stated, the project appears to be maintained as a pragmatic tool for testing. A key differentiator is its focus on test automation, generating comprehensive queries that expand all fields by default, with built-in mechanisms to handle recursive types and deprecated fields, which can be configured via CLI flags or programmatic options. It outputs CJS modules containing the generated queries.

npm install gql-generator
INSTALL
IMPORT
SIG · GQL-GENERATOR
G
gql-generator
testingjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gqlg
const gqlg = require('gql-generator')
This package is primarily a CommonJS module for programmatic access or a CLI tool. ESM import is not directly supported for the main `gql-generator` function, though generated output can be consumed in ESM contexts using `import`.
generatedQueries
const queries = require('./path/to/output')
import queries from './path/to/output'
The generated `index.js` files and sub-modules use CommonJS `module.exports`. While Node.js 18 supports ESM, direct `import` of these generated files will fail without explicit `type: 'module'` in the output directory's `package.json` or a transpilation step. Stick to `require()` for broad compatibility.
generatedMutations
const mutations = require('./path/to/output/mutations')
import { signup } from './path/to/output/mutations'
Individual query/mutation files are also CommonJS modules. Access specific operations via object destructuring on the `require` result, e.g., `const { signup } = require('./mutations');` if the module directly exports named properties, or through the property access as shown in the examples (`mutations.signup`).

This quickstart demonstrates programmatically generating GraphQL queries and mutations from an in-memory schema string, writing them to a temporary directory, and then requiring the generated files.

const gqlg = require('gql-generator'); const path = require('path'); const fs = require('fs'); const schemaContent = ` type Query { user(id: Int!): User! posts: [Post!] } type Post { id: Int! title: String! author: User! } type User { id: Int! username: String! email: String! } `; const schemaPath = path.join(__dirname, 'sampleTypeDef.graphql'); const outputPath = path.join(__dirname, 'output'); // Ensure schema file exists for the generator fs.writeFileSync(schemaPath, schemaContent); // Ensure output directory exists if (!fs.existsSync(outputPath)) { fs.mkdirSync(outputPath, { recursive: true }); } async function generateQueries() { console.log('Generating GraphQL queries...'); await gqlg({ schemaFilePath: schemaPath, destDirPath: outputPath, depthLimit: 2, includeDeprecatedFields: false, includeCrossReferences: false }); console.log(`Queries generated in ${outputPath}`); // Example of requiring generated queries const generated = require(outputPath); console.log('\nGenerated User Query:\n', generated.queries.user); console.log('\nGenerated Posts Query:\n', generated.queries.posts); // Clean up generated files for subsequent runs (optional) fs.rmSync(outputPath, { recursive: true, force: true }); fs.unlinkSync(schemaPath); } generateQueries().catch(console.error);
gql-generator --version
Debug
Known issues
gotchaThe generator by default excludes fields marked with the `@deprecated` directive. If you need to include deprecated fields in your generated queries, you must explicitly use the `--includeDeprecatedFields` CLI flag or set `includeDeprecatedFields: true` in programmatic usage.
fix
CLI: `gqlg --includeDeprecatedFields`. Programmatic: `gqlg({ ..., includeDeprecatedFields: true })`.
affects: >=1.0.0
gotchaTo prevent infinite recursion in schemas with circular references, `gql-generator` ignores types that have already been included in parent queries by default. To override this behavior and include cross-references, use the `--includeCrossReferences` CLI flag or programmatic option.
fix
CLI: `gqlg --includeCrossReferences`. Programmatic: `gqlg({ ..., includeCrossReferences: true })`.
affects: >=1.0.0
gotchaThe generated query files use CommonJS `require()` exports. Attempting to `import` these files directly in an ES Module context without proper configuration (e.g., a `package.json` with `"type": "module"` in the output directory, or a build step) will result in errors.
fix
Use `const queries = require('./path/to/output');` to consume the generated queries. If an ESM context is mandatory, consider a transpilation step or a custom loader.
affects: >=1.0.0
Errors
Common errors & fixes
Error: ENOENT: no such file or directory, open './schema.graphql'
The `schemaFilePath` provided to the `gql-generator` CLI or programmatic function does not point to an existing GraphQL schema file.
fix
Verify that the `schemaFilePath` argument correctly specifies the path to your `.graphql` or `.gql` schema definition file, and that the file exists at that location. Use an absolute path or a path relative to the process's current working directory.
ReferenceError: require is not defined in ES module scope
Attempting to `require()` the generated output files (or the `gql-generator` package itself if it were ESM) from within an ES Module (`.mjs` file or `type: 'module'` in `package.json`) context.
fix
Ensure your consuming script for the generated files is running in a CommonJS context (e.g., a `.js` file without `type: 'module'`). If you must use ESM for consumption, you may need to dynamically `import` the CJS module or adjust your build system.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Amazon
1
Resources