@@ -11,7 +11,7 @@ import {
1111import { Source , asArray } from '@graphql-tools/utils' ;
1212import { GraphQLConfig } from 'graphql-config' ;
1313import { ParserOptions } from './types' ;
14- import { getOnDiskFilepath } from './utils' ;
14+ import { getOnDiskFilepath , loaderCache } from './utils' ;
1515
1616export type FragmentSource = { filePath : string ; document : FragmentDefinitionNode } ;
1717export type OperationSource = { filePath : string ; document : OperationDefinitionNode } ;
@@ -61,15 +61,16 @@ const getSiblings = (filePath: string, gqlConfig: GraphQLConfig): Source[] => {
6161 return [ ] ;
6262 }
6363
64- if ( operationsCache . has ( documentsKey ) ) {
65- return operationsCache . get ( documentsKey ) ;
66- }
64+ let siblings = operationsCache . get ( documentsKey ) ;
6765
68- const documents = projectForFile . loadDocumentsSync ( projectForFile . documents , {
69- skipGraphQLImport : true ,
70- } ) ;
71- const siblings = handleVirtualPath ( documents )
72- operationsCache . set ( documentsKey , siblings ) ;
66+ if ( ! siblings ) {
67+ const documents = projectForFile . loadDocumentsSync ( projectForFile . documents , {
68+ skipGraphQLImport : true ,
69+ cache : loaderCache
70+ } ) ;
71+ siblings = handleVirtualPath ( documents )
72+ operationsCache . set ( documentsKey , siblings ) ;
73+ }
7374
7475 return siblings ;
7576} ;
@@ -106,95 +107,95 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
106107 // Since the siblings array is cached, we can use it as cache key.
107108 // We should get the same array reference each time we get
108109 // to this point for the same graphql project
109- if ( siblingOperationsCache . has ( siblings ) ) {
110- return siblingOperationsCache . get ( siblings ) ;
111- }
112-
113- let fragmentsCache : FragmentSource [ ] | null = null ;
114-
115- const getFragments = ( ) : FragmentSource [ ] => {
116- if ( fragmentsCache === null ) {
117- const result : FragmentSource [ ] = [ ] ;
118-
119- for ( const source of siblings ) {
120- for ( const definition of source . document . definitions || [ ] ) {
121- if ( definition . kind === Kind . FRAGMENT_DEFINITION ) {
122- result . push ( {
123- filePath : source . location ,
124- document : definition ,
125- } ) ;
110+ let siblingOperations = siblingOperationsCache . get ( siblings ) ;
111+ if ( ! siblingOperations ) {
112+ let fragmentsCache : FragmentSource [ ] | null = null ;
113+
114+ const getFragments = ( ) : FragmentSource [ ] => {
115+ if ( fragmentsCache === null ) {
116+ const result : FragmentSource [ ] = [ ] ;
117+
118+ for ( const source of siblings ) {
119+ for ( const definition of source . document . definitions || [ ] ) {
120+ if ( definition . kind === Kind . FRAGMENT_DEFINITION ) {
121+ result . push ( {
122+ filePath : source . location ,
123+ document : definition ,
124+ } ) ;
125+ }
126126 }
127127 }
128+ fragmentsCache = result ;
128129 }
129- fragmentsCache = result ;
130- }
131- return fragmentsCache ;
132- } ;
133-
134- let cachedOperations : OperationSource [ ] | null = null ;
135-
136- const getOperations = ( ) : OperationSource [ ] => {
137- if ( cachedOperations === null ) {
138- const result : OperationSource [ ] = [ ] ;
139-
140- for ( const source of siblings ) {
141- for ( const definition of source . document . definitions || [ ] ) {
142- if ( definition . kind === Kind . OPERATION_DEFINITION ) {
143- result . push ( {
144- filePath : source . location ,
145- document : definition ,
146- } ) ;
130+ return fragmentsCache ;
131+ } ;
132+
133+ let cachedOperations : OperationSource [ ] | null = null ;
134+
135+ const getOperations = ( ) : OperationSource [ ] => {
136+ if ( cachedOperations === null ) {
137+ const result : OperationSource [ ] = [ ] ;
138+
139+ for ( const source of siblings ) {
140+ for ( const definition of source . document . definitions || [ ] ) {
141+ if ( definition . kind === Kind . OPERATION_DEFINITION ) {
142+ result . push ( {
143+ filePath : source . location ,
144+ document : definition ,
145+ } ) ;
146+ }
147147 }
148148 }
149+ cachedOperations = result ;
149150 }
150- cachedOperations = result ;
151- }
152- return cachedOperations ;
153- } ;
154-
155- const getFragment = ( name : string ) => getFragments ( ) . filter ( f => f . document . name ?. value === name ) ;
156-
157- const collectFragments = (
158- selectable : SelectionSetNode | OperationDefinitionNode | FragmentDefinitionNode ,
159- recursive = true ,
160- collected : Map < string , FragmentDefinitionNode > = new Map ( )
161- ) => {
162- visit ( selectable , {
163- FragmentSpread ( spread : FragmentSpreadNode ) {
164- const name = spread . name . value ;
165- const fragmentInfo = getFragment ( name ) ;
166-
167- if ( fragmentInfo . length === 0 ) {
168- // eslint-disable-next-line no-console
169- console . warn (
170- `Unable to locate fragment named "${ name } ", please make sure it's loaded using "parserOptions.operations"`
171- ) ;
172- return ;
173- }
174- const fragment = fragmentInfo [ 0 ] ;
175- const alreadyVisited = collected . has ( name ) ;
151+ return cachedOperations ;
152+ } ;
176153
177- if ( ! alreadyVisited ) {
178- collected . set ( name , fragment . document ) ;
179- if ( recursive ) {
180- collectFragments ( fragment . document , recursive , collected ) ;
154+ const getFragment = ( name : string ) => getFragments ( ) . filter ( f => f . document . name ?. value === name ) ;
155+
156+ const collectFragments = (
157+ selectable : SelectionSetNode | OperationDefinitionNode | FragmentDefinitionNode ,
158+ recursive = true ,
159+ collected : Map < string , FragmentDefinitionNode > = new Map ( )
160+ ) => {
161+ visit ( selectable , {
162+ FragmentSpread ( spread : FragmentSpreadNode ) {
163+ const name = spread . name . value ;
164+ const fragmentInfo = getFragment ( name ) ;
165+
166+ if ( fragmentInfo . length === 0 ) {
167+ // eslint-disable-next-line no-console
168+ console . warn (
169+ `Unable to locate fragment named "${ name } ", please make sure it's loaded using "parserOptions.operations"`
170+ ) ;
171+ return ;
181172 }
182- }
183- } ,
184- } ) ;
185- return collected ;
186- } ;
187-
188- const siblingOperations : SiblingOperations = {
189- available : true ,
190- getFragments,
191- getOperations,
192- getFragment,
193- getFragmentByType : typeName => getFragments ( ) . filter ( f => f . document . typeCondition ?. name ?. value === typeName ) ,
194- getOperation : name => getOperations ( ) . filter ( o => o . document . name ?. value === name ) ,
195- getOperationByType : type => getOperations ( ) . filter ( o => o . document . operation === type ) ,
196- getFragmentsInUse : ( selectable , recursive = true ) => Array . from ( collectFragments ( selectable , recursive ) . values ( ) ) ,
197- } ;
198- siblingOperationsCache . set ( siblings , siblingOperations ) ;
173+ const fragment = fragmentInfo [ 0 ] ;
174+ const alreadyVisited = collected . has ( name ) ;
175+
176+ if ( ! alreadyVisited ) {
177+ collected . set ( name , fragment . document ) ;
178+ if ( recursive ) {
179+ collectFragments ( fragment . document , recursive , collected ) ;
180+ }
181+ }
182+ } ,
183+ } ) ;
184+ return collected ;
185+ } ;
186+
187+ siblingOperations = {
188+ available : true ,
189+ getFragments,
190+ getOperations,
191+ getFragment,
192+ getFragmentByType : typeName => getFragments ( ) . filter ( f => f . document . typeCondition ?. name ?. value === typeName ) ,
193+ getOperation : name => getOperations ( ) . filter ( o => o . document . name ?. value === name ) ,
194+ getOperationByType : type => getOperations ( ) . filter ( o => o . document . operation === type ) ,
195+ getFragmentsInUse : ( selectable , recursive = true ) => Array . from ( collectFragments ( selectable , recursive ) . values ( ) ) ,
196+ } ;
197+
198+ siblingOperationsCache . set ( siblings , siblingOperations ) ;
199+ }
199200 return siblingOperations ;
200201}
0 commit comments