Skip to content

Commit

Permalink
WIP on result-set changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yvann committed Aug 17, 2023
1 parent 4c692ed commit c67c5e4
Show file tree
Hide file tree
Showing 53 changed files with 2,399 additions and 2,146 deletions.
2,029 changes: 1,010 additions & 1,019 deletions .pnp.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"devDependencies": {
"@yarnpkg/sdks": "3.0.0-rc.48",
"jest": "29.5.0",
"jest": "29.6.2",
"prettier": "2.8.8",
"typescript": "5.1.6"
},
Expand Down
18 changes: 9 additions & 9 deletions packages/graphql-platform-connector-mariadb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
"test:watch": "yarn run test --watchAll"
},
"devDependencies": {
"@jest/globals": "29.5.0",
"@jest/globals": "29.6.2",
"@sqltools/formatter": "1.2.5",
"@swc/core": "1.3.67",
"@swc/jest": "0.2.26",
"@swc/core": "1.3.77",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.0",
"@types/node": "18.16.19",
"@types/node": "18.17.5",
"@types/semver": "7.5.0",
"graphql": "16.7.1",
"jest": "29.5.0",
"publint": "0.1.15",
"graphql": "16.8.0",
"jest": "29.6.2",
"publint": "0.2.0",
"typescript": "5.1.6"
},
"dependencies": {
Expand All @@ -47,8 +47,8 @@
"@prismamedia/memoize": "^5.0.1",
"inflection": "^2.0.1",
"mariadb": "^3.2.0",
"semver": "^7.5.3",
"type-fest": "^3.12.0"
"semver": "^7.5.4",
"type-fest": "^4.2.0"
},
"peerDependencies": {
"graphql": "^16.7.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ describe('Update statement', () => {

beforeAll(async () => {
gp = createMyGP(`connector_mariadb_update_statement`);
gp.on('node-change-aggregation', (aggregation) =>
changes?.push(...aggregation),
gp.on(
'node-change-aggregation',
(aggregation) => changes?.push(...aggregation),
);

await gp.connector.setup();
Expand Down
16 changes: 8 additions & 8 deletions packages/graphql-platform-integration-apollo-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
"test:watch": "yarn run test --watchAll"
},
"devDependencies": {
"@apollo/server": "4.7.5",
"@jest/globals": "29.5.0",
"@apollo/server": "4.9.1",
"@jest/globals": "29.6.2",
"@prismamedia/graphql-platform-connector-mariadb": "workspace:packages/graphql-platform-connector-mariadb",
"@swc/core": "1.3.67",
"@swc/jest": "0.2.26",
"@swc/core": "1.3.77",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.0",
"@types/node": "18.16.19",
"graphql": "16.7.1",
"jest": "29.5.0",
"publint": "0.1.15",
"@types/node": "18.17.5",
"graphql": "16.8.0",
"jest": "29.6.2",
"publint": "0.2.0",
"typescript": "5.1.6"
},
"dependencies": {
Expand Down
16 changes: 8 additions & 8 deletions packages/graphql-platform-scalars/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@
"test:watch": "yarn run test --watchAll"
},
"devDependencies": {
"@jest/globals": "29.5.0",
"@swc/core": "1.3.67",
"@swc/jest": "0.2.26",
"@jest/globals": "29.6.2",
"@swc/core": "1.3.77",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.0",
"@types/node": "18.16.19",
"graphql": "16.7.1",
"jest": "29.5.0",
"publint": "0.1.15",
"@types/node": "18.17.5",
"graphql": "16.8.0",
"jest": "29.6.2",
"publint": "0.2.0",
"typescript": "5.1.6"
},
"dependencies": {
"@prismamedia/graphql-platform-utils": "workspace:packages/graphql-platform-utils",
"@types/draft-js": "^0.11.12",
"entities": "^4.5.0",
"type-fest": "^3.12.0"
"type-fest": "^4.2.0"
},
"peerDependencies": {
"graphql": "^16.7.0"
Expand Down
18 changes: 9 additions & 9 deletions packages/graphql-platform-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
"test:watch": "yarn run test --watchAll"
},
"devDependencies": {
"@jest/globals": "29.5.0",
"@swc/core": "1.3.67",
"@swc/jest": "0.2.26",
"@jest/globals": "29.6.2",
"@swc/core": "1.3.77",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.0",
"@types/indefinite": "2.3.1",
"@types/node": "18.16.19",
"graphql": "16.7.1",
"jest": "29.5.0",
"publint": "0.1.15",
"@types/node": "18.17.5",
"graphql": "16.8.0",
"jest": "29.6.2",
"publint": "0.2.0",
"typescript": "5.1.6"
},
"dependencies": {
"@prismamedia/memoize": "^5.0.1",
"indefinite": "^2.4.3",
"remeda": "^1.23.0",
"type-fest": "^3.12.0"
"remeda": "^1.24.1",
"type-fest": "^4.2.0"
},
"peerDependencies": {
"graphql": "^16.7.0"
Expand Down
18 changes: 9 additions & 9 deletions packages/graphql-platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"test:watch": "yarn run test --watchAll"
},
"devDependencies": {
"@jest/globals": "29.5.0",
"@swc/core": "1.3.67",
"@swc/jest": "0.2.26",
"@jest/globals": "29.6.2",
"@swc/core": "1.3.77",
"@swc/jest": "0.2.29",
"@tsconfig/node18": "18.2.0",
"@types/node": "18.16.19",
"graphql": "16.7.1",
"jest": "29.5.0",
"publint": "0.1.15",
"@types/node": "18.17.5",
"graphql": "16.8.0",
"jest": "29.6.2",
"publint": "0.2.0",
"typescript": "5.1.6"
},
"dependencies": {
Expand All @@ -49,8 +49,8 @@
"inflection": "^2.0.1",
"p-queue": "^7.3.4",
"p-retry": "^5.1.2",
"remeda": "^1.23.0",
"type-fest": "^3.12.0"
"remeda": "^1.24.1",
"type-fest": "^4.2.0"
},
"peerDependencies": {
"graphql": "^16.7.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-platform/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,8 +1452,8 @@ export class Node<

if (this.isPubliclyUpdatable()) {
if (
!Array.from(this.componentSet).some((component) =>
component.updateInput?.isPublic(),
!Array.from(this.componentSet).some(
(component) => component.updateInput?.isPublic(),
)
) {
throw new utils.GraphError(
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql-platform/src/node/abstract-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export abstract class AbstractNodeChange<
public readonly createdAt: Date = new Date(),
public committedAt?: Date,
) {
Object.freeze(id);

this.stringifiedId = node.identifier.stringify(id);
}

Expand Down
57 changes: 46 additions & 11 deletions packages/graphql-platform/src/node/change.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,48 @@ describe('Change', () => {
},
{ id: 'fbc99af4-429d-49fd-be05-8fdba83559c5', title: 'My title' },
),
new NodeUpdate(
Tag,
myContext,
{
id: 5,
title: 'My fifth tag',
},
{
id: 5,
title: 'My updated fifth tag',
},
),
new NodeDeletion(Tag, myContext, {
id: 10,
title: 'My tenth tag',
}),
]);

expect(aggregate.requestContexts.length).toBe(1);
expect(aggregate.length).toBe(3);
expect(aggregate.length).toBe(5);

expect(
Object.fromEntries(
Array.from(aggregate.flatChanges, ([node, components]) => [
node.name,
Array.from(components).map((component) => component.name),
]),
Array.from(aggregate.changesByNode.keys(), ({ name }) => name),
).toEqual(['Article', 'Tag', 'ArticleTag']);

expect(aggregate.changesSummary.creations?.size).toBe(3);
expect(
Array.from(aggregate.changesSummary.creations!, ({ name }) => name),
).toEqual(['Article', 'Tag', 'ArticleTag']);

expect(aggregate.changesSummary.deletions?.size).toBe(1);
expect(
Array.from(aggregate.changesSummary.deletions!, ({ name }) => name),
).toEqual(['Tag']);

expect(aggregate.changesSummary.updatesByNode?.size).toBe(1);
expect(
Array.from(
aggregate.changesSummary.updatesByNode!.keys(),
({ name }) => name,
),
).toEqual({
Article: ['id', 'title'],
ArticleTag: ['article', 'tag', 'order'],
Tag: ['id', 'title'],
});
).toEqual(['Tag']);

expect(
Array.from(aggregate, (change) => ({
Expand All @@ -189,6 +214,16 @@ describe('Change', () => {
stringifiedId: '{"id":1}',
kind: 'creation',
},
{
node: 'Tag',
stringifiedId: '{"id":5}',
kind: 'update',
},
{
node: 'Tag',
stringifiedId: '{"id":10}',
kind: 'deletion',
},
{
node: 'ArticleTag',
stringifiedId:
Expand Down
73 changes: 36 additions & 37 deletions packages/graphql-platform/src/node/change/aggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ConnectorInterface } from '../../connector-interface.js';
import type { Component, Node } from '../../node.js';
import {
NodeCreation,
NodeDeletion,
NodeUpdate,
filterNodeChange,
type NodeChange,
Expand Down Expand Up @@ -72,8 +73,6 @@ const aggregatorMatrix: NodeChangeAggregatorMatrix = {
},
};

export type FlatChanges = ReadonlyMap<Node, ReadonlySet<Component>>;

export class NodeChangeAggregation<
TRequestContext extends object = any,
TConnector extends ConnectorInterface = any,
Expand All @@ -87,10 +86,11 @@ export class NodeChangeAggregation<
ReadonlyArray<NodeChange<TRequestContext, TConnector, TContainer>>
>;

/**
* Convenient to match against NodeResultSetMutability.flatDependencies
*/
public readonly flatChanges: FlatChanges;
public readonly changesSummary: {
readonly creations?: ReadonlySet<Node>;
readonly deletions?: ReadonlySet<Node>;
readonly updatesByNode?: ReadonlyMap<Node, ReadonlySet<Component>>;
};

public readonly length: number;

Expand Down Expand Up @@ -140,48 +140,47 @@ export class NodeChangeAggregation<
this.requestContexts = Array.from(requestContextSet);

this.changesByNode = new Map(
Array.from(
changesByIdByNode.entries(),
([node, changesByFlattenedId]) => [
node,
Array.from(changesByFlattenedId.values()),
],
),
Array.from(changesByIdByNode, ([node, changesByFlattenedId]) => [
node,
Array.from(changesByFlattenedId.values()),
]),
);

this.flatChanges = new Map(
Array.from(this.changesByNode.entries(), ([node, changes]) => {
const componentSet = new Set<Component>();
// changes-summary
{
const creations = new Set<Node>();
const deletions = new Set<Node>();
const updatesByNode = new Map<Node, Set<Component>>();

for (const change of changes) {
if (
change.kind === utils.MutationType.CREATION ||
change.kind === utils.MutationType.DELETION
) {
for (const component of node.componentSet) {
componentSet.add(component);
}
changesByIdByNode.forEach((changesByFlattenedId, node) => {
const updates = new Set<Component>();

break;
changesByFlattenedId.forEach((change) => {
if (change instanceof NodeCreation) {
creations.add(node);
} else if (change instanceof NodeDeletion) {
deletions.add(node);
} else {
for (const component of change.updatesByComponent.keys()) {
componentSet.add(component);
}

if (componentSet.size === node.componentSet.size) {
break;
}
change.updatesByComponent.forEach((_, component) =>
updates.add(component),
);
}
}
});

return [node, componentSet];
}),
);
updates.size && updatesByNode.set(node, updates);
});

this.changesSummary = {
...(creations.size && { creations }),
...(deletions.size && { deletions }),
...(updatesByNode.size && { updatesByNode }),
};
}

this.length = Array.from(
this.changesByNode.values(),
(changes) => changes.length,
).reduce<number>((sum, changesLength) => sum + changesLength, 0);
).reduce((sum, length) => sum + length, 0);
}

*[Symbol.iterator](): IterableIterator<
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-platform/src/node/change/creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class NodeCreation<

super(
node,
Object.freeze(node.identifier.parseValue(newValue)),
node.identifier.parseValue(newValue),
requestContext,
createdAt,
committedAt,
Expand Down
Loading

0 comments on commit c67c5e4

Please sign in to comment.