Skip to content

Commit 18c6de6

Browse files
committed
feat(astVisitor): add operation: 'query' | 'mutation' | 'subscription' property to visitFn in second argument info.
1 parent 3cfa66a commit 18c6de6

File tree

5 files changed

+91
-41
lines changed

5 files changed

+91
-41
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@types/dedent": "0.7.0",
2121
"@types/glob": "7.1.1",
2222
"@types/jest": "25.2.1",
23+
"@types/lodash.sortby": "^4.7.6",
2324
"@types/node": "13.11.0",
2425
"@typescript-eslint/eslint-plugin": "2.26.0",
2526
"@typescript-eslint/parser": "2.26.0",
@@ -30,6 +31,7 @@
3031
"graphql": "15.0.0",
3132
"graphql-compose": "7.14.1",
3233
"jest": "25.2.7",
34+
"lodash.sortby": "^4.7.0",
3335
"prettier": "2.0.2",
3436
"rimraf": "3.0.2",
3537
"semantic-release": "17.0.4",

src/__tests__/astVisitor-test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { astVisitor, VISITOR_REMOVE_NODE, VISITOR_SKIP_CHILDREN } from '../astVi
22
import { directoryToAst, AstRootNode } from '../directoryToAst';
33
import { astToSchema } from '../astToSchema';
44
import { graphql } from 'graphql';
5+
import sortBy from 'lodash.sortby';
56

67
describe('astVisitor', () => {
78
let ast: AstRootNode;
@@ -114,6 +115,59 @@ describe('astVisitor', () => {
114115
});
115116
});
116117

