{"id":13249,"library":"gql-generator","title":"GraphQL Query Generator for Testing","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/timqian/gql-generator","tags":["javascript","graphql","query","generator"],"install":[{"cmd":"npm install gql-generator","lang":"bash","label":"npm"},{"cmd":"yarn add gql-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add gql-generator","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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`.","symbol":"gqlg","correct":"const gqlg = require('gql-generator')"},{"note":"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.","wrong":"import queries from './path/to/output'","symbol":"generatedQueries","correct":"const queries = require('./path/to/output')"},{"note":"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`).","wrong":"import { signup } from './path/to/output/mutations'","symbol":"generatedMutations","correct":"const mutations = require('./path/to/output/mutations')"}],"quickstart":{"code":"const gqlg = require('gql-generator');\nconst path = require('path');\nconst fs = require('fs');\n\nconst schemaContent = `\n  type Query {\n    user(id: Int!): User!\n    posts: [Post!]\n  }\n\n  type Post {\n    id: Int!\n    title: String!\n    author: User!\n  }\n\n  type User {\n    id: Int!\n    username: String!\n    email: String!\n  }\n`;\n\nconst schemaPath = path.join(__dirname, 'sampleTypeDef.graphql');\nconst outputPath = path.join(__dirname, 'output');\n\n// Ensure schema file exists for the generator\nfs.writeFileSync(schemaPath, schemaContent);\n\n// Ensure output directory exists\nif (!fs.existsSync(outputPath)) {\n  fs.mkdirSync(outputPath, { recursive: true });\n}\n\nasync function generateQueries() {\n  console.log('Generating GraphQL queries...');\n  await gqlg({\n    schemaFilePath: schemaPath,\n    destDirPath: outputPath,\n    depthLimit: 2,\n    includeDeprecatedFields: false,\n    includeCrossReferences: false\n  });\n  console.log(`Queries generated in ${outputPath}`);\n\n  // Example of requiring generated queries\n  const generated = require(outputPath);\n  console.log('\\nGenerated User Query:\\n', generated.queries.user);\n  console.log('\\nGenerated Posts Query:\\n', generated.queries.posts);\n\n  // Clean up generated files for subsequent runs (optional)\n  fs.rmSync(outputPath, { recursive: true, force: true });\n  fs.unlinkSync(schemaPath);\n}\n\ngenerateQueries().catch(console.error);","lang":"javascript","description":"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."},"warnings":[{"fix":"CLI: `gqlg --includeDeprecatedFields`. Programmatic: `gqlg({ ..., includeDeprecatedFields: true })`.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"CLI: `gqlg --includeCrossReferences`. Programmatic: `gqlg({ ..., includeCrossReferences: true })`.","message":"To 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The `schemaFilePath` provided to the `gql-generator` CLI or programmatic function does not point to an existing GraphQL schema file.","error":"Error: ENOENT: no such file or directory, open './schema.graphql'"},{"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.","cause":"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.","error":"ReferenceError: require is not defined in ES module scope"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"gql-generator","cli_version":null}