A simple way to create and parse graphql query on node application
MIT License
Table of Contents
Note: V1 template will be deprecated soon. Use V2 instead
json-graphql-parser
is a modular and simpler way to write graph ql query from any node/javascript application. User doesn't have to know the complex structure of a graphql schema, rather its being driven by objects.
Quick example:
graphql syntax
query MyQuery {
users(limit: 5, offset: 5, where: {_and: {id: {_eq: "123"}}}, order_by: {created_at: asc}) {
id
name
user_like {
id
liked
}
description
}
}
json-graphql-parser syntax
display: 'fetch users',
name: 'Fetch_Users',
function: 'users',
return: [
"id",
"name",
{
"play_like": [
"liked",
"id"
]
},
"description"
],
orderBy: {
"created_at": "desc"
}
Note:
json-graphql-parser
was introduced keeping in mind to solve graphql parsing and understanding issue. It never been thought to replace too complex queries which are tough to maitain. If you have very complex queries, break it in multiple simple one. Handling very complex queries are in pipeline (check What next ) however not scoped or estimated yet
Visit https://jgpp.koustov.com/
npm install json-graphql-parser axios url
# or
yarn add json-graphql-parser axios url
Import ES6
// ES6
import { submit, submit_multi } from 'json-graphql-parser/v2/index.js';
// ES5
const { submit, submit_multi } = require("json-graphql-parser/v2/index.js");
Usage
submit(query_config, url, additional_header);
Below are the major parameters in a json-graphql-parser object
display: Optional | String : Give a display name for the query
name: Optional | String : Query name
function: Optional | String : Target graphql function name . Typical format of function name is <operation_type:mandatory>table:mandatorysuffix:optional.
Example:
return: Required | String Array: Array parameters to return
write: Optional | Boolean : Denotes if the given request is for a write or read operation. In other word mutation
object: Optional | Object: Value of a row when inserting and object
value: Optional | Object: Keyvalue paired object when its intended to update specific property of a row
where: Optional | Object: Contains an object structure to denote the where clause.
Format
{
where: {
clause {
class: "[Optional | String] Target class name",
operator: "[Optional | String] Operator type (or/and)",
conditions: [{
field: "[Optional | String]: Field in question",
operator: "[Optional | String] Operator (eq | ne | in | ilike | <any graphql operator>)",
value: "[Optional | String] Value to match",
type: "[Optional | String] Type of the value Required only when one wants to pass string value explicitly,
class: "[Optional | String] Target class name",
clause: "[Optional | Object] More recursive conditions"
}],
}
},
}
orderBy: Optional | Object : Inform the server what would be ordering mechanism on returned data Format: {<attribute_name>: <order direction: asc|desc>}
offset: Optional | Number: Offset record number. In other word skip the recird by.
limit: Optional | Number: Limit the return count. offset
and limit
together can form pagination mechanism
Basic Select Query
display: 'Fetch user ids',
name: 'Fetch_users',
function: 'users',
return: [
"id"
]
Basic Select Query with order by
display: 'Fetch user ids',
name: 'Fetch_users',
function: 'users',
return: [
"id"
],
orderBy: {
"created_at": "desc"
}
Basic Select Query with order by and clause
{
clause:{
operator: 'and',
conditions: [
{
field: 'country',
operator: 'eq',
value: "india",
type: 'string'
}
]
}
}
display: 'Fetch user ids',
name: 'Fetch_users',
function: 'users',
return: [
"id"
],
orderBy: {
"created_at": "desc"
},
where: {
clause:{
operator: 'and',
conditions: [
{
field: 'country',
operator: 'eq',
value: "india",
type: 'string'
}
]
}
}
Basic Select Query with order by and multiple clauses
{
clause:{
operator: 'and',
conditions: [
{
field: 'country',
operator: 'eq',
value: "india",
type: 'string'
},
{
field: 'department',
operator: 'eq',
value: "finance",
type: 'string'
}
]
}
}
display: 'Fetch user ids',
name: 'Fetch_users',
function: 'users',
return: [
"id"
],
orderBy: {
"created_at": "desc"
},
where: {
clause:{
operator: 'and',
conditions: [
{
field: 'country',
operator: 'eq',
value: "india",
type: 'string'
},
{
field: 'department',
operator: 'eq',
value: "finance",
type: 'string'
}
]
}
}
Note: In
conditions
, you could either usefield
,operator
andvalue
properties or you can nest one level down usingclause
Insert Query
display: 'Insert user',
name: 'Insert user',
write: true,
function: 'insert_users_one',
object: {
name: 'Jhn Doe',
department: 'finance',
country: 'us'
}
return: [
"affected_rows"
]
Update Query
display: 'Insert user',
name: 'Insert user',
function: 'insert_users_one',
write: true,
value: {
country: 'india'
}
where: {
clause:{
operator: 'and',
conditions: [
{
field: 'name',
operator: 'eq',
value: "John Doe",
type: 'string'
}
]
}
}
return: [
"affected_rows"
]
A bunch of examples have been given under queries from an outstanding open-source application called Reactplay
Check the object schema here
Feel free to create issue and make pull request
Refer Code of Conduct
Refer Contributing
MIT © Koustov