Skip to content

Commit b06e895

Browse files
committed
fix(VisitInfo): now getFieldPathArray correctly split fieldName with dots
1 parent d4df44a commit b06e895

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

src/VisitInfo.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,18 @@ export class VisitInfo<TNode extends AstDirNode | AstFileNode | AstRootTypeNode,
4242
this.node = data.node;
4343
this.operation = data.operation;
4444
this.nodeParent = data.nodeParent;
45-
this.fieldName = data.fieldName;
46-
this.fieldPath = data.fieldPath;
4745
this.schemaComposer = data.schemaComposer;
46+
47+
this.fieldPath = data.fieldPath;
48+
if (data.fieldName.indexOf('.')) {
49+
// if fieldName has dots, then split it
50+
const parts = data.fieldName.split('.').filter(Boolean);
51+
const fieldName = parts.pop() as string;
52+
this.fieldName = fieldName;
53+
this.fieldPath.push(...parts);
54+
} else {
55+
this.fieldName = data.fieldName;
56+
}
4857
}
4958

5059
/**
@@ -95,6 +104,7 @@ export class VisitInfo<TNode extends AstDirNode | AstFileNode | AstRootTypeNode,
95104
res.push(this.fieldName);
96105
}
97106

107+
// slice(1) - remove first element from array
98108
return opts?.includeOperation ? res : res.slice(1);
99109
}
100110

src/__tests__/VisitInfo-test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ describe('VisitInfo', () => {
5555
'debug',
5656
'ping',
5757
]);
58+
59+
const info2 = new VisitInfo({
60+
operation: 'query',
61+
fieldName: 'namespace.ping',
62+
fieldPath: ['query.storage'],
63+
node,
64+
nodeParent,
65+
schemaComposer,
66+
});
67+
expect(info2.getFieldPathArray()).toEqual(['storage', 'namespace', 'ping']);
68+
expect(info2.getFieldPathArray({ omitFieldName: true })).toEqual(['storage', 'namespace']);
5869
});
5970

6071
it('getFieldPathDotted()', () => {

src/__tests__/astVisitor-test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,27 @@ describe('astVisitor', () => {
145145
expect(sortBy(nodes, ['operation', 'name'])).toEqual([
146146
{ operation: 'mutation', name: 'auth', path: ['mutation'] },
147147
{ operation: 'mutation', name: 'create', path: ['mutation', 'user'] },
148-
{ operation: 'mutation', name: 'list', path: ['mutation', 'logs.nested'] },
148+
{ operation: 'mutation', name: 'list', path: ['mutation', 'logs', 'nested'] },
149149
{ operation: 'mutation', name: 'login', path: ['mutation', 'auth'] },
150150
{ operation: 'mutation', name: 'logout', path: ['mutation', 'auth'] },
151-
{ operation: 'mutation', name: 'logs.nested', path: ['mutation'] },
152151
{ operation: 'mutation', name: 'method', path: ['mutation', 'auth', 'nested'] },
152+
{ operation: 'mutation', name: 'nested', path: ['mutation', 'logs'] },
153153
{ operation: 'mutation', name: 'nested', path: ['mutation', 'auth'] },
154154
{ operation: 'mutation', name: 'update', path: ['mutation', 'user'] },
155155
{ operation: 'mutation', name: 'user', path: ['mutation'] },
156-
{ operation: 'query', name: 'address.city', path: ['query', 'me'] },
157-
{ operation: 'query', name: 'address.street', path: ['query', 'me'] },
158156
{ operation: 'query', name: 'auth', path: ['query'] },
157+
{ operation: 'query', name: 'city', path: ['query', 'me', 'address'] },
159158
{ operation: 'query', name: 'extendedData', path: ['query', 'user'] },
160159
{ operation: 'query', name: 'field', path: ['query'] },
160+
{ operation: 'query', name: 'index', path: ['query', 'some'] },
161161
{ operation: 'query', name: 'isLoggedIn', path: ['query', 'auth'] },
162162
{ operation: 'query', name: 'me', path: ['query'] },
163163
{ operation: 'query', name: 'method', path: ['query', 'auth', 'nested'] },
164164
{ operation: 'query', name: 'name', path: ['query', 'me'] },
165+
{ operation: 'query', name: 'nested', path: ['query', 'some'] },
165166
{ operation: 'query', name: 'nested', path: ['query', 'auth'] },
166167
{ operation: 'query', name: 'roles', path: ['query', 'user'] },
167-
{ operation: 'query', name: 'some.index', path: ['query'] },
168-
{ operation: 'query', name: 'some.nested', path: ['query'] },
168+
{ operation: 'query', name: 'street', path: ['query', 'me', 'address'] },
169169
{ operation: 'query', name: 'user', path: ['query'] },
170170
]);
171171
});

src/astVisitor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ export function visitNode(
8181

8282
if (result === VISITOR_REMOVE_NODE) {
8383
// `null` - means remove node from Ast and do not traverse children
84-
delete (info.nodeParent.children as any)[info.fieldName];
84+
delete (info.nodeParent.children as any)[info.node.name];
8585
return;
8686
} else if (result === VISITOR_SKIP_CHILDREN) {
8787
// `false` - do not traverse children
8888
return;
8989
} else if (result) {
9090
// replace node
91-
(info.nodeParent.children as any)[info.fieldName] = result;
91+
(info.nodeParent.children as any)[info.node.name] = result;
9292
} else {
9393
// `undefined` - just move further
9494
result = info.node;

0 commit comments

Comments
 (0)