graphql-query-builder-v2
Since graphql-query-builder seems to no longer be actively maintained, I have forked it. Nothing much has changed except the ability to use null
and undefined
values in the arguments. You are also allowed to not execute .find
API is unchanged.
Working examples in v2
let query = "functionName" name: null
Output
functionName( name: null )
With .find
let query = "functionName" name: null query
Output
{ _id name}
With enums
let query = "functionName" status: QueryBuilder
Output
Combining to create graphql-upload example
As I have recently started using graphql-upload, I am sharing how this package can also be used to create a valid FormData query.
const QueryBuilder = ; // The wrapping "mutation"let mutation = "mutation";mutation; // Nested data within actual_queryconst nested = data1: "stuff" image: QueryBuilder;// The actual query we want to runlet actual_query = "imageUpload";actual_query;// putting the actual query into the wrapping mutationmutation; let form_data = ; const operations = query: mutation_query variables: image: null ;form_data; const map = "0": "variables.image";form_data; form_data; // image_file is variable that is holding your File/Blob
Output
mutation output
mutation($image: Upload) {
imageUpload(
_id: 1,
display_name: "example",
nested: {
data1: "stuff",
image: $image
})
}
Install
Use this instead of the install below.
npm install graphql-query-builder-v2
graphql-query-builder
a simple but powerful graphQL query builder
info:
tests:
quality:
★ it on github
If this was helpful,Demo / Sandbox 👍
Install
npm install graphql-query-builder
Api
const Query = ;
constructor
query/mutator you wish to use, and an alias or filter arguments.
Argument (one to two) | Description |
---|---|
String | the name of the query function |
* String / Object | (optional) This can be an alias or filter values |
let profilePicture = "profilePicture"size : 50;
setAlias
set an alias for this result.
Argument | Description |
---|---|
String | The alias for this result |
profilePicture;
filter
the parameters to run the query against.
Argument | Description |
---|---|
Object | An object mapping attribute to values |
profilePicture;
find
outlines the properties you wish to be returned from the query.
Argument (one to many) | Description |
---|---|
String or Object | representing each attribute you want Returned |
... | same as above |
profilePicture;
toString
return to the formatted query string
// A (ES6)``;// BprofilePicture+'';// CprofilePicture;
run samples
node example/simple.js
Example
var Query = ;// example of nesting Queryslet profilePicture = "profilePicture"size : 50;profilePicture;let user = "user"id : 123;userconsole/*user( id:123 ) {id,nickname : name,isViewerFriend,image : profilePicture( size:50 ) {uri,width,height}}*/// And another examplelet MessageRequest = type:"chat" message:"yoyo"user:name:"bob"screen:height:1080width:1920friends:id:1name:"ann"id:2name:"tom";let MessageQuery = "Message""myPost";MessageQuery;MessageQuery;console;/*myPost:Message( type:"chat",message:"yoyo",user:{name:"bob",screen:{height:1080,width:1920}},friends:[{id:1,name:"ann"},{id:2,name:"tom"}]){messageId : id,postedTime : createTime}*/// Simple nestinglet user = "user";user/*user {profilePicture {uri,width,height}}*/// Simple nesting with renamelet user = "user";user/*user {image : profilePicture {uri,width,height}}*/