SK GraphQL Query Builder
An even simpler Javascript, ES5 friendly, GraphQL query builder
No need for multiple functions, commands, or es5 compatible compiling. Just create a single Query with an options object and get a GraphQL ready query string in return.
Forked from https://github.com/codemeasandwich/graphql-query-builder - a simple ES6 graphql query builder
info:
★ it on github
If this was helpful,Install
npm install sk-query-builder
Query function:
Query is available out of the box on the Window object, but can also be required to prevent namespace interference.
var Query = ;
Use:
Query can be called with a single arguement of either a single query object or an array of query objects and will return a GraphQL formatted string to be attached to query.
var query = ;
Query Constructor:
Single query object or an array of multiple queries to turn into a GQL query string.
Key Value | Argument | Description |
---|---|---|
func: | String | the name of the query function |
alias: | String | alias value that result will be returned under |
filters: | Object | An object mapping attribute to values |
value: | String or Object | Attribute name or nested query you want returned. Can be an array of multiple values with the values: key |
values: | Array | An Array of value items ( String or Obj ) to return multiple values |
Example: ( get the sum of users in given timeframe )
var sumQuery =;
Output: ( returns formatted string )
"{total_sum: sum(from: 0,to: 1501234567890){count}}"
Additional Options:
Key Value | Argument | Description |
---|---|---|
prefix | String | prefix string before query obj ( i.e. "mutation" ) |
Example:
var prefixQuery =
## Enum Values: This library also creates an Enum function on the Window object that can be used to create enumeration values in the query string.
Use:
Add Enum values inside query objects buy either the Enum() function with a string to represent the final value or by simply using any string starting with "e$"
Enum( 'VALUE' ) == 'e$VALUE'
Argument (one) | Description |
---|---|
String | the name of the query function |
Example: ( get the sum of users in given timeframe )
var eventQuery =;console;// "{event_123:event(frequency:DAILY,value:CLICKS){timestamp,value}}"
Examples:
Nested query values
var FetchLeeAndSam =;console;//"{FetchLeeAndSam:users{lee:user(id:"1"){name,id},sam:user(id:"2"){name,id}}}"
Multiple query array with reusable values
var {returnalias: modelfunc: 'vehicle'filters: year: yearvalues:"num_produced""horsepower";};var CarCatalog =;console;//"{Mustang:vehicle(year:"1964"){num_produced,horsepower} Camero:vehicle(year:"1988"){num_produced,horsepower}}"
run Examples
node example/simple.js