import { join } from 'path' ; import { DocumentNode , FragmentSpreadNode , FragmentDefinitionNode } from 'graphql' ; import { FragmentRegistry } from './fragment-resolver' ; import parsePath from 'parse-filepath' ; import { oldVisit } from '@graphql-codegen/plugin-helpers' ; export function defineFilepathSubfolder ( baseFilePath , folder ) { const parsedPath = parsePath ( baseFilePath ) ; return join ( parsedPath . dir , folder , parsedPath . base ) . replace ( /\\/g , '/' ) ; } export function appendExtensionToFilePath ( baseFilePath , extension ) { const parsedPath = parsePath ( baseFilePath ) ; return join ( parsedPath . dir , parsedPath . name + extension ) . replace ( /\\/g , '/' ) ; } export function extractExternalFragmentsInUse ( documentNode , fragmentNameToFile , result = { } , level = 0 ) { const ignoreList = new Set ( ) ; oldVisit ( documentNode , { enter : { FragmentDefinition : ( node ) => { ignoreList . add ( node . name . value ) ; } , } , } ) ; oldVisit ( documentNode , { enter : { FragmentSpread : ( node ) => { if ( ! ignoreList . has ( node . name . value ) ) { if ( result [ node . name . value ] === undefined || ( result [ node . name . value ] !== undefined && level < result [ node . name . value ] ) ) { result [ node . name . value ] = level ; if ( fragmentNameToFile [ node . name . value ] ) { extractExternalFragmentsInUse ( fragmentNameToFile [ node . name . value ] . node , fragmentNameToFile , result , level + 1 ) ; } } } } , } , } ) ; return result ; }