Registry / developer-tools / graphql-query-to-json

graphql-query-to-json

JSON →
library2.0.6jsnpmunverified

Converts GraphQL query, mutation, and subscription strings into structured JSON objects, enabling programmatic manipulation and analysis of GraphQL queries. This TypeScript library (v2.0.6, actively maintained on GitHub) is the reverse of json-to-graphql-query. Key differentiators: full GraphQL support including variables, arguments, aliases, inline fragments, enums, and type safety with comprehensive TypeScript definitions. It provides predictable transformation rules and descriptive error handling. Active development, regular releases on npm.

npm install graphql-query-to-json
INSTALL
IMPORT
SIG · GRAPHQL-QUERY-TO-J
G
graphql-query-to-json
developer-toolsjavascriptv2.0.6
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

graphQlQueryToJson
import { graphQlQueryToJson } from 'graphql-query-to-json'
import graphQlQueryToJson from 'graphql-query-to-json'
Named export; default import does not exist.
QueryObject
import type { QueryObject } from 'graphql-query-to-json'
import { QueryObject } from 'graphql-query-to-json'
Use type import for TypeScript to avoid runtime import.
Options
import type { Options } from 'graphql-query-to-json'
import { Options } from 'graphql-query-to-json'
Options is a type, not a value; use type import.

Converts a GraphQL query with a variable into a JSON object. Requires the graphQlQueryToJson function and a variables object.

import { graphQlQueryToJson } from 'graphql-query-to-json'; const query = ` query GetUser($id: ID!) { user(id: $id) { name email profile { bio } } } `; const variables = { id: '123' }; const result = graphQlQueryToJson(query, { variables }); console.log(JSON.stringify(result, null, 2));
Debug
Known issues
gotchaEnums are wrapped in an object with a 'value' property (e.g., { value: 'ACTIVE' }), not represented as strings or enum values directly.
fix
Access enum value via result.enumField.value, not result.enumField.
affects: >=2.0.0
gotchaVariables must be passed in the options object; omitting variables when the query uses them throws an error.
fix
Ensure you provide a variables object matching the query variable definitions.
affects: >=1.0.0
deprecatedThe function signature previously accepted (query, variables, options) but now accepts (query, options) where options includes variables.
fix
Pass variables as part of the options object: graphQlQueryToJson(query, { variables }).
affects: <2.0.0
Errors
Common errors & fixes
Error: Variable "$id" of type "ID!" is required but not provided.
Missing required variable in the options.variables object.
fix
Add the missing variable: graphQlQueryToJson(query, { variables: { id: '123' } })
TypeError: graphQlQueryToJson is not a function
Using default import instead of named import.
fix
Use import { graphQlQueryToJson } from 'graphql-query-to-json' (named import).
Upgrade
Version history
2.0.6latest on npm
Audit
Dependencies
typescriptoptionalTypeScript type definitions shipped with the package
Agent activity
41 hits · last 30 days
node
36
Resources
graphql-query-to-json — npm install graphql-query-to-json · libregistry