@@ -11,6 +11,9 @@ import {
11
11
SelectionSetNode ,
12
12
visit ,
13
13
} from 'graphql' ;
14
+ import { ParserOptions } from './types' ;
15
+ import { GraphQLConfig } from 'graphql-config' ;
16
+ import { dirname } from 'path' ;
14
17
15
18
export type SiblingOperations = {
16
19
available : boolean ;
@@ -47,8 +50,41 @@ function loadSiblings(baseDir: string, loadPaths: string[]): Source[] {
47
50
48
51
const operationsCache : Map < string , Source [ ] > = new Map ( ) ;
49
52
50
- export function getSiblingOperations ( baseDir : string , loadPaths : string [ ] ) : SiblingOperations {
51
- if ( loadPaths . length === 0 ) {
53
+ export function getSiblingOperations ( options : ParserOptions , gqlConfig : GraphQLConfig ) : SiblingOperations {
54
+ let siblings : Source [ ] | null = null ;
55
+
56
+ // We first try to use graphql-config for loading the operations paths, based on the type of the file,
57
+ // We are using the directory of the file as the key for the schema caching, to avoid reloading of the schema.
58
+ if ( options && options . filePath && ! options . skipGraphQLConfig ) {
59
+ const fileDir = dirname ( options . filePath ) ;
60
+
61
+ if ( operationsCache . has ( fileDir ) ) {
62
+ siblings = operationsCache . get ( fileDir ) ;
63
+ } else {
64
+ if ( gqlConfig ) {
65
+ const projectForFile = gqlConfig . getProject ( options . filePath ) ;
66
+
67
+ if ( projectForFile ) {
68
+ siblings = projectForFile . getDocumentsSync ( ) ;
69
+ operationsCache . set ( fileDir , siblings ) ;
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ if ( options && options . operations && ! siblings ) {
76
+ const loadPaths = Array . isArray ( options . operations ) ? options . operations : [ options . operations ] || [ ] ;
77
+ const loadKey = loadPaths . join ( ',' ) ;
78
+
79
+ if ( ! operationsCache . has ( loadKey ) ) {
80
+ siblings = loadSiblings ( process . cwd ( ) , loadPaths ) ;
81
+ operationsCache . set ( loadKey , siblings ) ;
82
+ } else {
83
+ siblings = operationsCache . get ( loadKey ) ;
84
+ }
85
+ }
86
+
87
+ if ( ! siblings || siblings . length === 0 ) {
52
88
let printed = false ;
53
89
54
90
const noopWarn = ( ) => {
@@ -76,14 +112,6 @@ export function getSiblingOperations(baseDir: string, loadPaths: string[]): Sibl
76
112
getUsedFragments : noopWarn ,
77
113
} ;
78
114
}
79
- const loadKey = loadPaths . join ( ',' ) ;
80
-
81
- if ( ! operationsCache . has ( loadKey ) ) {
82
- const loadedSiblings = loadSiblings ( baseDir , loadPaths ) ;
83
- operationsCache . set ( loadKey , loadedSiblings ) ;
84
- }
85
-
86
- const siblings = operationsCache . get ( loadKey ) ;
87
115
88
116
let fragmentsCache : FragmentDefinitionNode [ ] | null = null ;
89
117
0 commit comments