Skip to content

Commit 20de75c

Browse files
authored
refactor: extract functions in index.js (#19)
* Update TODOs in README * Move GitHub link logic from visitor functions to report * Add filePath to GraphQLTag
1 parent 5820bd1 commit 20de75c

9 files changed

Lines changed: 147 additions & 80 deletions

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ $ graphql-usage ./schema.graphql ./src/
4646

4747
# TODO:
4848

49-
- [ ] Show usage for input types
49+
- [ ] Show usage info for input types
50+
- [ ] Improve usage info for enum types
5051
- [ ] Improve usage info for abstract types
5152
- [ ] Improve usage info for object types (e.g. whether the type is used rather than individual fields)
52-
- [ ] Support for analyzing TS files
53+
- [ ] Warn when Git branch is out of date or not on remote
5354
- [ ] Better support for client-only fields and client-side schemas
5455
- [ ] Windows support

src/findJSGraphQLTags.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ describe("findJSGraphQLTags", () => {
77
const query = graphql\`query findJSGraphQLTagsQuery { hero { id } }\`
88
`;
99

10-
expect(findJSGraphQLTags(js)).toEqual([
10+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
1111
{
1212
sourceLocationOffset: {
1313
column: 29,
1414
line: 3
1515
},
16-
template: "query findJSGraphQLTagsQuery { hero { id } }"
16+
template: "query findJSGraphQLTagsQuery { hero { id } }",
17+
filePath: "Component.js"
1718
}
1819
]);
1920
});
@@ -28,13 +29,14 @@ describe("findJSGraphQLTags", () => {
2829
});
2930
`;
3031

31-
expect(findJSGraphQLTags(js)).toEqual([
32+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
3233
{
3334
sourceLocationOffset: {
3435
column: 21,
3536
line: 6
3637
},
37-
template: "fragment Hero_hero on Hero { id }"
38+
template: "fragment Hero_hero on Hero { id }",
39+
filePath: "Component.js"
3840
}
3941
]);
4042
});
@@ -47,14 +49,15 @@ describe("findJSGraphQLTags", () => {
4749
graphql\`mutation TestMutation($input: ReviewInput!) { createReview(review: $input) { commentary } }\`;
4850
`;
4951

50-
expect(findJSGraphQLTags(js)).toEqual([
52+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
5153
{
5254
sourceLocationOffset: {
5355
column: 15,
5456
line: 5
5557
},
5658
template:
57-
"mutation TestMutation($input: ReviewInput!) { createReview(review: $input) { commentary } }"
59+
"mutation TestMutation($input: ReviewInput!) { createReview(review: $input) { commentary } }",
60+
filePath: "Component.js"
5861
}
5962
]);
6063
});
@@ -66,20 +69,22 @@ describe("findJSGraphQLTags", () => {
6669
const queryTwo = graphql\`query secondQuery { hero { name } }\`
6770
`;
6871

69-
expect(findJSGraphQLTags(js)).toEqual([
72+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
7073
{
7174
sourceLocationOffset: {
7275
column: 32,
7376
line: 3
7477
},
75-
template: "query firstQuery { hero { id } }"
78+
template: "query firstQuery { hero { id } }",
79+
filePath: "Component.js"
7680
},
7781
{
7882
sourceLocationOffset: {
7983
column: 32,
8084
line: 4
8185
},
82-
template: "query secondQuery { hero { name } }"
86+
template: "query secondQuery { hero { name } }",
87+
filePath: "Component.js"
8388
}
8489
]);
8590
});
@@ -90,13 +95,14 @@ describe("findJSGraphQLTags", () => {
9095
const query = gql\`query findJSGraphQLTagsQuery { hero { id } }\`
9196
`;
9297

93-
expect(findJSGraphQLTags(js)).toEqual([
98+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
9499
{
95100
sourceLocationOffset: {
96101
column: 25,
97102
line: 3
98103
},
99-
template: "query findJSGraphQLTagsQuery { hero { id } }"
104+
template: "query findJSGraphQLTagsQuery { hero { id } }",
105+
filePath: "Component.js"
100106
}
101107
]);
102108
});
@@ -115,7 +121,7 @@ describe("findJSGraphQLTags", () => {
115121
\`;
116122
`;
117123

118-
expect(findJSGraphQLTags(js)).toEqual([
124+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
119125
{
120126
sourceLocationOffset: {
121127
column: 40,
@@ -129,7 +135,8 @@ describe("findJSGraphQLTags", () => {
129135
}
130136
}
131137
}
132-
`
138+
`,
139+
filePath: "Component.js"
133140
}
134141
]);
135142
});
@@ -156,7 +163,7 @@ describe("findJSGraphQLTags", () => {
156163
};
157164
`;
158165

159-
expect(findJSGraphQLTags(js)).toEqual([
166+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
160167
{
161168
sourceLocationOffset: {
162169
column: 20,
@@ -175,7 +182,8 @@ describe("findJSGraphQLTags", () => {
175182
...VoteButtons
176183
...RepoInfo
177184
}
178-
`
185+
`,
186+
filePath: "Component.js"
179187
}
180188
]);
181189
});
@@ -203,7 +211,7 @@ describe("findJSGraphQLTags", () => {
203211
\`;
204212
`;
205213

206-
expect(findJSGraphQLTags(js)).toEqual([
214+
expect(findJSGraphQLTags(js, "Component.js")).toEqual([
207215
{
208216
sourceLocationOffset: {
209217
column: 40,
@@ -227,7 +235,8 @@ describe("findJSGraphQLTags", () => {
227235
createdAt
228236
content
229237
}
230-
`
238+
`,
239+
filePath: "Component.js"
231240
}
232241
]);
233242
});

src/findJSGraphQLTags.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const PARSER_OPTIONS: parser.ParserOptions = {
3535
strictMode: false
3636
};
3737

38-
function find(text: string): Array<GraphQLTag> {
38+
function find(text: string, filePath: string): Array<GraphQLTag> {
3939
const result: Array<GraphQLTag> = [];
4040
const ast = parser.parse(text, PARSER_OPTIONS);
4141

@@ -46,7 +46,8 @@ function find(text: string): Array<GraphQLTag> {
4646
if (isGraphQLTag(node.tag)) {
4747
result.push({
4848
template: node.quasi.quasis[0].value.raw,
49-
sourceLocationOffset: getSourceLocationOffset(node.quasi)
49+
sourceLocationOffset: getSourceLocationOffset(node.quasi),
50+
filePath
5051
});
5152
}
5253
}

src/findTSGraphQLTags.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe("findTSGraphQLTags", () => {
1313
column: 29,
1414
line: 3
1515
},
16-
template: "query findGraphQLTagsQuery { hero { id } }"
16+
template: "query findGraphQLTagsQuery { hero { id } }",
17+
filePath: "Component.tsx"
1718
}
1819
]);
1920
});
@@ -34,7 +35,8 @@ describe("findTSGraphQLTags", () => {
3435
column: 21,
3536
line: 6
3637
},
37-
template: "fragment Hero_hero on Hero { id }"
38+
template: "fragment Hero_hero on Hero { id }",
39+
filePath: "Component.tsx"
3840
}
3941
]);
4042
});
@@ -54,7 +56,8 @@ describe("findTSGraphQLTags", () => {
5456
line: 5
5557
},
5658
template:
57-
"mutation TestMutation($input: ReviewInput!) { createReview(review: $input) { commentary } }"
59+
"mutation TestMutation($input: ReviewInput!) { createReview(review: $input) { commentary } }",
60+
filePath: "Component.tsx"
5861
}
5962
]);
6063
});
@@ -72,14 +75,16 @@ describe("findTSGraphQLTags", () => {
7275
column: 32,
7376
line: 3
7477
},
75-
template: "query firstQuery { hero { id } }"
78+
template: "query firstQuery { hero { id } }",
79+
filePath: "Component.tsx"
7680
},
7781
{
7882
sourceLocationOffset: {
7983
column: 32,
8084
line: 4
8185
},
82-
template: "query secondQuery { hero { name } }"
86+
template: "query secondQuery { hero { name } }",
87+
filePath: "Component.tsx"
8388
}
8489
]);
8590
});
@@ -96,7 +101,8 @@ describe("findTSGraphQLTags", () => {
96101
column: 25,
97102
line: 3
98103
},
99-
template: "query findGraphQLTagsQuery { hero { id } }"
104+
template: "query findGraphQLTagsQuery { hero { id } }",
105+
filePath: "Component.tsx"
100106
}
101107
]);
102108
});
@@ -129,7 +135,8 @@ describe("findTSGraphQLTags", () => {
129135
}
130136
}
131137
}
132-
`
138+
`,
139+
filePath: "Component.tsx"
133140
}
134141
]);
135142
});
@@ -175,7 +182,8 @@ describe("findTSGraphQLTags", () => {
175182
...VoteButtons
176183
...RepoInfo
177184
}
178-
`
185+
`,
186+
filePath: "Component.tsx"
179187
}
180188
]);
181189
});
@@ -227,7 +235,8 @@ describe("findTSGraphQLTags", () => {
227235
createdAt
228236
content
229237
}
230-
`
238+
`,
239+
filePath: "Component.tsx"
231240
}
232241
]);
233242
});

