Skip to content

Commit 36a5025

Browse files
authored
Merge pull request #6 from ef-eng/fix-nested-matching-bug
fix: fixing a bug with matching deeply nested fields
2 parents 96f57bf + 8d3e764 commit 36a5025

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const rewriteDoc = (
4747
const nextNodeAndVars = callback(curNodeAndVars, curParents);
4848
variableDefinitions = nextNodeAndVars.variableDefinitions;
4949
const node = nextNodeAndVars.node;
50-
const nextParents = [node, ...curParents];
50+
const nextParents = [...curParents, node];
5151
for (const key of Object.keys(node)) {
5252
if (key === 'loc') continue;
5353
const val = (node as any)[key];

test/ast.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { OperationDefinitionNode, parse } from 'graphql';
22
import {
3+
extractPath,
34
extractVariableDefinitions,
45
nodesMatch,
56
replaceVariableDefinitions,
@@ -183,4 +184,31 @@ describe('ast utils', () => {
183184
);
184185
});
185186
});
187+
188+
describe('extractPath', () => {
189+
it('returns the path to the current node in the final document', () => {
190+
const doc = parse(`
191+
query doStuff($arg1: String) {
192+
thing1 { thing2 { thing3 { thing4 } } }
193+
}
194+
`);
195+
const parents = [
196+
doc as any,
197+
(doc as any).definitions[0],
198+
(doc as any).definitions[0].selectionSet,
199+
(doc as any).definitions[0].selectionSet.selections[0],
200+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet,
201+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0],
202+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0]
203+
.selectionSet,
204+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0]
205+
.selectionSet.selections[0],
206+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0]
207+
.selectionSet.selections[0].selectionSet,
208+
(doc as any).definitions[0].selectionSet.selections[0].selectionSet.selections[0]
209+
.selectionSet.selections[0].selectionSet.selections[0]
210+
];
211+
expect(extractPath(parents)).toEqual(['thing1', 'thing2', 'thing3', 'thing4']);
212+
});
213+
});
186214
});

test/functional/rewriteFieldArgType.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,42 @@ describe('Rewrite field arg type', () => {
9797
}
9898
});
9999
});
100+
101+
it('works on deeply nested fields', () => {
102+
const query = gqlFmt`
103+
query doTheThings($arg1: String!, $arg2: String) {
104+
stuff {
105+
things(identifier: $arg1, arg2: $arg2) {
106+
cat
107+
}
108+
}
109+
}
110+
`;
111+
const expectedRewritenQuery = gqlFmt`
112+
query doTheThings($arg1: Int!, $arg2: String) {
113+
stuff {
114+
things(identifier: $arg1, arg2: $arg2) {
115+
cat
116+
}
117+
}
118+
}
119+
`;
120+
121+
const handler = new RewriteHandler([
122+
new FieldArgTypeRewriter({
123+
fieldName: 'things',
124+
argName: 'identifier',
125+
oldType: 'String!',
126+
newType: 'Int!',
127+
coerceVariable: val => parseInt(val, 10)
128+
})
129+
]);
130+
expect(handler.rewriteRequest(query, { arg1: '123', arg2: 'blah' })).toEqual({
131+
query: expectedRewritenQuery,
132+
variables: {
133+
arg1: 123,
134+
arg2: 'blah'
135+
}
136+
});
137+
});
100138
});

0 commit comments

Comments
 (0)