diff --git a/src/__tests__/directives.tests.ts b/src/__tests__/directives.tests.ts index d5fade3..ef6f76f 100644 --- a/src/__tests__/directives.tests.ts +++ b/src/__tests__/directives.tests.ts @@ -33,6 +33,70 @@ describe('jsonToGraphQLQuery() - directives', () => { }`); }); + it('places args in front of drirective if it is @rest', () => { + const path = "/foo" + + const query = { + query: { + Posts: { + __args: { + where: { + id: 10, + }, + orderBy: 'flibble' + }, + __directives: { + rest: { path } + }, + id: true, + title: true, + post_date: true + } + } + } as any; + expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal( + `query { + Posts (where: {id: 10}, orderBy: "flibble") @rest(path: "/foo") { + id + title + post_date + } +}`); + }); + + it('returns strings, arrays and objects in the directive args', () => { + const string = "/foo" + const object = { bar: 'baz' } + const array = ["complexities", "of", "naming", "conventions"] + + const query = { + query: { + Posts: { + __args: { + where: { + id: 10, + }, + orderBy: 'flibble' + }, + __directives: { + client: { string, object, array } + }, + id: true, + title: true, + post_date: true + } + } + } as any; + expect(jsonToGraphQLQuery(query, { pretty: true })).to.equal( + `query { + Posts @client(string: "/foo", object: {bar: "baz"}, array: ["complexities", "of", "naming", "conventions"]) (where: {id: 10}, orderBy: "flibble") { + id + title + post_date + } +}`); + }); + it('converts a complex query with directives with no arguments', () => { const query = { query: { diff --git a/src/jsonToGraphQLQuery.ts b/src/jsonToGraphQLQuery.ts index 2f6a739..d8f311d 100644 --- a/src/jsonToGraphQLQuery.ts +++ b/src/jsonToGraphQLQuery.ts @@ -56,7 +56,7 @@ function buildDirectives(dirsObj: any): string { else if (typeof directiveValue === 'object') { const args = []; for (const argName in directiveValue) { - const argVal = stringify(directiveValue[argName]).replace(/"/g, ''); + const argVal = stringify(directiveValue[argName]); args.push(`${argName}: ${argVal}`); } return `${directiveName}(${args.join(', ')})`; @@ -109,6 +109,7 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>, else if (argsExist || directivesExist) { let argsStr: string; let dirsStr: string; + let dirStrIsRest: boolean = false; if (directivesExist) { // TODO: Add support for multiple directives on one node. const numDirectives = Object.keys(value.__directives).length; @@ -122,8 +123,12 @@ function convertQuery(node: any, level: number, output: Array<[string, number]>, if (argsExist) { argsStr = `(${buildArgs(value.__args)})`; } + if (dirsStr && dirsStr.includes('@rest')) { + dirStrIsRest = true + } const spacer = directivesExist && argsExist ? ' ' : ''; - token = `${token} ${dirsStr ? dirsStr : ''}${spacer}${argsStr ? argsStr : ''}`; + + token = `${token}${dirStrIsRest && argsStr ? ` ${argsStr} ` : ' '}${dirsStr ? dirsStr : ''}${spacer}${!dirStrIsRest && argsStr ? argsStr : ''}`; } // DEPRECATED: Should be removed in version 2.0.0