diff --git a/src/ast.ts b/src/ast.ts index 9e1b721..34ff6db 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -47,7 +47,7 @@ export const rewriteDoc = ( const nextNodeAndVars = callback(curNodeAndVars, curParents); variableDefinitions = nextNodeAndVars.variableDefinitions; const node = nextNodeAndVars.node; - const nextParents = [node, ...curParents]; + const nextParents = [...curParents, node]; for (const key of Object.keys(node)) { if (key === 'loc') continue; const val = (node as any)[key]; diff --git a/test/ast.test.ts b/test/ast.test.ts index bd499e8..f6cfc36 100644 --- a/test/ast.test.ts +++ b/test/ast.test.ts @@ -1,5 +1,6 @@ import { OperationDefinitionNode, parse } from 'graphql'; import { + extractPath, extractVariableDefinitions, nodesMatch, replaceVariableDefinitions, @@ -183,4 +184,31 @@ describe('ast utils', () => { ); }); }); + + describe('extractPath', () => { + it('returns the path to the current node in the final document', () => { + const doc = parse(` + query doStuff($arg1: String) { + thing1 { thing2 { thing3 { thing4 } } } + } + `); + const parents = [ + doc as any, + (doc as any).definitions[0], + (doc as any).definitions[0].selectionSet, + (doc as any).definitions[0].selectionSet.selections[0], + (doc as any).definitions[0].selectionSet.selections[0].selectionSet, + (doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0], + (doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0] + .selectionSet, + (doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0] + .selectionSet.selections[0], + (doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0] + .selectionSet.selections[0].selectionSet, + (doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0] + .selectionSet.selections[0].selectionSet.selections[0] + ]; + expect(extractPath(parents)).toEqual(['thing1', 'thing2', 'thing3', 'thing4']); + }); + }); }); diff --git a/test/functional/rewriteFieldArgType.test.ts b/test/functional/rewriteFieldArgType.test.ts index a5d2bdc..9f8d173 100644 --- a/test/functional/rewriteFieldArgType.test.ts +++ b/test/functional/rewriteFieldArgType.test.ts @@ -97,4 +97,42 @@ describe('Rewrite field arg type', () => { } }); }); + + it('works on deeply nested fields', () => { + const query = gqlFmt` + query doTheThings($arg1: String!, $arg2: String) { + stuff { + things(identifier: $arg1, arg2: $arg2) { + cat + } + } + } + `; + const expectedRewritenQuery = gqlFmt` + query doTheThings($arg1: Int!, $arg2: String) { + stuff { + things(identifier: $arg1, arg2: $arg2) { + cat + } + } + } + `; + + const handler = new RewriteHandler([ + new FieldArgTypeRewriter({ + fieldName: 'things', + argName: 'identifier', + oldType: 'String!', + newType: 'Int!', + coerceVariable: val => parseInt(val, 10) + }) + ]); + expect(handler.rewriteRequest(query, { arg1: '123', arg2: 'blah' })).toEqual({ + query: expectedRewritenQuery, + variables: { + arg1: 123, + arg2: 'blah' + } + }); + }); });