This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a TypeScript library that converts GraphQL query strings into JSON objects. It's the reverse of json-to-graphql-query. The main export is the graphQlQueryToJson function located in src/index.ts.
npm run build- Compile TypeScript to JavaScript in the dist/ directorynpm run watch- Build in watch mode for developmentnpm test- Run Jest tests on compiled JavaScript in dist/npm run test:coverage- Run tests with coverage reportingnpm run lintFull- Run Prettier and ESLint with auto-fix
The main graphQlQueryToJson function:
- Parses GraphQL queries using the
graphqllibrary'sparsefunction - Recursively processes the AST to extract selections, arguments, and variables
- Handles aliases, enums (via json-to-graphql-query), nested objects, and variable substitution
- Returns a JSON representation where field names map to
trueor nested objects, with arguments stored in__argsproperties
getSelections()- Processes selection sets recursivelygetArguments()- Handles argument processing including variables and enum typesreplaceVariables()- Substitutes variable placeholders with actual valuescheckEachVariableInQueryIsDefined()- Validates all query variables have values
- TypeScript source in
src/compiles to CommonJS indist/ - Tests run against compiled JavaScript, not source TypeScript
- Uses ES5 target with CommonJS modules for broad compatibility
- Comprehensive Jest test suite in
src/test/full_functionality.spec.ts - README examples validation tests in
src/test/readme_examples.spec.ts - Tests cover queries, mutations, subscriptions, aliases, enums, variables, scalar fields with arguments, float arguments (including edge cases), inline fragments, and error cases
- Tests run against compiled dist/ files, so always build before testing
- To run a single test file:
npm test -- --testNamePattern="specific test name" - Current coverage: 99% statements, 98% branches (line 111 in index.ts is unreachable dead code)
The library processes GraphQL Abstract Syntax Trees (AST) from the graphql library. Key AST node types handled:
OperationDefinition- Query/mutation/subscription operationsField- Individual field selections with optional arguments and aliasesInlineFragment- Inline fragments for conditional type-based field selectionArgument- Field arguments with various value types (string, int, float, enum, object, list, variable)SelectionSet- Groups of field selections
- Field Selection: Simple fields become
fieldName: true - Nested Objects: Objects with selections become
fieldName: { nestedField: true } - Arguments: Stored in
__argsproperty:fieldName: { __args: { argName: value } } - Aliases: Aliased fields get
__aliasFormetadata:alias: { __aliasFor: "originalField" } - Variables: Replaced with actual values using
replaceVariables()function - Enums: Wrapped in
EnumTypeobjects from json-to-graphql-query library (except in arrays where they remain strings) - Inline Fragments: Single fragment becomes
__on: { __typeName: "TypeName", ...fields }, multiple fragments become__on: [{ __typeName: "Type1", ...}, ...]
- Validates that all variables referenced in query are provided
- Throws descriptive errors for malformed GraphQL syntax
- Prevents multiple operations in single query string