Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return returning per alias instead of array of returnings #180

Draft
wants to merge 6 commits into
base: main-broken
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions imports/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -511,6 +511,7 @@ export type SerialOperation<
> = {
type: TSerialOperationType;
table: TTable;
resultAlias?: string;
} & SerialOperationDetails<TSerialOperationType, TTable>;

export type DeepSerialOperation = SerialOperation<SerialOperationType, Table<SerialOperationType>>
Expand Down Expand Up @@ -789,7 +790,7 @@ export class DeepClient<L = Link<number>> implements DeepClientInstance<L> {
LL = L
>({
name, operations, returning, silent
}: AsyncSerialParams): Promise<DeepClientResult<{ id: number }[]>> {
}: AsyncSerialParams): Promise<DeepClientResult<Record<string, Array<{ id: number }>>>> {
// @ts-ignore
let operationsGroupedByTypeAndTable: Record<SerialOperationType, Record<Table, Array<SerialOperation>>> = {};
operationsGroupedByTypeAndTable = operations.reduce((acc, operation) => {
Expand Down Expand Up @@ -846,8 +847,12 @@ export class DeepClient<L = Link<number>> implements DeepClientInstance<L> {
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<Record<string, any>>((returningPerAlias, [alias, m]) => {
// @ts-ignore
returningPerAlias[alias] = m.returning;
return returningPerAlias
}, {})};
};

reserve<LL = L>(count: number): Promise<number[]> {
Expand Down Expand Up @@ -1144,4 +1149,4 @@ export interface UseDeepSubscriptionResult<LL = Link<number>> {
data?: LL[];
error?: any;
loading: boolean;
}
}
1 change: 1 addition & 0 deletions imports/gql/serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export function createSerialOperation<
>(params: {
type: TSerialOperationType;
table: TTable;
resultAlias?: string;
} & SerialOperationDetails<TSerialOperationType, TTable>): SerialOperation<TSerialOperationType, TTable> {
return params;
}
12 changes: 9 additions & 3 deletions tests/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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)
Expand Down