UNPKG

@graphql-codegen/near-operation-file-preset

Version:

GraphQL Code Generator preset for generating operation code near the operation file

39 lines (38 loc) 1.62 kB
import { join } from 'path'; 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 appendFileNameToFilePath(baseFilePath, fileName, extension) { const parsedPath = parsePath(baseFilePath); const name = fileName || parsedPath.name; return join(parsedPath.dir, name + extension).replace(/\\/g, '/'); } export function extractExternalFragmentsInUse(documentNode, fragmentNameToFile, result = {}, level = 0) { const ignoreList = new Set(); // First, take all fragments definition from the current file, and mark them as ignored oldVisit(documentNode, { enter: { FragmentDefinition: (node) => { ignoreList.add(node.name.value); }, }, }); // Then, look for all used fragments in this document oldVisit(documentNode, { enter: { FragmentSpread: (node) => { if (!ignoreList.has(node.name.value) && (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; }