Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 122 additions & 123 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,23 @@ a simple but powerful graphQL query builder
### If this was helpful, [★ it on github](https://github.com/codemeasandwich/graphql-query-builder)

*tested on [**NodeJS**](https://nodejs.org) and [**Webpack**](https://webpack.github.io)*


## [Demo / Sandbox](https://tonicdev.com/codemeasandwich/57a0727c80254315001cb366) :thumbsup:

# Install

`npm install graphql-query-builder`

# Api
## API

``` js
const Query = require('graphql-query-builder');
const {Query, Mutation, Nested} = require('graphql-query-builder');
```

### constructor
query/mutator you wish to use, and an alias or filter arguments.
Query or Mutator you wish to use, and an alias or filter arguments.

| Argument (**one** to **two**) | Description
| Argument | Description
|--- |---
| String | the name of the query function
| * String / Object | (**optional**) This can be an `alias` or `filter` values
| String or Object | (**optional**) This can be an `alias` or `filter` values

``` js
let profilePicture = new Query("profilePicture",{size : 50});
```
const profilePicture = new Query('profilePicture', {size: 50});
```

### setAlias
set an alias for this result.
Expand All @@ -60,8 +52,8 @@ set an alias for this result.
| String | The alias for this result

``` js
profilePicture.setAlias("MyPic");
```
profilePicture.setAlias('MyPic');
```

### filter
the parameters to run the query against.
Expand All @@ -71,126 +63,133 @@ the parameters to run the query against.
| Object | An object mapping attribute to values

``` js
profilePicture.filter({ height : 200, width : 200});
```
profilePicture.filter({ height: 200, width: 200});
```

### find
### select
outlines the properties you wish to be returned from the query.

| Argument (**one** to **many**) | Description
| Argument | Description
|--- |---
| String or Object | representing each attribute you want Returned
| ... | *same as above*
| String or Object or Array | representing each attribute you want Returned

``` js
profilePicture.find( { link : "uri"}, "width", "height");
```
```javascript
profilePicture.select({link: 'uri'}, 'width', 'height');
```

### toString
return to the formatted query string

``` js
// A (ES6)
`${profilePicture}`;
// B
profilePicture+'';
// C
```javascript
profilePicture.toString();
```
```

## Examples

## run samples
### Nested Query Example

``` bash
node example/simple.js
```javascript
const profilePicture = new Nested('profilePicture', {size: 50});
profilePicture.select('uri', 'width', 'height');

const user = new Query('user', {id: 123});
user.select(['id', {nickname: 'name'}, 'isViewerFriend', {image: profilePicture}]);

/*
query {
user( id:123 ) {
id,
nickname: name,
isViewerFriend,
image: profilePicture( size: 50 ) {
uri,
width,
height
}
}
}
*/
```

# Example

``` js
var Query = require('graphql-query-builder');

// example of nesting Querys
let profilePicture = new Query("profilePicture",{size : 50});
profilePicture.find( "uri", "width", "height");

let user = new Query("user",{id : 123});
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}])

console.log(user)
/*
user( id:123 ) {
id,
nickname : name,
isViewerFriend,

image : profilePicture( size:50 ) {
uri,
width,
height
### Mutation Example

```javascript
const MessageRequest = {
type: 'chat',
message: 'yoyo',
user:{
name: 'bob',
screen:{
height: 1080,
width: 1920
}
},
friends: [
{id: 1, name: 'ann'},
{id: 2, name: 'tom'}
]
};

const MessageQuery = new Mutation('Message', 'myPost');
MessageQuery.filter(MessageRequest);
MessageQuery.select({messageId: 'id'}, {postedTime: 'createTime' });

/*
mutation {
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
}
}
}
*/

// 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
*/
```

### Nested select Query

```javascript
const user = new Query('user');
user.select([{profilePicture: ['uri', 'width', 'height']}]);

/*
query {
user {
profilePicture {
uri,
width,
height
}
*/

// Simple nesting

let user = new Query("user");
user.find([{"profilePicture":["uri", "width", "height"]}])

/*
user {
profilePicture {
uri,
width,
height
}
}
*/

// Simple nesting with rename

let user = new Query("user");
user.find([{"image":{"profilePicture":["uri", "width", "height"]}}])

/*
user {
image : profilePicture {
uri,
width,
height
}
}
*/
}
}
*/
```

### Nested select query with rename

```javascript
const user = new Query('user');
user.select([{image: {profilePicture: ['uri', 'width', 'height']}}]);

/*
query {
user {
image: profilePicture {
uri,
width,
height
}
}
}
*/
```
92 changes: 56 additions & 36 deletions example/simple.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
var Query = require('../');

const {
Query,
Nested
} = require('../');

/*
{
user( id:3500401 ) {
id,
nickname : name,
isViewerFriend,

profilePicture( size:50 ) {
EXAMPLE 1
query {
user( id: 3500401 ) {
id,
nickname: name,
isViewerFriend,
profilePicture( size: 50 ) {
uri,
width,
height
}
}
}
}
*/

let profilePicture = new Query("profilePicture",{size : 50});
profilePicture.find( "uri", "width", "height");
let user = new Query("user",{id : 3500401});
user.find(["id", {"nickname":"name"}, "isViewerFriend", {"image":profilePicture}]);
const profilePicture = new Query('profilePicture', {
size: 50
});
profilePicture.select('uri', 'width', 'height');
const user = new Query('user', {
id: 3500401
});
user.select(['id', {
nickname: 'name'
}, 'isViewerFriend', {
image: profilePicture
}]);

logger.log("user",user+"");
/*
console.log('user', user + '');

query FetchLeeAndSam {
lee: user(id: "1") {
name
}
sam: user(id: "2") {
name
/*
EXAMPLE 2
query {
FetchLeeAndSam {
lee: user(id: '1') {
name
}
sam: user(id: '2') {
name
}
}
}
}
*/

let FetchLeeAndSam = new Query("FetchLeeAndSam");

let lee = new Query("user",{id : '1'});
lee.setAlias('lee');
lee.find({name:true});
logger.log("lee",lee.toString());

let sam = new Query("user","sam");
sam.filter({id : '2'});
sam.find("name");
logger.log("sam",sam+"");

console.log(FetchLeeAndSam.find(lee,sam)+"");
const FetchLeeAndSam = new Query('FetchLeeAndSam');

const lee = new Nested('user', {
id: '1'
});
lee.setAlias('lee');
lee.select({
name: true
});

console.log('lee', lee.toString());

const sam = new Nested('user', 'sam');
sam.filter({
id: '2'
});
sam.select('name');
console.log('sam', sam + '');

console.log(FetchLeeAndSam.select(lee, sam) + '');
Loading