Last active
November 9, 2017 08:23
-
-
Save matthewdenobrega/e72528bcf2a81bfb15dd 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): string { | |
if (!method || !dataDefinition) { return null } | |
let query: string = 'query ' + (queryName || method) + '{' + method | |
query += '(' + GraphQLUtils.flattenObject(parameters) + ')' | |
query += GraphQLUtils.processDataDefinition(dataDefinition) + '}' | |
return query | |
} | |
private static processDataDefinition(dataDefinition: Object): 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)) { | |
query += '}' | |
} 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
Hi. i forked this gist.
What can you say about my changes:
Fork