@graphql-tools/utils
Advanced tools
Comparing version 10.3.0-alpha-20240705110827-1fef94a25ba18b5ae498ae9aa88fde050ae610cd to 10.3.0-alpha-20240708182941-ed27153e85ecdb79083b1d124d470573a65893fd
@@ -62,1 +62,2 @@ "use strict"; | ||
tslib_1.__exportStar(require("./debugTimer.js"), exports); | ||
tslib_1.__exportStar(require("./createDeferred.js"), exports); |
"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 ?? []), | ||
]; | ||
for (const result of [executionResult, incrementalResult]) { | ||
if (result.pending) { | ||
let paths = pathsMap.get(executionResult); | ||
if (paths === undefined) { | ||
paths = new Map(); | ||
pathsMap.set(executionResult, paths); | ||
} | ||
for (const { id, path } of result.pending) { | ||
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 +71,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; |
@@ -57,1 +57,2 @@ export * from './loaders.js'; | ||
export * from './debugTimer.js'; | ||
export * from './createDeferred.js'; |
@@ -0,12 +1,50 @@ | ||
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 ?? []), | ||
]; | ||
for (const result of [executionResult, incrementalResult]) { | ||
if (result.pending) { | ||
let paths = pathsMap.get(executionResult); | ||
if (paths === undefined) { | ||
paths = new Map(); | ||
pathsMap.set(executionResult, paths); | ||
} | ||
for (const { id, path } of result.pending) { | ||
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 +67,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.3.0-alpha-20240705110827-1fef94a25ba18b5ae498ae9aa88fde050ae610cd", | ||
"version": "10.3.0-alpha-20240708182941-ed27153e85ecdb79083b1d124d470573a65893fd", | ||
"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" |
@@ -7,8 +7,1 @@ import { ExecutionRequest, ExecutionResult } from './Interfaces.js'; | ||
export type Executor<TBaseContext = Record<string, any>, TBaseExtensions = Record<string, any>> = <TReturn = any, TArgs extends Record<string, any> = Record<string, any>, TContext extends TBaseContext = TBaseContext, TRoot = any, TExtensions extends TBaseExtensions = TBaseExtensions>(request: ExecutionRequest<TArgs, TContext, TRoot, TExtensions, TReturn>) => MaybePromise<MaybeAsyncIterable<ExecutionResult<TReturn>>>; | ||
export type DisposableSyncExecutor<TBaseContext = Record<string, any>, TBaseExtensions = Record<string, any>> = SyncExecutor<TBaseContext, TBaseExtensions> & { | ||
[Symbol.dispose]?: () => void; | ||
}; | ||
export type DisposableAsyncExecutor<TBaseContext = Record<string, any>, TBaseExtensions = Record<string, any>> = AsyncExecutor<TBaseContext, TBaseExtensions> & { | ||
[Symbol.dispose]?: () => void; | ||
}; | ||
export type DisposableExecutor<TBaseContext = Record<string, any>, TBaseExtensions = Record<string, any>> = DisposableSyncExecutor<TBaseContext, TBaseExtensions> | DisposableAsyncExecutor<TBaseContext, TBaseExtensions>; |
@@ -57,1 +57,2 @@ export * from './loaders.js'; | ||
export * from './debugTimer.js'; | ||
export * from './createDeferred.js'; |
@@ -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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
508893
246
11046
6
+ Addeddlv@^1.1.3
+ Addeddlv@1.1.3(transitive)