1.1.25 • Published 4 years ago

graphql-sql v1.1.25

Weekly downloads
6
License
ISC
Repository
github
Last release
4 years ago

graphql-sql

A micro library that helps making graphql request in a simple way using sql like style.

Installing

Using npm:

$ npm install graphql-sql

Quickstart

var ocs = require('graphql-sql');

//your graphql services endpoint
var endPoint = 'https://www.endpoint.com/graphql';

new ocs.GQLService().query().select(['id','name']).from('getProduct').where({id:1})
.request(new ocs.HTTPServiceRequest('post', {Authorization:'bearer TOKEN'}, null, endPoint))
.then(response=>console.log(response.data))
.catch(error=>console.log('error',error));

Preview graphql query

require('graphql-sql');

console.log(new ocs.GQLService().query().select(['id','name']).from('getProduct').where({id:1}).toString());
// query 
// {
// 	getProduct(id:1) 
// 	{
// 		id,
// 		name
// 	}
// }

Creating an instance

creating a new instance with a custom config more info about config please refer to : https://www.npmjs.com/package/axios#request-config

require('graphql-sql');

var gqlService = (new gsql.GQLService())

gqlService.getRequester().defaults.baseURL = 'BASE_URL';
gqlService.getRequester().defaults.headers.common['Authorization'] = 'AUTH_TOKEN';

gqlService.query().select(['id','name']).from('getProduct').where({id:1}).post('/path/to/request').then(console.log);

Example

var ocs = require('graphql-sql');

//your graphql services endpoint
var endPoint = 'https://www.endpoint.com/graphql';

//initialize GQLService
new ocs.GQLService()
/* [optional] type of request (query or mutation) by calling .query() or .mutation()
* by default "query" is set.
*/
.query()
/* [required] defining which fields to be returned by passing an array with field name, 
* or object with key (type name) and value (array with list of field)
* #select([fieldName1, fieldName2, {typeName:[fieldName1, fieldName2]}])
*/
.select([{product:['id','name','path','status']},{collection:['id','name','path']}])
//defining type of service
.from('getProduct')
/* [optional] defining condition by passing an object with key pair value ()
* #where(key1:value1,key2:value2})
*/
.where({id:1,madeFrom:"HK"})
/*
* [required] start making request by passing HTTPServiceRequest
* @param {string} type - request method post, get, put...
* @param {object} data - currently only support Authorization
* @param {object} requester - callback object which has implemented result method for data handling and fault method for error handling
* @param {string} endpoint'
* @returns {Promise} 
*/
.request(new ocs.HTTPServiceRequest('post', {Authorization:'bearer TOKEN'}, null, endPoint))
// data handling once receiving server response
.then(response=>console.log(response.data))
// or error handling if anything goes wrong
.catch(error=>console.log('error',error));
1.1.25

4 years ago

1.1.23

5 years ago

1.1.21

5 years ago

1.1.19

5 years ago

1.1.17

5 years ago

1.1.15

5 years ago

1.1.13

5 years ago

1.1.11

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago