@graphql-tools/delegate
Version:
A set of utils for faster development of GraphQL tools
120 lines (119 loc) • 5.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.subtractSelectionSets = exports.extractUnavailableFields = exports.extractUnavailableFieldsFromSelectionSet = void 0;
const graphql_1 = require("graphql");
const utils_1 = require("@graphql-tools/utils");
exports.extractUnavailableFieldsFromSelectionSet = (0, utils_1.memoize4)(function extractUnavailableFieldsFromSelectionSet(schema, fieldType, fieldSelectionSet, shouldAdd) {
if ((0, graphql_1.isLeafType)(fieldType)) {
return [];
}
if ((0, graphql_1.isUnionType)(fieldType)) {
const unavailableSelections = [];
for (const type of fieldType.getTypes()) {
// Exclude other inline fragments
const fieldSelectionExcluded = {
...fieldSelectionSet,
selections: fieldSelectionSet.selections.filter(selection => selection.kind === graphql_1.Kind.INLINE_FRAGMENT
? selection.typeCondition
? selection.typeCondition.name.value === type.name
: false
: true),
};
unavailableSelections.push(...extractUnavailableFieldsFromSelectionSet(schema, type, fieldSelectionExcluded, shouldAdd));
}
return unavailableSelections;
}
const subFields = fieldType.getFields();
const unavailableSelections = [];
for (const selection of fieldSelectionSet.selections) {
if (selection.kind === graphql_1.Kind.FIELD) {
if (selection.name.value === '__typename') {
continue;
}
const fieldName = selection.name.value;
const selectionField = subFields[fieldName];
if (!selectionField) {
if (shouldAdd(fieldType, selection)) {
unavailableSelections.push(selection);
}
}
else {
const unavailableSubFields = (0, exports.extractUnavailableFields)(schema, selectionField, selection, shouldAdd);
if (unavailableSubFields.length) {
unavailableSelections.push({
...selection,
selectionSet: {
kind: graphql_1.Kind.SELECTION_SET,
selections: unavailableSubFields,
},
});
}
}
}
else if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT) {
const subFieldType = selection.typeCondition
? schema.getType(selection.typeCondition.name.value)
: fieldType;
if (subFieldType === fieldType ||
(((0, graphql_1.isObjectType)(subFieldType) || (0, graphql_1.isInterfaceType)(subFieldType)) &&
(0, graphql_1.isAbstractType)(fieldType) &&
schema.isSubType(fieldType, subFieldType))) {
const unavailableFields = extractUnavailableFieldsFromSelectionSet(schema, subFieldType, selection.selectionSet, shouldAdd);
if (unavailableFields.length) {
unavailableSelections.push({
...selection,
selectionSet: {
kind: graphql_1.Kind.SELECTION_SET,
selections: unavailableFields,
},
});
}
}
}
}
return unavailableSelections;
});
exports.extractUnavailableFields = (0, utils_1.memoize4)(function extractUnavailableFields(schema, field, fieldNode, shouldAdd) {
if (fieldNode.selectionSet) {
const fieldType = (0, graphql_1.getNamedType)(field.type);
return (0, exports.extractUnavailableFieldsFromSelectionSet)(schema, fieldType, fieldNode.selectionSet, shouldAdd);
}
return [];
});
function getByPath(object, path) {
let current = object;
for (const pathSegment of path) {
if (current == null) {
return;
}
current = current[pathSegment];
}
return current;
}
function subtractSelectionSets(selectionSetA, selectionSetB) {
return (0, graphql_1.visit)(selectionSetA, {
[graphql_1.Kind.FIELD]: {
enter(node, _key, _parent, path) {
if (!node.selectionSet &&
getByPath(selectionSetB, path.slice(0, -1))?.some(selection => selection.kind === graphql_1.Kind.FIELD && selection.name.value === node.name.value)) {
return null;
}
},
},
[graphql_1.Kind.SELECTION_SET]: {
leave(node) {
if (node.selections.length === 0) {
return null;
}
},
},
[graphql_1.Kind.INLINE_FRAGMENT]: {
leave(node) {
if (node.selectionSet?.selections.length === 0) {
return null;
}
},
},
});
}
exports.subtractSelectionSets = subtractSelectionSets;