Registry / devops / json-to-graphql-query

json-to-graphql-query

JSON →
library2.3.0jsnpmunverified

json-to-graphql-query converts a JavaScript object into a GraphQL query string. v2.3.0 is current stable; released 2021, maintenance mode. Supports queries, mutations, arguments, aliases, enums, variables, directives, and inline fragments. Differentiator: fully declarative object-to-query mapping without string interpolation. Ships TypeScript types.

npm install json-to-graphql-query
INSTALL
IMPORT
SIG · JSON-TO-GRAPHQL-QU
J
json-to-graphql-query
devopsjavascriptv2.3.0
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.

jsonToGraphQLQuery
import { jsonToGraphQLQuery } from 'json-to-graphql-query'
ESM with named export is the standard way since v2.
jsonToGraphQLQuery (default import)
import jsonToGraphQLQuery from 'json-to-graphql-query'
const jsonToGraphQLQuery = require('json-to-graphql-query')
Default import is also supported, but require() may break in ESM environments.
EnumType
import { EnumType } from 'json-to-graphql-query'
Named export for GraphQL enum values.

Builds a GraphQL query with arguments and an enum value from a plain object, then prints it.

import { jsonToGraphQLQuery, EnumType } from 'json-to-graphql-query'; const query = { query: { User: { __args: { id: 1 }, name: true, role: new EnumType('ADMIN'), posts: { title: true, } } } }; const graphqlString = jsonToGraphQLQuery(query, { pretty: true }); console.log(graphqlString); /* query { User (id: 1) { name role: ADMIN posts { title } } } */
Debug
Known issues
deprecatedThe `__all_on` field for inline fragments is deprecated in favor of `__on.__typeName`.
fix
Use `__on: { __typeName: 'FragmentName', ...fields }` instead of `__all_on: { ... }`.
affects: >=2.0.0
gotchaFalsy field values (false, 0, '') are omitted from the generated query unless `includeFalsyKeys` option is true.
fix
Add option `{ includeFalsyKeys: true }` to preserve fields with falsy values.
affects: >=1.0.0
gotchaNested object keys must be at least one level deep; a key set to `true` becomes a leaf field, not an object.
fix
Use an object with subfields for nested selections, e.g., `posts: { id: true }` instead of `posts: true`.
affects: >=1.0.0
breakingRemoved support for `__args` inside inline fragments in v2.0.0. Arguments on fragments are invalid GraphQL.
fix
Move `__args` to the parent field; fragments cannot have arguments.
affects: >=2.0.0
Errors
Common errors & fixes
jsonToGraphQLQuery is not a function
CommonJS require returns an object with a default property if the module uses ESM exports.
fix
Use `import { jsonToGraphQLQuery } from 'json-to-graphql-query'` or `const j = require('json-to-graphql-query'); j.default(...)`.
Cannot read property 'replace' of undefined
Passing a non-object (e.g., string or array) as the query parameter.
fix
Ensure the first argument to jsonToGraphQLQuery is a plain JavaScript object with a `query` or `mutation` key.
EnumType is not defined
Forgetting to import EnumType or using it as a class without instantiation.
fix
Import `{ EnumType }` from the package and use `new EnumType('VALUE')`.
Upgrade
Version history
2.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources