graphql-query-builder
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -23,3 +23,3 @@ var Query = require('../'); | ||
let user = new Query("user",{id : 3500401}); | ||
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]) | ||
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]); | ||
@@ -51,2 +51,2 @@ logger.log("user",user+""); | ||
console.log(FetchLeeAndSam.find(lee,sam)+"") | ||
console.log(FetchLeeAndSam.find(lee,sam)+""); |
63
index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -26,3 +26,3 @@ //===================================================== | ||
return `${propS} : ${item} `; | ||
} else if ( "string" === typeof itemX ) { | ||
} else if ( "string" === typeof itemX ) { | ||
return itemX; | ||
@@ -38,2 +38,44 @@ } else { | ||
//===================================================== | ||
//=================================== get GraphQL Value | ||
//===================================================== | ||
function getGraphQLValue(value) { | ||
if ("string" === typeof value) { | ||
value = JSON.stringify(value); | ||
} else if (Array.isArray(value)) { | ||
value = value.map(item => { | ||
return getGraphQLValue(item); | ||
}).join(); | ||
value = `[${value}]`; | ||
} else if ("object" === typeof value) { | ||
/*if (value.toSource) | ||
value = value.toSource().slice(2,-2); | ||
else*/ | ||
value = objectToString(value); | ||
//console.error("No toSource!!",value); | ||
} | ||
return value; | ||
} | ||
function objectToString(obj) { | ||
let sourceA = []; | ||
for(let prop in obj){ | ||
if ("function" === typeof obj[prop]) { | ||
continue; | ||
} | ||
// if ("object" === typeof obj[prop]) { | ||
sourceA.push(`${prop}:${getGraphQLValue(obj[prop])}`); | ||
// } else { | ||
// sourceA.push(`${prop}:${obj[prop]}`); | ||
// } | ||
} | ||
return `{${sourceA.join()}}`; | ||
} | ||
//===================================================== | ||
//========================================= Query Class | ||
@@ -50,3 +92,10 @@ //===================================================== | ||
for(let propS in filtersO){ | ||
this.headA.push( `${propS}:${("string" === typeof filtersO[propS])?JSON.stringify(filtersO[propS]):filtersO[propS]}` ); | ||
if ("function" === typeof filtersO[propS]) { | ||
continue; | ||
} | ||
let val = getGraphQLValue(filtersO[propS]); | ||
if ("{}" === val) { | ||
continue; | ||
} | ||
this.headA.push( `${propS}:${val}` ); | ||
} | ||
@@ -68,3 +117,3 @@ return this; | ||
this.aliasS = _aliasS; | ||
return this | ||
return this; | ||
}; | ||
@@ -80,4 +129,4 @@ | ||
return this; | ||
} | ||
}; | ||
}; | ||
} | ||
@@ -97,4 +146,4 @@ //===================================================== | ||
} | ||
} | ||
}; | ||
module.exports = Query; |
{ | ||
"name": "graphql-query-builder", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "a simple but powerful graphQL query builder", | ||
@@ -13,5 +13,5 @@ "main": "index.js", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.11", | ||
"coveralls": "^2.11.12", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^2.5.3" | ||
"mocha": "^3.0.1" | ||
}, | ||
@@ -32,3 +32,4 @@ "scripts": { | ||
"generator", | ||
"orm" | ||
"orm", | ||
"queries" | ||
], | ||
@@ -35,0 +36,0 @@ "author": { |
@@ -7,5 +7,8 @@ # graphql-query-builder | ||
[![Coverage Status](https://coveralls.io/repos/github/codemeasandwich/graphql-query-builder/badge.svg?branch=master)](https://coveralls.io/github/codemeasandwich/graphql-query-builder?branch=master) | ||
[![bitHound Overall Score](https://www.bithound.io/github/codemeasandwich/graphql-query-builder/badges/score.svg)](https://www.bithound.io/github/codemeasandwich/graphql-query-builder) | ||
a simple but powerful graphQL query builder | ||
### [Demo](https://tonicdev.com/codemeasandwich/57a0727c80254315001cb366) | ||
# Install | ||
@@ -48,3 +51,36 @@ | ||
// And another example | ||
let MessageRequest = { type:"chat", message:"yoyo", | ||
user:{ | ||
name:"bob", | ||
screen:{ | ||
height:1080, | ||
width:1920 | ||
} | ||
}, | ||
friends:[ | ||
{id:1,name:"ann"}, | ||
{id:2,name:"tom"} | ||
] | ||
}; | ||
let MessageQuery = new Query("Message","myPost"); | ||
MessageQuery.filter(MessageRequest); | ||
MessageQuery.find({ messageId : "id"}, {postedTime : "createTime" }); | ||
console.log(MessageQuery); | ||
/* | ||
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 | ||
} | ||
*/ | ||
``` | ||
137
test.js
@@ -1,2 +0,2 @@ | ||
"use strict" | ||
"use strict"; | ||
var expect = require('chai').expect; | ||
@@ -14,6 +14,6 @@ var Query = require('./index'); | ||
let expeted = `user{name}`; | ||
let user = new Query("user").find("name") | ||
let user = new Query("user").find("name"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
@@ -23,6 +23,6 @@ it('should create a Query with function name & alia', function(){ | ||
let expeted = `sam : user{name}`; | ||
let user = new Query("user","sam").find("name") | ||
let user = new Query("user","sam").find("name"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
@@ -32,6 +32,6 @@ it('should create a Query with function name & input', function(){ | ||
let expeted = `user(id:12345){name}`; | ||
let user = new Query("user",{id : 12345}).find("name") | ||
let user = new Query("user",{id : 12345}).find("name"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
@@ -41,20 +41,20 @@ it('should create a Query with function name & input(s)', function(){ | ||
let expeted = `user(id:12345, age:34){name}`; | ||
let user = new Query("user",{id : 12345, age:34}).find("name") | ||
let user = new Query("user",{id : 12345, age:34}).find("name"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
it('should accept a single find value', function(){ | ||
let expeted = `user{name}`; | ||
let user = new Query("user").find("name") | ||
let user = new Query("user").find("name"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
it('should accept a single find value with alia', function(){ | ||
let expeted = `user{nickname:name}`; | ||
let user = new Query("user").find({nickname:"name"}) | ||
let user = new Query("user").find({nickname:"name"}); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
@@ -66,10 +66,10 @@ it('should accept a multiple find values', function(){ | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
it('should accept an array find values', function(){ | ||
let expeted = `user{firstname, lastname}`; | ||
let user = new Query("user").find(["firstname","lastname"]) | ||
let user = new Query("user").find(["firstname","lastname"]); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
@@ -79,9 +79,5 @@ it('should work with nesting Querys', function(){ | ||
let expeted = `user( id:12345 ) { | ||
id, | ||
nickname : name, | ||
isViewerFriend, | ||
id, nickname : name, isViewerFriend, | ||
image : profilePicture( size:50 ) { | ||
uri, width, height | ||
} | ||
}`; | ||
uri, width, height } }`; | ||
@@ -92,17 +88,11 @@ let profilePicture = new Query("profilePicture",{size : 50}); | ||
let user = new Query("user",{id : 12345}); | ||
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]) | ||
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(user)); | ||
}) | ||
}); | ||
it('should be able to group Querys', function(){ | ||
let expeted = `FetchLeeAndSam { | ||
lee: user(id: "1") { | ||
name | ||
}, | ||
sam: user(id: "2") { | ||
name | ||
} | ||
}`; | ||
let expeted = `FetchLeeAndSam { lee: user(id: "1") { name }, | ||
sam: user(id: "2") { name } }`; | ||
@@ -122,23 +112,87 @@ let FetchLeeAndSam = new Query("FetchLeeAndSam"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(FetchLeeAndSam)); | ||
}) | ||
}); | ||
it('should work with nasted objects and lists', function(){ | ||
let expeted =`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 }`; | ||
let MessageRequest = { type:"chat", | ||
message:"yoyo", | ||
user:{ name:"bob", | ||
screen:{ height:1080, width:1920 } }, | ||
friends:[ {id:1,name:"ann"}, {id:2,name:"tom"} ] | ||
}; | ||
let MessageQuery = new Query("Message","myPost"); | ||
MessageQuery.filter(MessageRequest); | ||
MessageQuery.find({ messageId : "id"}, {postedTime : "createTime" }); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(MessageQuery)); | ||
}); | ||
it('should work with objects that have help functions(will skip function name)', function(){ | ||
let expeted ='inventory(toy:"jack in the box") { id }'; | ||
let ChildsToy = { toy:"jack in the box", getState:function(){ } }; | ||
ChildsToy.getState();//for istanbul(coverage) to say all fn was called | ||
let ItemQuery = new Query("inventory",ChildsToy); | ||
ItemQuery.find("id"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery)); | ||
}); | ||
it('should work with nasted objects that have help functions(will skip function name)', function(){ | ||
let expeted ='inventory(toy:"jack in the box") { id }'; | ||
let ChildsToy = { toy:"jack in the box", utils: { getState:function(){ } } }; | ||
ChildsToy.utils.getState();//for istanbul(coverage) to say all fn was called | ||
let ItemQuery = new Query("inventory",ChildsToy); | ||
ItemQuery.find("id"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery)); | ||
}); | ||
it('should skip empty objects in filter/args', function(){ | ||
let expeted ='inventory(toy:"jack in the box") { id }'; | ||
let ChildsToy = { toy:"jack in the box", utils: { } }; | ||
let ItemQuery = new Query("inventory",ChildsToy); | ||
ItemQuery.find("id"); | ||
expect(removeSpaces(expeted)).to.equal(removeSpaces(ItemQuery)); | ||
}); | ||
it('should throw Error if find input items have zero props', function(){ | ||
expect(() => new Query("x").find({})).to.throw(Error); | ||
}) | ||
}); | ||
it('should throw Error if find input items have multiple props', function(){ | ||
expect(() => new Query("x").find({a:"z",b:"y"})).to.throw(Error); | ||
}) | ||
}); | ||
it('should throw Error if find is undefined', function(){ | ||
expect(() => new Query("x").find()).to.throw(Error); | ||
}) | ||
}); | ||
it('should throw Error if no find values have been set', function(){ | ||
expect(() => `${new Query("x")}`).to.throw(Error); | ||
}) | ||
}); | ||
it('should throw Error if find is not valid', function(){ | ||
expect(() => new Query("x").find(123)).to.throw(Error); | ||
}) | ||
}); | ||
@@ -148,8 +202,7 @@ it('should throw Error if you accidentally pass an undefined', function(){ | ||
expect(() => new Query("x",alia)).to.throw(Error); | ||
}) | ||
}); | ||
it('should throw Error it is not an input object for alias', function(){ | ||
expect(() => new Query("x",true)).to.throw(Error); | ||
}) | ||
}); | ||
}); | ||
}); |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
17039
287
85
0