@@ -14,7 +14,11 @@ import {
1414 normalizeOutputParam ,
1515 Types ,
1616} from '@graphql-codegen/plugin-helpers' ;
17+ < < < << << HEAD
1718import { NoTypeDefinitionsFound } from '@graphql-tools/load' ;
19+ === === =
20+ import { NoTypeDefinitionsFound , type UnnormalizedTypeDefPointer } from '@graphql-tools/load' ;
21+ >>> >>> > 134 a5443e ( Implement externalDocuments implemented )
1822import { mergeTypeDefs } from '@graphql-tools/merge' ;
1923import { CodegenContext , ensureContext } from './config.js' ;
2024import { getDocumentTransform } from './documentTransforms.js' ;
@@ -86,6 +90,7 @@ export async function executeCodegen(
8690 let rootConfig : { [ key : string ] : any } = { } ;
8791 let rootSchemas : Types . Schema [ ] ;
8892 let rootDocuments : Types . OperationDocument [ ] ;
93+ let rootExternalDocuments : Types . OperationDocument [ ] ;
8994 const generates : { [ filename : string ] : Types . ConfiguredOutput } = { } ;
9095
9196 const cache = createCache ( ) ;
@@ -136,6 +141,11 @@ export async function executeCodegen(
136141 /* Normalize root "documents" field */
137142 rootDocuments = normalizeInstanceOrArray < Types . OperationDocument > ( config . documents ) ;
138143
144+ /* Normalize root "externalDocuments" field */
145+ rootExternalDocuments = normalizeInstanceOrArray < Types . OperationDocument > (
146+ config . externalDocuments ,
147+ ) ;
148+
139149 /* Normalize "generators" field */
140150 const generateKeys = Object . keys ( config . generates || { } ) ;
141151
@@ -228,13 +238,15 @@ export async function executeCodegen(
228238 let outputSchemaAst : GraphQLSchema ;
229239 let outputSchema : DocumentNode ;
230240 const outputFileTemplateConfig = outputConfig . config || { } ;
231- let outputDocuments : Types . DocumentFile [ ] = [ ] ;
241+ const outputDocuments : Types . DocumentFile [ ] = [ ] ;
232242 const outputSpecificSchemas = normalizeInstanceOrArray < Types . Schema > (
233243 outputConfig . schema ,
234244 ) ;
235245 let outputSpecificDocuments = normalizeInstanceOrArray < Types . OperationDocument > (
236246 outputConfig . documents ,
237247 ) ;
248+ let outputSpecificExternalDocuments =
249+ normalizeInstanceOrArray < Types . OperationDocument > ( outputConfig . externalDocuments ) ;
238250
239251 const preset : Types . OutputPreset | null = hasPreset
240252 ? typeof outputConfig . preset === 'string'
@@ -247,6 +259,10 @@ export async function executeCodegen(
247259 filename ,
248260 outputSpecificDocuments ,
249261 ) ;
262+ outputSpecificExternalDocuments = await preset . prepareDocuments (
263+ filename ,
264+ outputSpecificExternalDocuments ,
265+ ) ;
250266 }
251267
252268 return subTask . newListr (
@@ -308,41 +324,102 @@ export async function executeCodegen(
308324 task : wrapTask (
309325 async ( ) => {
310326 debugLog ( `[CLI] Loading Documents` ) ;
311- const documentPointerMap : any = { } ;
327+
328+ const populateDocumentPointerMap = (
329+ allDocumentsDenormalizedPointers : Types . OperationDocument [ ] ,
330+ ) : UnnormalizedTypeDefPointer => {
331+ const pointer : UnnormalizedTypeDefPointer = { } ;
332+ for ( const denormalizedPtr of allDocumentsDenormalizedPointers ) {
333+ if ( typeof denormalizedPtr === 'string' ) {
334+ pointer [ denormalizedPtr ] = { } ;
335+ } else if ( typeof denormalizedPtr === 'object' ) {
336+ Object . assign ( pointer , denormalizedPtr ) ;
337+ }
338+ }
339+ return pointer ;
340+ } ;
341+
312342 const allDocumentsDenormalizedPointers = [
313343 ...rootDocuments ,
314344 ...outputSpecificDocuments ,
315345 ] ;
316- for ( const denormalizedPtr of allDocumentsDenormalizedPointers ) {
317- if ( typeof denormalizedPtr === 'string' ) {
318- documentPointerMap [ denormalizedPtr ] = { } ;
319- } else if ( typeof denormalizedPtr === 'object' ) {
320- Object . assign ( documentPointerMap , denormalizedPtr ) ;
321- }
322- }
346+ const documentPointerMap = populateDocumentPointerMap (
347+ allDocumentsDenormalizedPointers ,
348+ ) ;
323349
324350 const hash = JSON . stringify ( documentPointerMap ) ;
325- const result = await cache ( 'documents' , hash , async ( ) => {
326- try {
327- const documents = await context . loadDocuments ( documentPointerMap ) ;
328- return {
329- documents,
330- } ;
331- } catch ( error : any ) {
332- if (
333- error instanceof NoTypeDefinitionsFound &&
334- config . ignoreNoDocuments
335- ) {
336- return {
337- documents : [ ] ,
338- } ;
351+ const outputDocumentsStandard = await cache (
352+ 'documents' ,
353+ hash ,
354+ async ( ) : Promise < Types . DocumentFile [ ] > => {
355+ try {
356+ const documents = await context . loadDocuments (
357+ documentPointerMap ,
358+ 'standard' ,
359+ ) ;
360+ return documents ;
361+ } catch ( error ) {
362+ if (
363+ error instanceof NoTypeDefinitionsFound &&
364+ config . ignoreNoDocuments
365+ ) {
366+ return [ ] ;
367+ }
368+ throw error ;
369+ }
370+ } ,
371+ ) ;
372+
373+ const allExternalDocumentsDenormalizedPointers = [
374+ ...rootExternalDocuments ,
375+ ...outputSpecificExternalDocuments ,
376+ ] ;
377+
378+ const externalDocumentsPointerMap = populateDocumentPointerMap (
379+ allExternalDocumentsDenormalizedPointers ,
380+ ) ;
381+
382+ const externalDocumentHash = JSON . stringify ( externalDocumentsPointerMap ) ;
383+ const outputExternalDocuments = await cache (
384+ 'documents' ,
385+ externalDocumentHash ,
386+ async ( ) : Promise < Types . DocumentFile [ ] > => {
387+ try {
388+ const documents = await context . loadDocuments (
389+ externalDocumentsPointerMap ,
390+ 'external' ,
391+ ) ;
392+ return documents ;
393+ } catch ( error ) {
394+ if (
395+ error instanceof NoTypeDefinitionsFound &&
396+ config . ignoreNoDocuments
397+ ) {
398+ return [ ] ;
399+ }
400+ throw error ;
339401 }
402+ } ,
403+ ) ;
340404
341- throw error ;
405+ /**
406+ * Merging `standard` and `external` documents here,
407+ * so they can be processed the same way,
408+ * before passed into presets and plugins
409+ */
410+ const processedFile : Record < string , true > = { } ;
411+ const mergedDocuments = [
412+ ...outputDocumentsStandard ,
413+ ...outputExternalDocuments ,
414+ ] ;
415+ for ( const file of mergedDocuments ) {
416+ if ( processedFile [ file . hash ] ) {
417+ continue ;
342418 }
343- } ) ;
344419
345- outputDocuments = result . documents ;
420+ outputDocuments . push ( file ) ;
421+ processedFile [ file . hash ] = true ;
422+ }
346423 } ,
347424 filename ,
348425 `Load GraphQL documents: ${ filename } ` ,
@@ -437,7 +514,7 @@ export async function executeCodegen(
437514 pluginContext,
438515 profiler : context . profiler ,
439516 documentTransforms,
440- } ,
517+ } satisfies Types . GenerateOptions ,
441518 ] ;
442519
443520 const process = async ( outputArgs : Types . GenerateOptions ) => {
0 commit comments