118+
describe('visitFn should have path & operation & name properties', () => {
119+
it('check ROOT_TYPE', () => {
120+
const nodes = [] as Array<any>;
121+
astVisitor(ast, {
122+
ROOT_TYPE: (node, info) => {
123+
nodes.push({ operation: info.operation, name: info.name, path: info.path });
124+
},
125+
});
126+
expect(sortBy(nodes, ['operation', 'name'])).toEqual([
127+
{ name: 'mutation', operation: 'mutation', path: [] },
128+
{ name: 'query', operation: 'query', path: [] },
129+
]);
130+
});
131+
132+
it('check DIR & FILE elements', () => {
133+
const nodes = [] as Array<any>;
134+
astVisitor(ast, {
135+
DIR: (node, info) => {
136+
nodes.push({ operation: info.operation, name: info.name, path: info.path });
137+
},
138+
FILE: (node, info) => {
139+
nodes.push({ operation: info.operation, name: info.name, path: info.path });
140+
},
141+
});
142+
expect(sortBy(nodes, ['operation', 'name'])).toEqual([
143+
{ operation: 'mutation', name: 'auth', path: ['mutation'] },
144+
{ operation: 'mutation', name: 'create', path: ['mutation', 'user'] },
145+
{ operation: 'mutation', name: 'list', path: ['mutation', 'logs.nested'] },
146+
{ operation: 'mutation', name: 'login', path: ['mutation', 'auth'] },
147+
{ operation: 'mutation', name: 'logout', path: ['mutation', 'auth'] },
148+
{ operation: 'mutation', name: 'logs.nested', path: ['mutation'] },
149+
{ operation: 'mutation', name: 'method', path: ['mutation', 'auth', 'nested'] },
150+
{ operation: 'mutation', name: 'nested', path: ['mutation', 'auth'] },
151+
{ operation: 'mutation', name: 'update', path: ['mutation', 'user'] },
152+
{ operation: 'mutation', name: 'user', path: ['mutation'] },
153+
{ operation: 'query', name: 'address.city', path: ['query', 'me'] },
154+
{ operation: 'query', name: 'address.street', path: ['query', 'me'] },
155+
{ operation: 'query', name: 'auth', path: ['query'] },
156+
{ operation: 'query', name: 'extendedData', path: ['query', 'user'] },
157+
{ operation: 'query', name: 'field', path: ['query'] },
158+
{ operation: 'query', name: 'isLoggedIn', path: ['query', 'auth'] },
159+
{ operation: 'query', name: 'me', path: ['query'] },
160+
{ operation: 'query', name: 'method', path: ['query', 'auth', 'nested'] },
161+
{ operation: 'query', name: 'name', path: ['query', 'me'] },
162+
{ operation: 'query', name: 'nested', path: ['query', 'auth'] },
163+
{ operation: 'query', name: 'roles', path: ['query', 'user'] },
164+
{ operation: 'query', name: 'some.index', path: ['query'] },
165+
{ operation: 'query', name: 'some.nested', path: ['query'] },
166+
{ operation: 'query', name: 'user', path: ['query'] },
167+
]);
168+
});
169+
});
170+
117171
it('try to wrap all mutations', async () => {
118172
const logs: any[] = [];
119173
astVisitor(ast, {

src/astVisitor.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { AstRootTypeNode, AstDirNode, AstFileNode, AstRootNode } from './directoryToAst';
1+
import {
2+
AstRootTypeNode,
3+
AstDirNode,
4+
AstFileNode,
5+
AstRootNode,
6+
RootTypeNames,
7+
} from './directoryToAst';
28

39
/**
410
* Do not traverse children
@@ -27,14 +33,23 @@ export type AstVisitor = {
2733
};
2834

2935
export interface VisitInfo {
36+
operation: RootTypeNames;
3037
parent: AstDirNode | AstRootTypeNode | AstRootNode;
3138
name: string;
3239
path: string[];
3340
}
3441

3542
export function astVisitor(ast: AstRootNode, visitor: AstVisitor): void {
36-
forEachKey(ast.children, (childNode, name) => {
37-
if (childNode) visitNode(childNode, visitor, { parent: ast, name, path: [] });
43+
(Object.keys(ast.children) as Array<keyof typeof ast.children>).forEach((operation) => {
44+
const rootNode = ast.children[operation];
45+
if (!rootNode) return;
46+
47+
visitNode(rootNode, visitor, {
48+
parent: ast,
49+
name: operation,
50+
path: [],
51+
operation,
52+
});
3853
});
3954
}
4055

@@ -73,6 +88,7 @@ export function visitNode(
7388
parent: result as AstDirNode,
7489
name,
7590
path: [...info.path, info.name],
91+
operation: info.operation,
7692
});
7793
});
7894
}

src/directoryToAst.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ export type RootTypeNames = 'query' | 'mutation' | 'subscription';
4343

4444
export interface AstRootNode extends AstBaseNode {
4545
kind: 'root';
46-
children: {
47-
[T in RootTypeNames]?: AstRootTypeNode;
48-
};
46+
children: Record<RootTypeNames, AstRootTypeNode>;
4947
}
5048

5149
export const defaultOptions: DirectoryToAstOptions = {
@@ -56,7 +54,7 @@ export function directoryToAst(
5654
m: NodeModule,
5755
options: DirectoryToAstOptions = defaultOptions
5856
): AstRootNode {
59-
// if no path was passed in, assume the equivelant of __dirname from caller
57+
// if no path was passed in, assume the equivalent of __dirname from caller
6058
// otherwise, resolve path relative to the equivalent of __dirname
6159
const schemaPath = options?.relativePath
6260
? resolve(dirname(m.filename), options.relativePath)

yarn.lock

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,18 @@
908908
"@types/koa-compose" "*"
909909
"@types/node" "*"
910910

911+
"@types/lodash.sortby@^4.7.6":
912+
version "4.7.6"
913+
resolved "https://registry.yarnpkg.com/@types/lodash.sortby/-/lodash.sortby-4.7.6.tgz#eed689835f274b553db4ae16a4a23f58b79618a1"
914+
integrity sha512-EnvAOmKvEg7gdYpYrS6+fVFPw5dL9rBnJi3vcKI7wqWQcLJVF/KRXK9dH29HjGNVvFUj0s9prRP3J8jEGnGKDw==
915+
dependencies:
916+
"@types/lodash" "*"
917+
918+
"@types/lodash@*":
919+
version "4.14.167"
920+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.167.tgz#ce7d78553e3c886d4ea643c37ec7edc20f16765e"
921+
integrity sha512-w7tQPjARrvdeBkX/Rwg95S592JwxqOjmms3zWQ0XZgSyxSLdzWaYH3vErBhdVS/lRBX7F8aBYcYJYTr5TMGOzw==
922+
911923
"@types/long@^4.0.0":
912924
version "4.0.0"
913925
resolved "https://registry.yarnpkg.com/@types/long/-/long-4.0.0.tgz#719551d2352d301ac8b81db732acb6bdc28dbdef"
@@ -2403,7 +2415,7 @@ debug@^3.1.0:
24032415
dependencies:
24042416
ms "^2.1.1"
24052417

2406-
debuglog@*, debuglog@^1.0.1:
2418+
debuglog@^1.0.1:
24072419
version "1.0.1"
24082420
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
24092421
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -3841,7 +3853,7 @@ import-local@^3.0.2:
38413853
pkg-dir "^4.2.0"
38423854
resolve-cwd "^3.0.0"
38433855

3844-
imurmurhash@*, imurmurhash@^0.1.4:
3856+
imurmurhash@^0.1.4:
38453857
version "0.1.4"
38463858
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
38473859
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -5029,11 +5041,6 @@ lockfile@^1.0.4:
50295041
dependencies:
50305042
signal-exit "^3.0.2"
50315043

5032-
lodash._baseindexof@*:
5033-
version "3.1.0"
5034-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
5035-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
5036-
50375044
lodash._baseuniq@~4.6.0:
50385045
version "4.6.0"
50395046
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -5042,33 +5049,11 @@ lodash._baseuniq@~4.6.0:
50425049
lodash._createset "~4.0.0"
50435050
lodash._root "~3.0.0"
50445051

5045-
lodash._bindcallback@*:
5046-
version "3.0.1"
5047-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
5048-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
5049-
5050-
lodash._cacheindexof@*:
5051-
version "3.0.2"
5052-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
5053-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
5054-
5055-
lodash._createcache@*:
5056-
version "3.1.2"
5057-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
5058-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
5059-
dependencies:
5060-
lodash._getnative "^3.0.0"
5061-
50625052
lodash._createset@~4.0.0:
50635053
version "4.0.3"
50645054
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
50655055
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
50665056

5067-
lodash._getnative@*, lodash._getnative@^3.0.0:
5068-
version "3.9.1"
5069-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
5070-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
5071-
50725057
lodash._root@~3.0.0:
50735058
version "3.0.1"
50745059
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -5114,11 +5099,6 @@ [email protected]:
51145099
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
51155100
integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=
51165101

5117-
lodash.restparam@*:
5118-
version "3.6.1"
5119-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
5120-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
5121-
51225102
lodash.set@^4.3.2:
51235103
version "4.3.2"
51245104
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"

0 commit comments

Comments
 (0)