-
-
Save Groxot/6cad3b2b16db345bada8f926f6840d1a to your computer and use it in GitHub Desktop.
GraphQL query generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Angular imports | |
import { isArray, isBlank, isDate, isNumber, isStringMap, isPresent, isString } from 'angular2/src/facade/lang' | |
export class GraphQLUtils { | |
static createMutation(data: Object, dataDefinition: Object, method: string, mutationName?: string): string { | |
if (!method || !data) { return null } | |
let mutation: string = (mutationName || method) + '{' + method | |
mutation += '(' + GraphQLUtils.flattenObject(data) + ')' | |
mutation += GraphQLUtils.processDataDefinition(dataDefinition || data) + '}' | |
return mutation | |
} | |
static createQuery(dataDefinition: Object, method: string, parameters: Object, queryName?: string, subQuery?: string): string { | |
if (!method || !dataDefinition) { return null } | |
let query: string = 'query ' + (queryName || method) + '{' + this.createSubQuery(dataDefinition, method, parameters, subQuery) | |
// for graphql post (graphql express) | |
return JSON.stringify( | |
{ | |
'query': query | |
}) | |
} | |
static createSubQuery(dataDefinition: Object, method: string, parameters: Object, subQuery?: string): string { | |
if (!method || !dataDefinition) { return null } | |
let query: string = method | |
if (parameters) { | |
query += '(' + GraphQLUtils.flattenObject(parameters) + ')' | |
} | |
query += GraphQLUtils.processDataDefinition(dataDefinition, subQuery) + '}' | |
return query | |
} | |
private static processDataDefinition(dataDefinition: Object, subQuery?: string): string { | |
if (!dataDefinition) { return '' } | |
let query: string = '' | |
let keys: string[] = Object.keys(dataDefinition) | |
keys.forEach((key: string, index: number) => { | |
if (!index) { query += '{' } | |
query += key | |
if (dataDefinition[key] instanceof Array && dataDefinition[key].length) { | |
query += GraphQLUtils.processDataDefinition(dataDefinition[key][0]) | |
} else if (dataDefinition[key] instanceof Object) { | |
query += GraphQLUtils.processDataDefinition(dataDefinition[key]) | |
} | |
if (index === (keys.length - 1)) { | |
if (!subQuery) { | |
subQuery = '}' | |
} | |
else{ | |
subQuery = ',' + subQuery | |
} | |
query += subQuery | |
} else { | |
query += ',' | |
} | |
}) | |
return query | |
} | |
private static flattenObject(object: Object): string { | |
return Object.keys(object || {}).reduce((array: any[], key: string) => { | |
if (!isBlank(object[key])) { array.push(key + ':' + GraphQLUtils.processValue(object[key])) } | |
return array | |
}, []).join(',') | |
} | |
private static processValue(value: any): string { | |
if (isBlank(value)) { return '' } | |
if (isString(value)) { return '"' + value + '"' } | |
if (isArray(value)) { | |
let arrayString: string = '[' | |
value.forEach((valueInArray: any, index: number) => { | |
arrayString += GraphQLUtils.processValue(valueInArray) | |
if (index !== value.length - 1) { arrayString += ',' } | |
}) | |
arrayString += ']' | |
return arrayString | |
} | |
if (isStringMap(value)) { | |
let objectString: string = '{' | |
let keys: string[] = Object.keys(value) | |
keys.forEach((key: string, index: number) => { | |
objectString += key + ':' + GraphQLUtils.processValue(value[key]) | |
if (index !== keys.length - 1) { objectString += ',' } | |
}) | |
objectString += '}' | |
return objectString | |
} | |
return value.toString() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment