0.0.1-alpha.8 • Published 8 months ago
generate-query v0.0.1-alpha.8
generate-query
Generate GraphQL queries from JavaScript objects.
This module was originally designed to be used with the generate-graphql-code module. It can generate TypeScript code for you from your GraphQL endpoints.
Installation
# with pnpm
pnpm i generate-query
# or with npm
npm i generate-query
Example
import generateQuery from 'generate-query'
const query = generateQuery({
/**
* 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: {
/**
* 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',
direction: { $enum: 'DESC' }
}
},
/**
* Optional sub-fields.
*/
fields: {
title: true,
summary: true
}
}
]
}
}
}
})
console.log(query)
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"
}
}
) {
name
gender
familyName: lastName
friends {
name
gender
}
top5articles: articles (
first: 5
orderBy: {
field: "totalLike"
direction: DESC
}
) {
title
summary
}
}
}
0.0.1-alpha.8
8 months ago
0.0.1-alpha.7
8 months ago
0.0.1-alpha.6
8 months ago
0.0.1-alpha.5
8 months ago
0.0.1-alpha.4
8 months ago
0.0.1-alpha.3
8 months ago
0.0.1-alpha.2
8 months ago
0.0.1-alpha.1
8 months ago