
Security News
AI Slop Is Polluting Bug Bounty Platforms with Fake Vulnerability Reports
AI-generated slop reports are making bug bounty triage harder, wasting maintainer time, and straining trust in vulnerability disclosure programs.
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.
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 queryignoreFields
option__args
__aliasFor
EnumType
__variables
@client
) via __directives
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
}
}
}
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
}
}
Pull requests welcome!
MIT
1.7.0
__aliasFor
option. The old __alias
syntax did not support more than one alias. Thanks @jeniffer9__alias
is now deprecated and will be removed in version 2.0.0FAQs
This is a simple module that takes a JavaScript object and turns it into a GraphQL query to be sent to a GraphQL server.
The npm package json-to-graphql-query receives a total of 32,538 weekly downloads. As such, json-to-graphql-query popularity was classified as popular.
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
AI-generated slop reports are making bug bounty triage harder, wasting maintainer time, and straining trust in vulnerability disclosure programs.
Research
Security News
The Socket Research team investigates a malicious Python package disguised as a Discord error logger that executes remote commands and exfiltrates data via a covert C2 channel.
Research
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.