src/findTSGraphQLTags.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ import { GraphQLTag } from "./types";
1616

1717
// https://github.com/relay-tools/relay-compiler-language-typescript/blob/d3c7af5d5558569def7c46d485b7fc64c6a94ccb/src/FindGraphQLTags.ts
1818

19-
function visit(node: Node, addGraphQLTag: (tag: GraphQLTag) => void): void {
19+
function visit(
20+
node: Node,
21+
filePath: string,
22+
addGraphQLTag: (tag: GraphQLTag) => void
23+
): void {
2024
function visitNode(node: Node) {
2125
switch (node.kind) {
2226
case SyntaxKind.TaggedTemplateExpression: {
2327
const taggedTemplate = node as TaggedTemplateExpression;
2428
if (isGraphQLTag(taggedTemplate.tag)) {
2529
addGraphQLTag({
2630
template: getGraphQLText(taggedTemplate),
27-
sourceLocationOffset: getSourceLocationOffset(taggedTemplate)
31+
sourceLocationOffset: getSourceLocationOffset(taggedTemplate),
32+
filePath
2833
});
2934
}
3035
}
@@ -76,7 +81,7 @@ function getSourceLocationOffset(quasi: TaggedTemplateExpression) {
7681
function find(text: string, filePath: string) {
7782
const result: GraphQLTag[] = [];
7883
const ast = createSourceFile(filePath, text, ScriptTarget.Latest, true);
79-
visit(ast, tag => result.push(tag));
84+
visit(ast, filePath, tag => result.push(tag));
8085
return result;
8186
}
8287

src/getFieldInfo.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import { GraphQLTag } from "./types";
1313

1414
export interface FieldInfo {
1515
name: string;
16-
link: string;
16+
line: number;
1717
parentType: string;
1818
type: string;
1919
rootNodeName: string;
20+
filePath: string;
2021
}
2122

2223
function getFeildInfo(
23-
{ template, sourceLocationOffset }: GraphQLTag,
24+
{ template, sourceLocationOffset, filePath }: GraphQLTag,
2425
typeInfo: TypeInfo,
25-
githubBaseURL: string
26+
cb: (fieldInfo: FieldInfo) => void
2627
) {
27-
const fields: FieldInfo[] = [];
2828
const ast = parse(template);
2929

3030
visit(
@@ -36,10 +36,10 @@ function getFeildInfo(
3636
graphqlNode,
3737
graphqlNode.name.value,
3838
typeInfo,
39-
fields,
4039
template,
4140
sourceLocationOffset,
42-
githubBaseURL
41+
filePath,
42+
cb
4343
);
4444
} else {
4545
throw new Error(`No name for OperationDefinition`);
@@ -51,29 +51,27 @@ function getFeildInfo(
5151
graphqlNode,
5252
graphqlNode.name.value,
5353
typeInfo,
54-
fields,
5554
template,
5655
sourceLocationOffset,
57-
githubBaseURL
56+
filePath,
57+
cb
5858
);
5959
} else {
6060
throw new Error(`No name for FragmentDefinition`);
6161
}
6262
}
6363
})
6464
);
65-
66-
return fields;
6765
}
6866

6967
function visitFields(
7068
node: ASTNode,
7169
operationOrFragmentName: string,
7270
typeInfo: TypeInfo,
73-
fields: FieldInfo[],
7471
template: string,
7572
sourceLocationOffset: { line: number; column: number },
76-
githubBaseURL: string
73+
filePath: string,
74+
cb: (fieldInfo: FieldInfo) => void
7775
) {
7876
visit(
7977
node,
@@ -103,12 +101,13 @@ function visitFields(
103101
const templateStart = getLocation(source, loc.start);
104102
const line = sourceLocationOffset.line + templateStart.line - 1;
105103

106-
fields.push({
104+
cb({
107105
name: nodeName,
108106
type: nodeType.toString(),
109107
parentType: parentType.toString(),
110-
link: `${githubBaseURL}#L${line}`,
111-
rootNodeName: operationOrFragmentName
108+
rootNodeName: operationOrFragmentName,
109+
filePath,
110+
line
112111
});
113112
}
114113
})

0 commit comments

Comments
 (0)