1
- import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader' ;
2
- import { CodeFileLoader } from '@graphql-tools/code-file-loader' ;
3
- import { loadDocumentsSync } from '@graphql-tools/load' ;
4
- import { Loader , SingleFileOptions , Source } from '@graphql-tools/utils' ;
5
1
import {
6
2
FragmentDefinitionNode ,
7
3
FragmentSpreadNode ,
8
4
Kind ,
9
5
OperationDefinitionNode ,
10
- parse ,
11
6
SelectionSetNode ,
12
7
visit ,
13
8
} from 'graphql' ;
14
- import { ParserOptions } from './types ' ;
9
+ import { Source , asArray } from '@graphql-tools/utils ' ;
15
10
import { GraphQLConfig } from 'graphql-config' ;
16
- import { dirname } from 'path' ;
11
+ import { ParserOptions } from './types' ;
12
+ import { getOnDiskFilepath } from './utils' ;
17
13
18
14
export type FragmentSource = { filePath : string ; document : FragmentDefinitionNode } ;
19
15
export type OperationSource = { filePath : string ; document : OperationDefinitionNode } ;
20
16
21
- export const operationsLoaders : Loader < string , SingleFileOptions > [ ] = [
22
- new GraphQLFileLoader ( ) ,
23
- new CodeFileLoader ( ) ,
24
- {
25
- loaderId : ( ) => 'direct-string' ,
26
- canLoad : async ( ) => false ,
27
- load : async ( ) => null ,
28
- canLoadSync : pointer => typeof pointer === 'string' && pointer . includes ( 'type ' ) ,
29
- loadSync : pointer => ( {
30
- document : parse ( pointer ) ,
31
- } ) ,
32
- } ,
33
- ] ;
34
-
35
17
export type SiblingOperations = {
36
18
available : boolean ;
37
19
getOperations ( ) : OperationSource [ ] ;
@@ -46,65 +28,46 @@ export type SiblingOperations = {
46
28
getOperationByType ( operationType : 'query' | 'mutation' | 'subscription' ) : OperationSource [ ] ;
47
29
} ;
48
30
49
- function loadSiblings ( baseDir : string , loadPaths : string [ ] ) : Source [ ] {
50
- return loadDocumentsSync ( loadPaths , {
51
- cwd : baseDir ,
52
- loaders : operationsLoaders ,
53
- skipGraphQLImport : true ,
54
- } ) ;
55
- }
56
-
57
31
const operationsCache : Map < string , Source [ ] > = new Map ( ) ;
58
32
const siblingOperationsCache : Map < Source [ ] , SiblingOperations > = new Map ( ) ;
59
33
60
- export function getSiblingOperations ( options : ParserOptions , gqlConfig : GraphQLConfig ) : SiblingOperations {
61
- let siblings : Source [ ] | null = null ;
62
-
63
- // We first try to use graphql-config for loading the operations paths, based on the type of the file,
64
- // We are using the directory of the file as the key for the schema caching, to avoid reloading of the schema.
65
- if ( gqlConfig && options ?. filePath ) {
66
- const fileDir = dirname ( options . filePath ) ;
67
-
68
- if ( operationsCache . has ( fileDir ) ) {
69
- siblings = operationsCache . get ( fileDir ) ;
70
- } else {
71
- const projectForFile = gqlConfig . getProjectForFile ( options . filePath ) ;
72
-
73
- if ( projectForFile ?. documents ) {
74
- siblings = projectForFile . loadDocumentsSync ( projectForFile . documents , {
75
- skipGraphQLImport : true ,
76
- } ) ;
77
- operationsCache . set ( fileDir , siblings ) ;
78
- }
79
- }
80
- }
34
+ const getSiblings = ( filePath : string , gqlConfig : GraphQLConfig ) : Source [ ] | null => {
35
+ const realFilepath = filePath ? getOnDiskFilepath ( filePath ) : null ;
36
+ const projectForFile = realFilepath ? gqlConfig . getProjectForFile ( realFilepath ) : gqlConfig . getDefault ( ) ;
37
+ const documentsKey = asArray ( projectForFile . documents )
38
+ . sort ( )
39
+ . join ( ',' ) ;
81
40
82
- if ( ! siblings && options ?. operations ) {
83
- const loadPaths = Array . isArray ( options . operations ) ? options . operations : [ options . operations ] ;
84
- const loadKey = loadPaths . join ( ',' ) ;
41
+ if ( ! documentsKey ) {
42
+ return [ ] ;
43
+ }
85
44
86
- if ( operationsCache . has ( loadKey ) ) {
87
- siblings = operationsCache . get ( loadKey ) ;
88
- } else {
89
- siblings = loadSiblings ( process . cwd ( ) , loadPaths ) ;
90
- operationsCache . set ( loadKey , siblings ) ;
91
- }
45
+ if ( operationsCache . has ( documentsKey ) ) {
46
+ return operationsCache . get ( documentsKey ) ;
92
47
}
93
48
94
- if ( ! siblings || siblings . length === 0 ) {
49
+ const siblings = projectForFile . loadDocumentsSync ( projectForFile . documents , {
50
+ skipGraphQLImport : true ,
51
+ } ) ;
52
+ operationsCache . set ( documentsKey , siblings ) ;
53
+
54
+ return siblings ;
55
+ } ;
56
+
57
+ export function getSiblingOperations ( options : ParserOptions , gqlConfig : GraphQLConfig ) : SiblingOperations {
58
+ const siblings = getSiblings ( options . filePath , gqlConfig ) ;
59
+
60
+ if ( siblings . length === 0 ) {
95
61
let printed = false ;
96
62
97
63
const noopWarn = ( ) => {
98
- if ( printed ) {
99
- return [ ] ;
64
+ if ( ! printed ) {
65
+ // eslint-disable-next-line no-console
66
+ console . warn (
67
+ `getSiblingOperations was called without any operations. Make sure to set "parserOptions.operations" to make this feature available!`
68
+ ) ;
69
+ printed = true ;
100
70
}
101
-
102
- printed = true ;
103
- // eslint-disable-next-line no-console
104
- console . warn (
105
- `getSiblingOperations was called without any operations. Make sure to set "parserOptions.operations" to make this feature available!`
106
- ) ;
107
-
108
71
return [ ] ;
109
72
} ;
110
73
0 commit comments