@@ -9,46 +9,41 @@ import {
99 GraphQLNamedType ,
1010 GraphQLInterfaceType ,
1111 GraphQLArgument ,
12+ GraphQLDirective ,
1213 isObjectType ,
1314 isInterfaceType ,
1415 isUnionType ,
1516 isInputObjectType ,
1617 isListType ,
1718 isNonNullType ,
18- GraphQLDirective ,
1919 TypeInfo ,
2020 visit ,
2121 visitWithTypeInfo ,
2222} from 'graphql' ;
2323import { SiblingOperations } from './sibling-operations' ;
2424
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 > ;
3326
34- return cache ;
35- } ;
36- }
27+ let reachableTypesCache : ReachableTypes ;
3728
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+ }
4035
41- export function collectReachableTypes ( schema : GraphQLSchema ) : Set < string > {
42- const reachableTypes = new Set < string > ( ) ;
36+ const reachableTypes : ReachableTypes = new Set ( ) ;
4337
4438 collectFromDirectives ( schema . getDirectives ( ) ) ;
4539 collectFrom ( schema . getQueryType ( ) ) ;
4640 collectFrom ( schema . getMutationType ( ) ) ;
4741 collectFrom ( schema . getSubscriptionType ( ) ) ;
4842
49- return reachableTypes ;
43+ reachableTypesCache = reachableTypes ;
44+ return reachableTypesCache ;
5045
51- function collectFromDirectives ( directives : readonly GraphQLDirective [ ] ) {
46+ function collectFromDirectives ( directives : readonly GraphQLDirective [ ] ) : void {
5247 for ( const directive of directives || [ ] ) {
5348 reachableTypes . add ( directive . name ) ;
5449 directive . args . forEach ( collectFromArgument ) ;
@@ -119,43 +114,33 @@ export function collectReachableTypes(schema: GraphQLSchema): Set<string> {
119114 if ( isListType ( type ) || isNonNullType ( type ) ) {
120115 return resolveName ( type . ofType ) ;
121116 }
122-
123117 return type . name ;
124118 }
125119
126120 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 ;
130123 }
131-
132- return false ;
124+ reachableTypes . add ( name ) ;
125+ return true ;
133126 }
134127}
135128
136- export type FieldsCache = Record < string , Set < string > > ;
129+ export type UsedFields = Record < string , Set < string > > ;
137130
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 ;
149132
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+ }
153139
154- export function collectUsedFields ( schema : GraphQLSchema , operations : SiblingOperations ) : FieldsCache {
155- const cache : FieldsCache = { } ;
140+ const usedFields : UsedFields = { } ;
156141
157142 const addField = ( typeName , fieldName ) => {
158- const fieldType = cache [ typeName ] ?? ( cache [ typeName ] = new Set ( ) ) ;
143+ const fieldType = usedFields [ typeName ] ?? ( usedFields [ typeName ] = new Set ( ) ) ;
159144 fieldType . add ( fieldName ) ;
160145 } ;
161146
@@ -180,14 +165,12 @@ export function collectUsedFields(schema: GraphQLSchema, operations: SiblingOper
180165 } ,
181166 } ) ;
182167
183- const allDocuments = [
184- ...operations . getOperations ( ) ,
185- ...operations . getFragments ( ) ,
186- ] ;
168+ const allDocuments = [ ...operations . getOperations ( ) , ...operations . getFragments ( ) ] ;
187169
188170 for ( const { document } of allDocuments ) {
189171 visit ( document , visitor ) ;
190172 }
191173
192- return cache ;
174+ usedFieldsCache = usedFields ;
175+ return usedFieldsCache ;
193176}
0 commit comments