diff --git a/imports/client.tsx b/imports/client.tsx index cf3ff3ba..a480d6e4 100644 --- a/imports/client.tsx +++ b/imports/client.tsx @@ -2,7 +2,7 @@ import atob from 'atob'; import { gql, useQuery, useSubscription, useApolloClient } from '@apollo/client/index.js'; import type { ApolloQueryResult } from '@apollo/client/index.js'; import { generateApolloClient, IApolloClient } from '@deep-foundation/hasura/client.js'; -import { useLocalStore } from '@deep-foundation/store/local.js'; +import { useLocalStore } from '@deep-foundation/store/local.js'; import React, { createContext, useContext, useEffect, useMemo, useState } from "react"; import { deprecate, inherits, inspect } from "util"; import { deleteMutation, generateMutation, generateQuery, generateQueryData, generateSerial, IGenerateMutationBuilder, IGenerateMutationOptions, insertMutation, ISerialResult, updateMutation } from './gql/index.js'; @@ -511,6 +511,7 @@ export type SerialOperation< > = { type: TSerialOperationType; table: TTable; + resultAlias?: string; } & SerialOperationDetails; export type DeepSerialOperation = SerialOperation> @@ -789,7 +790,7 @@ export class DeepClient> implements DeepClientInstance { LL = L >({ name, operations, returning, silent - }: AsyncSerialParams): Promise> { + }: AsyncSerialParams): Promise>>> { // @ts-ignore let operationsGroupedByTypeAndTable: Record>> = {}; operationsGroupedByTypeAndTable = operations.reduce((acc, operation) => { @@ -846,8 +847,12 @@ export class DeepClient> implements DeepClientInstance { if (!silent) throw e; return { ...result, data: (result)?.data?.m0?.returning, error: e }; } - // @ts-ignore - return { ...result, data: (result)?.data && Object.values((result)?.data).flatMap(m => m.returning)}; + + return { ...result, data: (result)?.data && Object.entries((result)?.data).reduce>((returningPerAlias, [alias, m]) => { + // @ts-ignore + returningPerAlias[alias] = m.returning; + return returningPerAlias + }, {})}; }; reserve(count: number): Promise { @@ -1144,4 +1149,4 @@ export interface UseDeepSubscriptionResult> { data?: LL[]; error?: any; loading: boolean; -} \ No newline at end of file +} diff --git a/imports/gql/serial.ts b/imports/gql/serial.ts index faf3f571..1815a50f 100644 --- a/imports/gql/serial.ts +++ b/imports/gql/serial.ts @@ -59,6 +59,7 @@ export function createSerialOperation< >(params: { type: TSerialOperationType; table: TTable; + resultAlias?: string; } & SerialOperationDetails): SerialOperation { return params; } \ No newline at end of file diff --git a/tests/client.tsx b/tests/client.tsx index 0ad22945..0e5bd25d 100644 --- a/tests/client.tsx +++ b/tests/client.tsx @@ -241,9 +241,11 @@ describe('client', () => { it('one insert', async () => { let linkIdsToDelete = []; const typeTypeLinkId = await deepClient.id("@deep-foundation/core", "Type"); + const resultAlias = "resultAlias"; const operation = createSerialOperation({ table: 'links', type: 'insert', + resultAlias, objects: { type_id: typeTypeLinkId } @@ -256,7 +258,7 @@ describe('client', () => { }); assert.equal(result.error, undefined); assert.notEqual(result.data, undefined); - linkIdsToDelete = [...linkIdsToDelete, ...result.data.map(link => link.id)]; + linkIdsToDelete = [...linkIdsToDelete, ...result.data[resultAlias].map(link => link.id)]; assert.strictEqual(result.data.length, 1); } finally { await deepClient.delete(linkIdsToDelete) @@ -265,9 +267,11 @@ describe('client', () => { it('multiple inserts in one operation', async () => { let linkIdsToDelete = []; const typeTypeLinkId = await deepClient.id("@deep-foundation/core", "Type"); + const resultAlias = "resultAlias"; const operation = createSerialOperation({ table: 'links', type: 'insert', + resultAlias, objects: { type_id: typeTypeLinkId } @@ -281,7 +285,7 @@ describe('client', () => { }); assert.equal(result.error, undefined); assert.notEqual(result.data, undefined); - linkIdsToDelete = [...linkIdsToDelete, ...result.data.map(link => link.id)]; + linkIdsToDelete = [...linkIdsToDelete, ...result.data[resultAlias].map(link => link.id)]; assert.strictEqual(result.data.length, 2); } finally { await deepClient.delete(linkIdsToDelete) @@ -290,9 +294,11 @@ describe('client', () => { it('multiple inserts ine multiple operations', async () => { let linkIdsToDelete = []; const typeTypeLinkId = await deepClient.id("@deep-foundation/core", "Type"); + const resultAlias = "resultAlias"; const operation = createSerialOperation({ table: 'links', type: 'insert', + resultAlias, objects: { type_id: typeTypeLinkId } @@ -307,7 +313,7 @@ describe('client', () => { inspect(result); assert.equal(result.error, undefined); assert.notEqual(result.data, undefined); - linkIdsToDelete = [...linkIdsToDelete, ...result.data.map(link => link.id)]; + linkIdsToDelete = [...linkIdsToDelete, ...result.data[resultAlias].map(link => link.id)]; assert.strictEqual(result.data.length, 2); } finally { await deepClient.delete(linkIdsToDelete)