@graphql-tools/utils
Advanced tools
Comparing version 10.2.2-alpha-20240605151259-eaff57b1d278951caa3ecec956bbad9655a042d1 to 10.2.2-alpha-20240605195134-d80e71b21702e5acb20a2d5319300a3813ff01cd
@@ -6,26 +6,9 @@ "use strict"; | ||
function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
if (respectArrays && respectArrayLength) { | ||
let expectedLength; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
expectedLength = source.length; | ||
return true; | ||
} | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
if (areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
const target = sources[0] || {}; | ||
const output = {}; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target))); | ||
} | ||
for (const source of sources) { | ||
if (isObject(source)) { | ||
if (isObject(target) && isObject(source)) { | ||
if (respectPrototype) { | ||
@@ -49,3 +32,3 @@ const outputPrototype = Object.getPrototypeOf(output); | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays); | ||
} | ||
@@ -55,8 +38,3 @@ } | ||
if (Array.isArray(source[key])) { | ||
if (respectArrayLength && output[key].length === source[key].length) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
output[key].push(...source[key]); | ||
} | ||
output[key].push(...source[key]); | ||
} | ||
@@ -72,2 +50,16 @@ else { | ||
} | ||
else if (respectArrays && Array.isArray(target)) { | ||
if (Array.isArray(source)) { | ||
if (respectArrayLength && source.length === target.length) { | ||
return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
target.push(...source); | ||
} | ||
else { | ||
target.push(source); | ||
} | ||
} | ||
else if (respectArrays && Array.isArray(source)) { | ||
return [target, ...source]; | ||
} | ||
} | ||
@@ -74,0 +66,0 @@ return output; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mergeIncrementalResult = void 0; | ||
const tslib_1 = require("tslib"); | ||
const dlv_1 = tslib_1.__importDefault(require("dlv")); | ||
const merge_1 = require("dset/merge"); | ||
const pathsMap = new WeakMap(); | ||
function mergeIncrementalResult({ incrementalResult, executionResult, }) { | ||
const path = ['data', ...(incrementalResult.path ?? [])]; | ||
let path = [ | ||
'data', | ||
...(incrementalResult.path ?? []), | ||
]; | ||
if (incrementalResult.pending) { | ||
let paths = pathsMap.get(executionResult); | ||
if (paths === undefined) { | ||
paths = new Map(); | ||
pathsMap.set(executionResult, paths); | ||
} | ||
for (const { id, path } of incrementalResult.pending) { | ||
paths.set(id, ['data', ...path]); | ||
} | ||
} | ||
if (incrementalResult.pending) { | ||
const paths = pathsMap.get(executionResult); | ||
for (const { id, path } of incrementalResult.pending) { | ||
if (id !== undefined) { | ||
if (paths === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
paths.set(id, ['data', ...path]); | ||
} | ||
} | ||
} | ||
if (incrementalResult.items) { | ||
for (const item of incrementalResult.items) { | ||
(0, merge_1.dset)(executionResult, path, item); | ||
// Increment the last path segment (the array index) to merge the next item at the next index | ||
path[path.length - 1]++; | ||
if (incrementalResult.id) { | ||
const id = incrementalResult.id; | ||
path = pathsMap.get(executionResult)?.get(id); | ||
if (path === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
const list = (0, dlv_1.default)(executionResult, path); | ||
list.push(...incrementalResult.items); | ||
} | ||
else { | ||
const path = ['data', ...(incrementalResult.path ?? [])]; | ||
for (const item of incrementalResult.items) { | ||
(0, merge_1.dset)(executionResult, path, item); | ||
// Increment the last path segment (the array index) to merge the next item at the next index | ||
path[path.length - 1]++; | ||
} | ||
} | ||
} | ||
if (incrementalResult.data) { | ||
if (incrementalResult.id) { | ||
const id = incrementalResult.id; | ||
if (id !== undefined) { | ||
path = pathsMap.get(executionResult)?.get(id); | ||
if (path === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
} | ||
} | ||
(0, merge_1.dset)(executionResult, path, incrementalResult.data); | ||
@@ -32,3 +80,13 @@ } | ||
} | ||
if (incrementalResult.completed) { | ||
// Remove tracking and add additional errors | ||
for (const { id, errors } of incrementalResult.completed) { | ||
pathsMap.get(executionResult)?.delete(id); | ||
if (errors) { | ||
executionResult.errors = executionResult.errors || []; | ||
executionResult.errors.push(...errors); | ||
} | ||
} | ||
} | ||
} | ||
exports.mergeIncrementalResult = mergeIncrementalResult; |
import { isSome } from './helpers.js'; | ||
export function mergeDeep(sources, respectPrototype = false, respectArrays = false, respectArrayLength = false) { | ||
if (respectArrays && respectArrayLength) { | ||
let expectedLength; | ||
const areArraysInTheSameLength = sources.every(source => { | ||
if (Array.isArray(source)) { | ||
if (expectedLength === undefined) { | ||
expectedLength = source.length; | ||
return true; | ||
} | ||
else if (expectedLength === source.length) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}); | ||
if (areArraysInTheSameLength) { | ||
return new Array(expectedLength).fill(null).map((_, index) => mergeDeep(sources.map(source => source[index]), respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
} | ||
const target = sources[0] || {}; | ||
const output = {}; | ||
if (respectPrototype) { | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(sources[0]))); | ||
Object.setPrototypeOf(output, Object.create(Object.getPrototypeOf(target))); | ||
} | ||
for (const source of sources) { | ||
if (isObject(source)) { | ||
if (isObject(target) && isObject(source)) { | ||
if (respectPrototype) { | ||
@@ -45,3 +28,3 @@ const outputPrototype = Object.getPrototypeOf(output); | ||
else { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays); | ||
} | ||
@@ -51,8 +34,3 @@ } | ||
if (Array.isArray(source[key])) { | ||
if (respectArrayLength && output[key].length === source[key].length) { | ||
output[key] = mergeDeep([output[key], source[key]], respectPrototype, respectArrays, respectArrayLength); | ||
} | ||
else { | ||
output[key].push(...source[key]); | ||
} | ||
output[key].push(...source[key]); | ||
} | ||
@@ -68,2 +46,16 @@ else { | ||
} | ||
else if (respectArrays && Array.isArray(target)) { | ||
if (Array.isArray(source)) { | ||
if (respectArrayLength && source.length === target.length) { | ||
return target.map((targetElem, i) => mergeDeep([targetElem, source[i]], respectPrototype, respectArrays, respectArrayLength)); | ||
} | ||
target.push(...source); | ||
} | ||
else { | ||
target.push(source); | ||
} | ||
} | ||
else if (respectArrays && Array.isArray(source)) { | ||
return [target, ...source]; | ||
} | ||
} | ||
@@ -70,0 +62,0 @@ return output; |
@@ -0,12 +1,59 @@ | ||
import dlv from 'dlv'; | ||
import { dset } from 'dset/merge'; | ||
const pathsMap = new WeakMap(); | ||
export function mergeIncrementalResult({ incrementalResult, executionResult, }) { | ||
const path = ['data', ...(incrementalResult.path ?? [])]; | ||
let path = [ | ||
'data', | ||
...(incrementalResult.path ?? []), | ||
]; | ||
if (incrementalResult.pending) { | ||
let paths = pathsMap.get(executionResult); | ||
if (paths === undefined) { | ||
paths = new Map(); | ||
pathsMap.set(executionResult, paths); | ||
} | ||
for (const { id, path } of incrementalResult.pending) { | ||
paths.set(id, ['data', ...path]); | ||
} | ||
} | ||
if (incrementalResult.pending) { | ||
const paths = pathsMap.get(executionResult); | ||
for (const { id, path } of incrementalResult.pending) { | ||
if (id !== undefined) { | ||
if (paths === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
paths.set(id, ['data', ...path]); | ||
} | ||
} | ||
} | ||
if (incrementalResult.items) { | ||
for (const item of incrementalResult.items) { | ||
dset(executionResult, path, item); | ||
// Increment the last path segment (the array index) to merge the next item at the next index | ||
path[path.length - 1]++; | ||
if (incrementalResult.id) { | ||
const id = incrementalResult.id; | ||
path = pathsMap.get(executionResult)?.get(id); | ||
if (path === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
const list = dlv(executionResult, path); | ||
list.push(...incrementalResult.items); | ||
} | ||
else { | ||
const path = ['data', ...(incrementalResult.path ?? [])]; | ||
for (const item of incrementalResult.items) { | ||
dset(executionResult, path, item); | ||
// Increment the last path segment (the array index) to merge the next item at the next index | ||
path[path.length - 1]++; | ||
} | ||
} | ||
} | ||
if (incrementalResult.data) { | ||
if (incrementalResult.id) { | ||
const id = incrementalResult.id; | ||
if (id !== undefined) { | ||
path = pathsMap.get(executionResult)?.get(id); | ||
if (path === undefined) { | ||
throw new Error('Invalid incremental delivery format.'); | ||
} | ||
} | ||
} | ||
dset(executionResult, path, incrementalResult.data); | ||
@@ -29,2 +76,12 @@ } | ||
} | ||
if (incrementalResult.completed) { | ||
// Remove tracking and add additional errors | ||
for (const { id, errors } of incrementalResult.completed) { | ||
pathsMap.get(executionResult)?.delete(id); | ||
if (errors) { | ||
executionResult.errors = executionResult.errors || []; | ||
executionResult.errors.push(...errors); | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "@graphql-tools/utils", | ||
"version": "10.2.2-alpha-20240605151259-eaff57b1d278951caa3ecec956bbad9655a042d1", | ||
"version": "10.2.2-alpha-20240605195134-d80e71b21702e5acb20a2d5319300a3813ff01cd", | ||
"description": "Common package containing utils and types for GraphQL tools", | ||
@@ -12,2 +12,3 @@ "sideEffects": false, | ||
"cross-inspect": "1.0.0", | ||
"dlv": "^1.1.3", | ||
"dset": "^3.1.2", | ||
@@ -14,0 +15,0 @@ "tslib": "^2.4.0" |
@@ -20,2 +20,11 @@ import { DefinitionNode, DocumentNode, EnumTypeDefinitionNode, EnumTypeExtensionNode, FieldDefinitionNode, FieldNode, FragmentDefinitionNode, GraphQLArgument, GraphQLArgumentConfig, GraphQLDirective, GraphQLEnumType, GraphQLEnumValue, GraphQLEnumValueConfig, GraphQLError, GraphQLField, GraphQLFieldConfig, GraphQLInputField, GraphQLInputFieldConfig, GraphQLInputObjectType, GraphQLInputType, GraphQLInterfaceType, GraphQLIsTypeOfFn, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLResolveInfo, GraphQLScalarLiteralParser, GraphQLScalarSerializer, GraphQLScalarType, GraphQLScalarValueParser, GraphQLSchema, GraphQLType, GraphQLTypeResolver, GraphQLUnionType, InputObjectTypeDefinitionNode, InputObjectTypeExtensionNode, InterfaceTypeDefinitionNode, InterfaceTypeExtensionNode, ObjectTypeDefinitionNode, ObjectTypeExtensionNode, OperationTypeNode, ScalarTypeDefinitionNode, ScalarTypeExtensionNode, SelectionNode, Source, UnionTypeDefinitionNode, UnionTypeExtensionNode } from 'graphql'; | ||
items?: TData | null; | ||
id?: string; | ||
pending?: ReadonlyArray<{ | ||
id: string; | ||
path: ReadonlyArray<string | number>; | ||
}>; | ||
completed?: ReadonlyArray<{ | ||
id: string; | ||
errors?: ReadonlyArray<GraphQLError>; | ||
}>; | ||
} | ||
@@ -22,0 +31,0 @@ export interface ExecutionRequest<TVariables extends Record<string, any> = any, TContext = any, TRootValue = any, TExtensions = Record<string, any>, TReturn = any> { |
Sorry, the diff of this file is not supported yet
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
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
505154
10957
6
+ Addeddlv@^1.1.3
+ Addeddlv@1.1.3(transitive)