@graphql-codegen/near-operation-file-preset
Version:
GraphQL Code Generator preset for generating operation code near the operation file
149 lines (148 loc) • 6.74 kB
JavaScript
import { join } from 'path';
import { isInterfaceType, isObjectType, isUnionType, Kind, TypeInfo, visit, visitWithTypeInfo, } from 'graphql';
import parsePath from 'parse-filepath';
import { getPossibleTypes, separateSelectionSet } from '@graphql-codegen/visitor-plugin-common';
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, '/');
}
/**
* Analyzes fragment usage in a GraphQL document.
* Returns information about which fragments are used and which specific types they're used with.
*/
export function analyzeFragmentUsage(documentNode, fragmentRegistry, schema) {
const localFragments = getLocalFragments(documentNode);
const fragmentsInUse = extractExternalFragmentsInUse(documentNode, fragmentRegistry, localFragments);
const usedFragmentTypes = analyzeFragmentTypeUsage(documentNode, fragmentRegistry, schema, localFragments, fragmentsInUse);
const operationFragments = collectOperationFragments(documentNode, fragmentRegistry, localFragments);
return { fragmentsInUse, usedFragmentTypes, operationFragments };
}
/**
* External fragments transitively reachable from the document's operations (graphQLTag operations
* interpolate all of these). Steps through local fragments too, since an operation may reach an
* external fragment via a local one.
*/
function collectOperationFragments(documentNode, fragmentRegistry, localFragments) {
const reachable = new Set();
const visited = new Set();
const collect = (node) => {
visit(node, {
FragmentSpread: ({ name: { value } }) => {
var _a, _b;
if (fragmentRegistry[value])
reachable.add(value);
if (visited.has(value))
return;
visited.add(value);
const fragmentNode = (_b = (_a = fragmentRegistry[value]) === null || _a === void 0 ? void 0 : _a.node) !== null && _b !== void 0 ? _b : localFragments.get(value);
if (fragmentNode)
collect(fragmentNode);
},
});
};
for (const definition of documentNode.definitions) {
if (definition.kind === Kind.OPERATION_DEFINITION)
collect(definition);
}
return reachable;
}
/**
* Get all fragment definitions that are local to this document, keyed by name
*/
function getLocalFragments(documentNode) {
const localFragments = new Map();
visit(documentNode, {
FragmentDefinition: node => {
localFragments.set(node.name.value, node);
},
});
return localFragments;
}
function extractExternalFragmentsInUse(documentNode, fragmentNameToFile, localFragment, result = {}, level = 0) {
// Then, look for all used fragments in this document
visit(documentNode, {
FragmentSpread: node => {
if (!localFragment.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, localFragment, result, level + 1);
}
}
},
});
return result;
}
/**
* Analyze which specific types each fragment is used with (for polymorphic fragments)
*/
function analyzeFragmentTypeUsage(documentNode, fragmentRegistry, schema, localFragments, fragmentsInUse) {
const usedFragmentTypes = {};
const typeInfo = new TypeInfo(schema);
visit(documentNode, visitWithTypeInfo(typeInfo, {
Field: (node) => {
if (!node.selectionSet)
return;
const fieldType = typeInfo.getType();
if (!fieldType)
return;
const baseType = getBaseType(fieldType);
if (isObjectType(baseType) || isInterfaceType(baseType) || isUnionType(baseType)) {
analyzeSelectionSetTypeContext(node.selectionSet, baseType.name, usedFragmentTypes, fragmentRegistry, schema, localFragments);
}
},
}));
const result = {};
// Fill in missing types for multi-type fragments
for (const fragmentName in fragmentsInUse) {
const fragment = fragmentRegistry[fragmentName];
if (!fragment || fragment.possibleTypes.length <= 1)
continue;
const usedTypes = usedFragmentTypes[fragmentName];
result[fragmentName] = (usedTypes === null || usedTypes === void 0 ? void 0 : usedTypes.size) > 0 ? Array.from(usedTypes) : fragment.possibleTypes;
}
return result;
}
/**
* Analyze fragment usage within a specific selection set and type context
*/
function analyzeSelectionSetTypeContext(selectionSet, currentTypeName, usedFragmentTypes, fragmentRegistry, schema, localFragments) {
var _a, _b;
var _c;
const { spreads, inlines } = separateSelectionSet(selectionSet.selections);
// Process fragment spreads in this type context
for (const spread of spreads) {
if (localFragments.has(spread.name.value))
continue;
const fragment = fragmentRegistry[spread.name.value];
if (!fragment || fragment.possibleTypes.length <= 1)
continue;
const currentType = schema.getType(currentTypeName);
if (!currentType)
continue;
const possibleTypes = getPossibleTypes(schema, currentType).map(t => t.name);
const matchingTypes = possibleTypes.filter(type => fragment.possibleTypes.includes(type));
if (matchingTypes.length > 0) {
const typeSet = ((_a = usedFragmentTypes[_c = spread.name.value]) !== null && _a !== void 0 ? _a : (usedFragmentTypes[_c] = new Set()));
matchingTypes.forEach(type => typeSet.add(type));
}
}
// Process inline fragments
for (const inline of inlines) {
if (((_b = inline.typeCondition) === null || _b === void 0 ? void 0 : _b.name.value) && inline.selectionSet) {
analyzeSelectionSetTypeContext(inline.selectionSet, inline.typeCondition.name.value, usedFragmentTypes, fragmentRegistry, schema, localFragments);
}
}
}
function getBaseType(type) {
let baseType = type;
while ('ofType' in baseType && baseType.ofType) {
baseType = baseType.ofType;
}
return baseType;
}