Skip to content

Commit

Permalink
fix: fixing a bug with matching deeply nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
chanind committed Oct 4, 2019
1 parent 96f57bf commit 8d3e764
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
28 changes: 28 additions & 0 deletions test/ast.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OperationDefinitionNode, parse } from 'graphql';
import {
extractPath,
extractVariableDefinitions,
nodesMatch,
replaceVariableDefinitions,
Expand Down Expand Up @@ -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']);
});
});
});
38 changes: 38 additions & 0 deletions test/functional/rewriteFieldArgType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
});
});
});

0 comments on commit 8d3e764

Please sign in to comment.