Skip to content

Repository files navigation

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

varocs=require('graphql-sql');//your graphql services endpointvarendPoint='https://www.endpoint.com/graphql';newocs.GQLService().query().select(['id','name']).from('getProduct').where({id:1}).request(newocs.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(newocs.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');vargqlService=(newgsql.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

varocs=require('graphql-sql');//your graphql services endpointvarendPoint='https://www.endpoint.com/graphql';//initialize GQLServicenewocs.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(newocs.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));

About

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

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages