@graphql-tools/delegate
Advanced tools
Comparing version 10.0.28 to 10.0.29-alpha-20241031093119-758db604a5159bc55caa775bd733e0322cfb67ec
@@ -90,12 +90,35 @@ "use strict"; | ||
}); | ||
function getByPath(object, path) { | ||
let current = object; | ||
for (const pathSegment of path) { | ||
if (current == null) { | ||
return; | ||
function fieldExistsInSelectionSet(node, path) { | ||
let currentNode = node; | ||
const isArrayOfSelectionNodes = (node) => Array.isArray(node); | ||
for (const [segment, fieldName] of path) { | ||
if (!currentNode) { | ||
return false; | ||
} | ||
current = current[pathSegment]; | ||
if (isArrayOfSelectionNodes(currentNode) && fieldName) { | ||
currentNode = currentNode.find(selectionNode => { | ||
return selectionNode.name?.value === fieldName; | ||
}); | ||
} | ||
else { | ||
currentNode = currentNode[segment]; | ||
} | ||
} | ||
return current; | ||
return !!currentNode; | ||
} | ||
function getPathWithFieldNames(node, path) { | ||
const pathWithFieldNames = []; | ||
let currentNode = node; | ||
const isArrayOfSelectionNodes = (node) => Array.isArray(node); | ||
for (const segment of path) { | ||
currentNode = currentNode[segment]; | ||
if (!isArrayOfSelectionNodes(currentNode) && currentNode.kind === graphql_1.Kind.FIELD) { | ||
pathWithFieldNames.push([segment, currentNode.name.value]); | ||
} | ||
else { | ||
pathWithFieldNames.push([segment]); | ||
} | ||
} | ||
return pathWithFieldNames; | ||
} | ||
function subtractSelectionSets(selectionSetA, selectionSetB) { | ||
@@ -105,5 +128,8 @@ return (0, graphql_1.visit)(selectionSetA, { | ||
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; | ||
if (!node.selectionSet) { | ||
const pathWithFieldNames = getPathWithFieldNames(selectionSetA, path); | ||
const fieldExists = fieldExistsInSelectionSet(selectionSetB, pathWithFieldNames); | ||
if (fieldExists) { | ||
return null; | ||
} | ||
} | ||
@@ -110,0 +136,0 @@ }, |
@@ -87,12 +87,35 @@ import { getNamedType, isAbstractType, isInterfaceType, isLeafType, isObjectType, isUnionType, Kind, visit, } from 'graphql'; | ||
}); | ||
function getByPath(object, path) { | ||
let current = object; | ||
for (const pathSegment of path) { | ||
if (current == null) { | ||
return; | ||
function fieldExistsInSelectionSet(node, path) { | ||
let currentNode = node; | ||
const isArrayOfSelectionNodes = (node) => Array.isArray(node); | ||
for (const [segment, fieldName] of path) { | ||
if (!currentNode) { | ||
return false; | ||
} | ||
current = current[pathSegment]; | ||
if (isArrayOfSelectionNodes(currentNode) && fieldName) { | ||
currentNode = currentNode.find(selectionNode => { | ||
return selectionNode.name?.value === fieldName; | ||
}); | ||
} | ||
else { | ||
currentNode = currentNode[segment]; | ||
} | ||
} | ||
return current; | ||
return !!currentNode; | ||
} | ||
function getPathWithFieldNames(node, path) { | ||
const pathWithFieldNames = []; | ||
let currentNode = node; | ||
const isArrayOfSelectionNodes = (node) => Array.isArray(node); | ||
for (const segment of path) { | ||
currentNode = currentNode[segment]; | ||
if (!isArrayOfSelectionNodes(currentNode) && currentNode.kind === Kind.FIELD) { | ||
pathWithFieldNames.push([segment, currentNode.name.value]); | ||
} | ||
else { | ||
pathWithFieldNames.push([segment]); | ||
} | ||
} | ||
return pathWithFieldNames; | ||
} | ||
export function subtractSelectionSets(selectionSetA, selectionSetB) { | ||
@@ -102,5 +125,8 @@ return visit(selectionSetA, { | ||
enter(node, _key, _parent, path) { | ||
if (!node.selectionSet && | ||
getByPath(selectionSetB, path.slice(0, -1))?.some(selection => selection.kind === Kind.FIELD && selection.name.value === node.name.value)) { | ||
return null; | ||
if (!node.selectionSet) { | ||
const pathWithFieldNames = getPathWithFieldNames(selectionSetA, path); | ||
const fieldExists = fieldExistsInSelectionSet(selectionSetB, pathWithFieldNames); | ||
if (fieldExists) { | ||
return null; | ||
} | ||
} | ||
@@ -107,0 +133,0 @@ }, |
{ | ||
"name": "@graphql-tools/delegate", | ||
"version": "10.0.28", | ||
"version": "10.0.29-alpha-20241031093119-758db604a5159bc55caa775bd733e0322cfb67ec", | ||
"description": "A set of utils for faster development of GraphQL tools", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
243414
4764
2