@@ -9,46 +9,41 @@ import {
9
9
GraphQLNamedType ,
10
10
GraphQLInterfaceType ,
11
11
GraphQLArgument ,
12
+ GraphQLDirective ,
12
13
isObjectType ,
13
14
isInterfaceType ,
14
15
isUnionType ,
15
16
isInputObjectType ,
16
17
isListType ,
17
18
isNonNullType ,
18
- GraphQLDirective ,
19
19
TypeInfo ,
20
20
visit ,
21
21
visitWithTypeInfo ,
22
22
} from 'graphql' ;
23
23
import { SiblingOperations } from './sibling-operations' ;
24
24
25
- export function createReachableTypesService ( schema : GraphQLSchema ) : ( ) => Set < string > ;
26
- export function createReachableTypesService ( schema ?: GraphQLSchema ) : ( ) => Set < string > | null {
27
- if ( schema ) {
28
- let cache : Set < string > = null ;
29
- return ( ) => {
30
- if ( ! cache ) {
31
- cache = collectReachableTypes ( schema ) ;
32
- }
25
+ export type ReachableTypes = Set < string > ;
33
26
34
- return cache ;
35
- } ;
36
- }
27
+ let reachableTypesCache : ReachableTypes ;
37
28
38
- return ( ) => null ;
39
- }
29
+ export function getReachableTypes ( schema : GraphQLSchema ) : ReachableTypes {
30
+ // We don't want cache reachableTypes on test environment
31
+ // Otherwise reachableTypes will be same for all tests
32
+ if ( process . env . NODE_ENV !== 'test' && reachableTypesCache ) {
33
+ return reachableTypesCache
34
+ }
40
35
41
- export function collectReachableTypes ( schema : GraphQLSchema ) : Set < string > {
42
- const reachableTypes = new Set < string > ( ) ;
36
+ const reachableTypes : ReachableTypes = new Set ( ) ;
43
37
44
38
collectFromDirectives ( schema . getDirectives ( ) ) ;
45
39
collectFrom ( schema . getQueryType ( ) ) ;
46
40
collectFrom ( schema . getMutationType ( ) ) ;
47
41
collectFrom ( schema . getSubscriptionType ( ) ) ;
48
42
49
- return reachableTypes ;
43
+ reachableTypesCache = reachableTypes ;
44
+ return reachableTypesCache ;
50
45
51
- function collectFromDirectives ( directives : readonly GraphQLDirective [ ] ) {
46
+ function collectFromDirectives ( directives : readonly GraphQLDirective [ ] ) : void {
52
47
for ( const directive of directives || [ ] ) {
53
48
reachableTypes . add ( directive . name ) ;
54
49
directive . args . forEach ( collectFromArgument ) ;
@@ -119,43 +114,33 @@ export function collectReachableTypes(schema: GraphQLSchema): Set<string> {
119
114
if ( isListType ( type ) || isNonNullType ( type ) ) {
120
115
return resolveName ( type . ofType ) ;
121
116
}
122
-
123
117
return type . name ;
124
118
}
125
119
126
120
function shouldCollect ( name : string ) : boolean {
127
- if ( ! reachableTypes . has ( name ) ) {
128
- reachableTypes . add ( name ) ;
129
- return true ;
121
+ if ( reachableTypes . has ( name ) ) {
122
+ return false ;
130
123
}
131
-
132
- return false ;
124
+ reachableTypes . add ( name ) ;
125
+ return true ;
133
126
}
134
127
}
135
128
136
- export type FieldsCache = Record < string , Set < string > > ;
129
+ export type UsedFields = Record < string , Set < string > > ;
137
130
138
- export function createUsedFieldsService ( schema : GraphQLSchema , operations : SiblingOperations ) : ( ) => FieldsCache | null {
139
- if ( ! schema || ! operations ) {
140
- return ( ) => null ;
141
- }
142
-
143
- let cache : FieldsCache = null ;
144
-
145
- return ( ) => {
146
- if ( ! cache ) {
147
- cache = collectUsedFields ( schema , operations ) ;
148
- }
131
+ let usedFieldsCache : UsedFields ;
149
132
150
- return cache ;
151
- } ;
152
- }
133
+ export function getUsedFields ( schema : GraphQLSchema , operations : SiblingOperations ) : UsedFields {
134
+ // We don't want cache usedFields on test environment
135
+ // Otherwise usedFields will be same for all tests
136
+ if ( process . env . NODE_ENV !== 'test' && usedFieldsCache ) {
137
+ return usedFieldsCache ;
138
+ }
153
139
154
- export function collectUsedFields ( schema : GraphQLSchema , operations : SiblingOperations ) : FieldsCache {
155
- const cache : FieldsCache = { } ;
140
+ const usedFields : UsedFields = { } ;
156
141
157
142
const addField = ( typeName , fieldName ) => {
158
- const fieldType = cache [ typeName ] ?? ( cache [ typeName ] = new Set ( ) ) ;
143
+ const fieldType = usedFields [ typeName ] ?? ( usedFields [ typeName ] = new Set ( ) ) ;
159
144
fieldType . add ( fieldName ) ;
160
145
} ;
161
146
@@ -180,14 +165,12 @@ export function collectUsedFields(schema: GraphQLSchema, operations: SiblingOper
180
165
} ,
181
166
} ) ;
182
167
183
- const allDocuments = [
184
- ...operations . getOperations ( ) ,
185
- ...operations . getFragments ( ) ,
186
- ] ;
168
+ const allDocuments = [ ...operations . getOperations ( ) , ...operations . getFragments ( ) ] ;
187
169
188
170
for ( const { document } of allDocuments ) {
189
171
visit ( document , visitor ) ;
190
172
}
191
173
192
- return cache ;
174
+ usedFieldsCache = usedFields ;
175
+ return usedFieldsCache ;
193
176
}
0 commit comments