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' ;
51import {
62 FragmentDefinitionNode ,
73 FragmentSpreadNode ,
84 Kind ,
95 OperationDefinitionNode ,
10- parse ,
116 SelectionSetNode ,
127 visit ,
138} from 'graphql' ;
14- import { ParserOptions } from './types ' ;
9+ import { Source , asArray } from '@graphql-tools/utils ' ;
1510import { GraphQLConfig } from 'graphql-config' ;
16- import { dirname } from 'path' ;
11+ import { ParserOptions } from './types' ;
12+ import { getOnDiskFilepath } from './utils' ;
1713
1814export type FragmentSource = { filePath : string ; document : FragmentDefinitionNode } ;
1915export type OperationSource = { filePath : string ; document : OperationDefinitionNode } ;
2016
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-
3517export type SiblingOperations = {
3618 available : boolean ;
3719 getOperations ( ) : OperationSource [ ] ;
@@ -46,65 +28,46 @@ export type SiblingOperations = {
4628 getOperationByType ( operationType : 'query' | 'mutation' | 'subscription' ) : OperationSource [ ] ;
4729} ;
4830
49- function loadSiblings ( baseDir : string , loadPaths : string [ ] ) : Source [ ] {
50- return loadDocumentsSync ( loadPaths , {
51- cwd : baseDir ,
52- loaders : operationsLoaders ,
53- skipGraphQLImport : true ,
54- } ) ;
55- }
56-
5731const operationsCache : Map < string , Source [ ] > = new Map ( ) ;
5832const siblingOperationsCache : Map < Source [ ] , SiblingOperations > = new Map ( ) ;
5933
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 ( ',' ) ;
8140
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+ }
8544
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 ) ;
9247 }
9348
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 ) {
9561 let printed = false ;
9662
9763 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 ;
10070 }
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-
10871 return [ ] ;
10972 } ;
11073
0 commit comments