
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
graphql-client-utils
Advanced tools
This is a small collection of utilities for generating GraphQL queries:
renderQuery
: Renders a GraphQL query from a structured query objectfieldsToQuery
: Converts a list of dot-syntax nested fields or a list of lists of nested fields to a structured query projectionpruneUndefined
: Recursively removes any keys that have a value undefined
npm install graphql-client-utils
const graphqlUtils = require('graphql-client-utils')
Generates a GraphQL query from a structured JSON object. Support field projection, arguments & aliases.
> obj = {a: true, b: true};
> graphqlUtils.renderQuery(obj);
'{ a b }'
Use a value of true
.
> obj = {
a: true,
b: true,
c: true
}
> graphqlUtils.renderQuery(obj)
'{ a b c }'
Use a value of a nested object.
> obj = {
a: {
b: {
c: true,
d: true
},
e: true
}
}
> graphqlUtils.renderQuery(obj)
'{ a { b { c d } e } }'
Specify field arguments via _
as kvp. Specify an alias name as the key and @
as the field to alias. In this case field a
is aliased to myfield
with the specified args. If there are no other keys in the obj, it will be treated as a leaf field.
> obj = {
myfield: {
_: {arg1: 'foo', arg2: 'bar'},
'@': 'a'
}
}
> graphqlUtils.renderQuery(obj)
'{ myfield: a(arg1: "foo", arg2: "bar") }'
Combine all of the above to have arbitrarily nested projections, arguments and aliases
> obj = {
myfield: {
_: {arg1: 'foo', arg2: 'bar'},
'@': 'a',
b: true,
c: {
_: {arg: 'baz'},
d: true
}
}
}
> graphqlUtils.renderQuery(obj)
'{ myfield: a(arg1: "foo", arg2: "bar") { b c(arg: "baz") { d } } }'
Converts a list of fields to a structured query object. Fields can use dot-syntax for nesting or be an array of nested field names. Nested fields are grouped hierarchically.
Convert a list of fields to a projection.
> graphqlUtils.fieldsToQuery(['a','b','c'])
{ a: true, b: true, c: true }
Convert a list of nested fields to a projection using the dot-syntax.
> graphqlUtils.fieldsToQuery(['a.b.c', 'a.b.d', 'a.e'])
{ a: { b: { c: true, d: true }, e: true } }
Convert a list of nested fields to a projection using the array-syntax.
> graphqlUtils.fieldsToQuery([['a','b','c'], ['a','b','d'], ['a','e']])
{ a: { b: { c: true, d: true }, e: true } }
The two syntaxes can be mixed as well.
> graphqlUtils.fieldsToQuery(['a.b.c', ['a','b','d'], 'a.e'])
{ a: { b: { c: true, d: true }, e: true } }
It is useful to combine renderQuery
and fieldsToQuery
in the following manner to build arbitrarily complex queries.
> const fields = ['b.c', 'b.d', 'e'];
> const obj = {
a: _.extend(
{_: {arg1: 'foo', arg2: 'bar'}},
graphqlUtils.fieldsToQuery(fields)
)
}
> graphqlUtils.renderQuery(obj)
'{ a(arg1: "foo", arg2: "bar") { b { c d } e } }'
docker build -t graphql-client-utils .
docker run --rm -it -v `pwd`:/opt/graphql-client-utils -v /opt/graphql-client-utils/node_modules graphql-client-utils
npm run test
npm version x.y.z -m 'release notes'
npm publish
FAQs
Utilities for generating GraphQL queries
We found that graphql-client-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.