11import {
2- ASTNode ,
32 FieldNode ,
3+ FragmentDefinitionNode ,
44 getLocation ,
5+ OperationDefinitionNode ,
56 parse ,
67 Source ,
78 TypeInfo ,
@@ -20,59 +21,41 @@ export interface FieldInfo {
2021 filePath : string ;
2122}
2223
23- function getFeildInfo (
24- { template , sourceLocationOffset , filePath } : GraphQLTag ,
24+ function findFields (
25+ graphQLTag : GraphQLTag ,
2526 typeInfo : TypeInfo ,
2627 cb : ( fieldInfo : FieldInfo ) => void
2728) {
28- const ast = parse ( template ) ;
29+ const ast = parse ( graphQLTag . template ) ;
2930
3031 visit (
3132 ast ,
3233 visitWithTypeInfo ( typeInfo , {
33- OperationDefinition ( graphqlNode ) {
34- if ( graphqlNode . name ) {
35- visitFields (
36- graphqlNode ,
37- graphqlNode . name . value ,
38- typeInfo ,
39- template ,
40- sourceLocationOffset ,
41- filePath ,
42- cb
43- ) ;
44- } else {
45- throw new Error ( `No name for OperationDefinition` ) ;
46- }
34+ OperationDefinition ( node ) {
35+ visitFields ( node , graphQLTag , typeInfo , cb ) ;
4736 } ,
48- FragmentDefinition ( graphqlNode ) {
49- if ( graphqlNode . name ) {
50- visitFields (
51- graphqlNode ,
52- graphqlNode . name . value ,
53- typeInfo ,
54- template ,
55- sourceLocationOffset ,
56- filePath ,
57- cb
58- ) ;
59- } else {
60- throw new Error ( `No name for FragmentDefinition` ) ;
61- }
37+ FragmentDefinition ( node ) {
38+ visitFields ( node , graphQLTag , typeInfo , cb ) ;
6239 }
6340 } )
6441 ) ;
6542}
6643
6744function visitFields (
68- node : ASTNode ,
69- operationOrFragmentName : string ,
45+ node : OperationDefinitionNode | FragmentDefinitionNode ,
46+ graphQLTag : GraphQLTag ,
7047 typeInfo : TypeInfo ,
71- template : string ,
72- sourceLocationOffset : { line : number ; column : number } ,
73- filePath : string ,
7448 cb : ( fieldInfo : FieldInfo ) => void
7549) {
50+ if ( ! node . name ) {
51+ throw new Error (
52+ "visitFields expects OperationDefinitions and FragmentDefinitions to be named"
53+ ) ;
54+ }
55+
56+ const { filePath, sourceLocationOffset, template } = graphQLTag ;
57+ const operationOrFragmentName = node . name . value ;
58+
7659 visit (
7760 node ,
7861 visitWithTypeInfo ( typeInfo , {
@@ -85,15 +68,21 @@ function visitFields(
8568 const nodeName = graphqlNode . name . value ;
8669
8770 if ( ! parentType ) {
88- throw new Error ( `No parent type for ${ nodeName } ` ) ;
71+ throw new Error (
72+ `visitFields expects fields to have a parent type. No parent type for ${ nodeName } `
73+ ) ;
8974 }
9075
9176 if ( ! nodeType ) {
92- throw new Error ( `No type for ${ nodeName } ` ) ;
77+ throw new Error (
78+ `visitFields expects fields to have a type. No type for ${ nodeName } `
79+ ) ;
9380 }
9481
9582 if ( ! graphqlNode . loc ) {
96- throw new Error ( `No location for ${ nodeName } ` ) ;
83+ throw new Error (
84+ `visitFields expects fields to have a location. No location for ${ nodeName } `
85+ ) ;
9786 }
9887
9988 const loc = graphqlNode . loc ;
@@ -124,4 +113,4 @@ function isClientOnlyField(field: FieldNode): boolean {
124113 return ! ! clientOnlyDirective ;
125114}
126115
127- export default getFeildInfo ;
116+ export default findFields ;
0 commit comments