
Security News
CISA Extends MITRE Contract as Crisis Accelerates Alternative CVE Coordination Efforts
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
json-to-graphql-query
Advanced tools
This is a simple module that takes a JavaScript object and turns it into a GraphQL query to be sent to a GraphQL server.
This is a simple module that takes a JavaScript object and turns it into a GraphQL query to be sent to a GraphQL server.
Mainly useful for applications that need to generate graphql queries dynamically.
Huge thanks to @vkolgi for agreeing to take over maintenance of this library! I look forward to following its continued development. - @dupski
npm install json-to-graphql-query
const query = jsonToGraphQLQuery(queryObject: object, options?: object);
Supported Options:
pretty
- boolean - optional - set to true
to enable pretty-printed outputignoreFields
- string[] - optional - you can pass an array of object key names that you want removed from the queryincludeFalsyKeys
- boolean - optional - disable the default behaviour if excluding keys with a falsy valueignoreFields
option__args
__aliasFor
EnumType
__variables
@client
) via __directives
__on.__typeName
__all_on
__name
See the CHANGELOG
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
id: true,
title: true,
post_date: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
id
title
post_date
}
}
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
__args: {
where: { id: 2 }
orderBy: 'post_date'
},
id: true,
title: true,
post_date: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts (where: {id: 2}, orderBy: "post_date") {
id
title
post_date
}
}
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
id: true,
title: true,
comments: {
id: true,
comment: true,
user: true
}
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
id
title
comments {
id
comment
user
}
}
}
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
id: true,
title: false,
comments: {
id: true,
comment: false,
user: true
}
},
User: false
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
id
comments {
id
user
}
}
}
NOTE: You can tell jsonToGraphQLQuery() not to exclude keys with a falsy value
by setting the includeFalsyKeys
option.
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
allPosts: {
__aliasFor: 'Posts',
id: true,
comments: {
id: true,
comment: true
}
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
allPosts: Posts {
id
comments {
id
comment
}
}
}
import { jsonToGraphQLQuery, EnumType } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
__args: {
orderBy: 'post_date',
status: new EnumType('PUBLISHED')
},
title: true,
body: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts (orderBy: "post_date", status: PUBLISHED) {
title
body
}
}
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
const query = {
query: {
__variables: {
variable1: 'String!',
variableWithDefault: 'String = "default_value"'
},
Posts: {
__args: {
arg1: 20,
arg2: new VariableType('variable1')
},
id: true,
title: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query ($variable1: String!, $variableWithDefault: String = "default_value") {
Posts (arg1: 20, arg2: $variable1) {
id
title
}
}
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
__directives: {
client: true
}
Posts: {
id: true,
title: true,
post_date: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts @client {
id
title
post_date
}
}
We sometimes want to ignore specific fields in the initial object, for instance __typename in Apollo queries.
You can specify these fields using the ignoreFields
option:
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
shouldBeIgnored: {
variable1: 'a value'
},
id: true,
title: true,
post_date: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, {
pretty: true,
ignoreFields: ['shouldBeIgnored']
});
Resulting graphql_query
query {
Posts {
id
title
post_date
}
}
Full inline fragments
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
title: true,
__all_on: [
"ConfigurablePost",
"PageInfo"
]
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
title
...ConfigurablePost
...PageInfo
}
}
Partial inline fragments
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
title: true,
__on: {
__typeName: "ConfigurablePost",
id: true
}
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
title
... on ConfigurablePost {
id
}
}
}
import { jsonToGraphQLQuery } from 'json-to-graphql-query';
const query = {
query: {
Posts: {
__on: [
{
__typeName: "ConfigurablePost",
id: true
},
{
__typeName: "UnconfigurablePost",
name: true
}]
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query {
Posts {
title
... on ConfigurablePost {
id
}
... on UnconfigurablePost {
name
}
}
}
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
const query = {
query: {
__name: 'NewName',
__variables: {
variable1: 'String!',
variableWithDefault: 'String = "default_value"'
},
Posts: {
__args: {
arg1: 20,
arg2: new VariableType('variable1')
},
id: true,
title: true
}
}
};
const graphql_query = jsonToGraphQLQuery(query, { pretty: true });
Resulting graphql_query
query NewName ($variable1: String!, $variableWithDefault: String = "default_value") {
Posts (arg1: 20, arg2: $variable1) {
id
title
}
}
import { jsonToGraphQLQuery, VariableType } from 'json-to-graphql-query';
const mutation = {
mutation: {
delete_post: {
__args: { id: 1234 },
id: true,
}
}
};
const graphql_query = jsonToGraphQLQuery(mutation, { pretty: true });
Resulting graphql_query
mutation {
delete_post (id: 1234) {
id
}
}
Pull requests welcome!
MIT
2.2.5
FAQs
This is a simple module that takes a JavaScript object and turns it into a GraphQL query to be sent to a GraphQL server.
We found that json-to-graphql-query demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
CISA extended MITRE’s CVE contract by 11 months, avoiding a shutdown but leaving long-term governance and coordination issues unresolved.
Product
Socket's Rubygems ecosystem support is moving from beta to GA, featuring enhanced security scanning to detect supply chain threats beyond traditional CVEs in your Ruby dependencies.
Research
The Socket Research Team investigates a malicious npm package that appears to be an Advcash integration but triggers a reverse shell during payment success, targeting servers handling transactions.