
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
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
The npm package graphql-client-utils receives a total of 2 weekly downloads. As such, graphql-client-utils popularity was classified as not popular.
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
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.