
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
generate-gql
Advanced tools
Generating GraphQL code from JavaScript object.
# with pnpm
pnpm i generate-gql
# or with npm
npm i generate-gql
import generateGQL from 'generate-gql'
const gql = generateGQL({
/**
* Operation type, can be 'query', 'mutation' or 'subscription'.
*
* Default: 'query'.
*/
type: 'query',
/**
* The operation body.
*/
body: {
users: {
/**
* Optional alias of the field.
*/
alias: 'results',
/**
* Optional arguments. If not set, no arguments will be generated.
*/
args: {
first: 10,
filter: {
/**
* String values will be enclosed in double quotes.
*/
nameContains: 'j',
/**
* Enum value is an object with only one key named "$enum".
* The enum value will not be enclosed in double quotes.
*/
gender: { $enum: 'Male' },
/**
* Object can be nested.
*/
createdAt: {
after: '2020-01-01T00:00:00.000Z',
before: '2023-01-01T00:00:00.000Z'
},
/**
* The fields with the value of `null` or `undefined` will
* be ignored. If all fields of an object are ignored, the
* object will also be ignored.
*/
hasFriendsWith: {
nameContains: 'j',
gender: null,
createdAt: {
after: null,
before: null
},
hasFriendsWith: undefined
}
}
},
/**
* Optional fields.
* If it is not an object or it is an empty object,
* nothing will be generated.
*/
fields: {
edges: {
/**
* If the value type is `number` or `boolean` and the value is truthy,
* the field will be generated. If the value is falsy, the field will
* be skipped.
*/
name: true,
gender: 1,
/**
* If the value is a string and it is not an empty string,
* the string will be used as the alias of this field.
* If the value is an empty string, the field will be skipped.
*/
lastName: 'familyName',
/**
* Object can be nested.
*/
friends: {
name: true,
gender: true
},
/**
* The value can be an array with only one element.
* The element is used to describe the field.
* You can set the alias, arguments or sub-fields
* of this field here.
*/
articles: [
{
/**
* Optional field alias.
*/
alias: 'top5articles',
/**
* Optional arguments.
*/
args: {
first: 5,
orderBy: {
field: 'totalLike',
order: { $enum: 'DESC' }
}
},
/**
* Optional sub-fields.
*/
fields: {
title: true,
summary: true
}
}
]
}
}
}
}
})
console.log(gql)
The output is:
query {
results: users (
first: 10
filter: {
nameContains: "j"
gender: Male
createdAt: {
after: "2020-01-01T00:00:00.000Z"
before: "2023-01-01T00:00:00.000Z"
}
hasFriendsWith: {
nameContains: "j"
}
}
) {
edges {
name
gender
familyName: lastName
friends {
name
gender
}
top5articles: articles (
first: 5
orderBy: {
field: "totalLike"
order: DESC
}
) {
title
summary
}
}
}
}
FAQs
Generating GraphQL code from JavaScript object.
We found that generate-gql 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.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.