Skip to content

Commit

Permalink
Refactored on dependency / change
Browse files Browse the repository at this point in the history
  • Loading branch information
yvann committed Nov 22, 2024
1 parent 028e08f commit beb2dc5
Show file tree
Hide file tree
Showing 60 changed files with 2,225 additions and 2,360 deletions.
584 changes: 292 additions & 292 deletions .yarn/releases/yarn-4.5.1.cjs → .yarn/releases/yarn-4.5.2.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ supportedArchitectures:
os:
- linux

yarnPath: .yarn/releases/yarn-4.5.1.cjs
yarnPath: .yarn/releases/yarn-4.5.2.cjs
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"prettier": "3.3.3",
"typescript": "5.6.3"
},
"packageManager": "[email protected].1"
"packageManager": "[email protected].2"
}
6 changes: 3 additions & 3 deletions packages/graphql-platform-connector-mariadb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"devDependencies": {
"@jest/globals": "30.0.0-alpha.6",
"@sqltools/formatter": "1.2.5",
"@swc/core": "1.8.0",
"@swc/core": "1.9.2",
"@swc/jest": "0.2.37",
"@tsconfig/node20": "20.1.4",
"@types/node": "20.17.6",
Expand All @@ -48,9 +48,9 @@
"dependency-graph": "^1.0.0",
"inflection": "^3.0.0",
"mariadb": "^3.4.0",
"remeda": "^2.16.0",
"remeda": "^2.17.3",
"semver": "^7.6.3",
"type-fest": "^4.26.1"
"type-fest": "^4.27.0"
},
"peerDependencies": {
"graphql": "^16.9.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ export class Table {
)
: null;
} else if (expression instanceof core.VirtualSelection) {
result[expression.key] = expression.dependency
result[expression.key] = expression.sourceSelection
? this.parseJsonDocument(
jsonValue,
expression.dependency,
expression.sourceSelection,
expressionPath,
)
: undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ function parseSelectionExpression(
} else if (core.isMultipleReverseEdgeSelection(selection)) {
return parseMultipleReverseEdgeSelection(tableReference, selection);
} else if (selection instanceof core.VirtualSelection) {
return selection.dependency
? selectNode(tableReference, selection.dependency, referenceColumnTree)
return selection.sourceSelection
? selectNode(
tableReference,
selection.sourceSelection,
referenceColumnTree,
)
: 'NULL';
} else {
throw new utils.UnreachableValueError(selection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ describe('Subscription', () => {
await gp.connector.teardown();
});

it('has consistent dependency', () => {
expect(subscription.dependency.summary.toJSON()).toEqual({
creations: ['Article', 'ArticleTag'],
deletions: ['Article', 'ArticleTag'],
updatesByNode: {
Article: ['status', 'title'],
Tag: ['deprecated'],
},
changes: ['Article', 'ArticleTag', 'Tag'],
});
});

it('is iterable', async () => {
const changes: ChangesSubscriptionChange[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@apollo/server": "4.11.2",
"@jest/globals": "30.0.0-alpha.6",
"@prismamedia/graphql-platform-connector-mariadb": "workspace:packages/graphql-platform-connector-mariadb",
"@swc/core": "1.8.0",
"@swc/core": "1.9.2",
"@swc/jest": "0.2.37",
"@tsconfig/node20": "20.1.4",
"@types/body-parser": "^1.19.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-platform-scalars/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"devDependencies": {
"@jest/globals": "30.0.0-alpha.6",
"@swc/core": "1.8.0",
"@swc/core": "1.9.2",
"@swc/jest": "0.2.37",
"@tsconfig/node20": "20.1.4",
"@types/node": "20.17.6",
Expand All @@ -39,8 +39,8 @@
"dependencies": {
"@prismamedia/graphql-platform-utils": "workspace:packages/graphql-platform-utils",
"@types/draft-js": "^0.11.18",
"remeda": "^2.16.0",
"type-fest": "^4.26.1"
"remeda": "^2.17.3",
"type-fest": "^4.27.0"
},
"peerDependencies": {
"graphql": "^16.9.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-platform-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"devDependencies": {
"@jest/globals": "30.0.0-alpha.6",
"@swc/core": "1.8.0",
"@swc/core": "1.9.2",
"@swc/jest": "0.2.37",
"@tsconfig/node20": "20.1.4",
"@types/indefinite": "2.3.4",
Expand All @@ -40,8 +40,8 @@
"dependencies": {
"@prismamedia/memoize": "^5.0.3",
"indefinite": "^2.5.1",
"remeda": "^2.16.0",
"type-fest": "^4.26.1"
"remeda": "^2.17.3",
"type-fest": "^4.27.0"
},
"peerDependencies": {
"graphql": "^16.9.0"
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-platform-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './nil.js';
export * from './operation-types.js';
export * from './path.js';
export * from './plain-object.js';
export * from './set.js';
export * from './stringifiable.js';
export * from './thunkable.js';
export * from './typing.js';
13 changes: 13 additions & 0 deletions packages/graphql-platform-utils/src/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface SetLike<T> {
size: number;
has: (value: T) => boolean;
keys: () => Iterator<T>;
}

/**
* @deprecated Use `Set.isDisjointWith` when available
*/
export const areSetsDisjoints = <T>(
a: ReadonlySet<T>,
b: SetLike<T>,
): boolean => !Array.from(a).some((value) => b.has(value));
8 changes: 4 additions & 4 deletions packages/graphql-platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"devDependencies": {
"@jest/globals": "30.0.0-alpha.6",
"@swc/core": "1.8.0",
"@swc/core": "1.9.2",
"@swc/jest": "0.2.37",
"@tsconfig/node20": "20.1.4",
"@types/node": "20.17.6",
Expand All @@ -49,9 +49,9 @@
"dependency-graph": "^1.0.0",
"inflection": "^3.0.0",
"p-queue": "^8.0.1",
"p-retry": "^6.2.0",
"remeda": "^2.16.0",
"type-fest": "^4.26.1"
"p-retry": "^6.2.1",
"remeda": "^2.17.3",
"type-fest": "^4.27.0"
},
"peerDependencies": {
"graphql": "^16.9.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-platform/src/broker/in-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class InMemoryBroker implements BrokerInterface {

public async publish(changes: NodeChangeAggregation): Promise<void> {
await Promise.all(
Array.from(this.#subscriptions.values()).map((queue) =>
Array.from(this.#subscriptions.values(), (queue) =>
queue.enqueue(changes),
),
);
Expand Down
44 changes: 19 additions & 25 deletions packages/graphql-platform/src/broker/in-memory/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,29 @@ export class InMemorySubscription
changes: NodeChangeAggregation,
waitUntilProcessed: boolean = this.subscription.isConsumingNodeChanges(),
): Promise<void> {
if (!this.subscription.isAffectedBy(changes)) {
const filteredChanges = changes.filter(this.subscription.dependency);
if (!filteredChanges.size) {
return;
}

const [processing, enqueued] = waitUntilProcessed
? [
new Promise<void>((resolve) => {
const off = this.on(
'dequeued',
(dequeued) => {
if (enqueued === dequeued) {
off();
resolve();
}
},
this.subscription.signal,
() => resolve(),
);
}),
changes,
]
: [
undefined,
// As the original changes will get disposed of, we need to keep a copy of them here
changes.clone(),
];
const processing = waitUntilProcessed
? new Promise<void>((resolve) => {
const off = this.on(
'dequeued',
(dequeued) => {
if (filteredChanges === dequeued) {
off();
resolve();
}
},
this.subscription.signal,
() => resolve(),
);
})
: undefined;

this.#queue.push(enqueued);
await this.emit('enqueued', enqueued);
this.#queue.push(filteredChanges);
await this.emit('enqueued', filteredChanges);

return processing;
}
Expand Down
26 changes: 7 additions & 19 deletions packages/graphql-platform/src/node/change.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,14 @@ describe('Change', () => {
}),
]);

expect(aggregate.size).toBe(5);

expect(
Array.from(aggregate.changesByNode.keys(), ({ name }) => name),
).toEqual(['Article', 'Tag', 'ArticleTag']);

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

expect(aggregate.summary.deletions?.size).toBe(1);
expect(
Array.from(aggregate.summary.deletions!, ({ name }) => name),
).toEqual(['Tag']);
expect(aggregate.summary.toJSON()).toEqual({
creations: ['Article', 'Tag', 'ArticleTag'],
deletions: ['Tag'],
updatesByNode: { Tag: ['title'] },
changes: ['Article', 'Tag', 'ArticleTag'],
});

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

expect(Array.from(aggregate, String)).toMatchInlineSnapshot(`
[
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-platform/src/node/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NodeUpdate } from './change/update.js';
export * from './change/aggregation.js';
export * from './change/creation.js';
export * from './change/deletion.js';
export * from './change/dependency.js';
export * from './change/update.js';

export type NodeChange<TRequestContext extends object = any> =
Expand Down
43 changes: 36 additions & 7 deletions packages/graphql-platform/src/node/change/aggregation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as utils from '@prismamedia/graphql-platform-utils';
import { Memoize } from '@prismamedia/memoize';
import type { Node } from '../../node.js';
import {
isActualNodeChange,
NodeCreation,
NodeUpdate,
type DependencyTree,
type NodeChange,
} from '../change.js';
import { NodeFilter, OrOperation } from '../statement.js';
import { NodeChangeAggregationSummary } from './aggregation/summary.js';

export * from './aggregation/summary.js';
Expand Down Expand Up @@ -196,12 +199,38 @@ export class NodeChangeAggregation<TRequestContext extends object = any>
);
}

/**
* Creates a shallow copy
*/
public clone(
config: NodeChangeAggregationConfig | undefined = this.config,
): NodeChangeAggregation<TRequestContext> {
return new NodeChangeAggregation(this, config);
public filter(dependencyTree: DependencyTree): NodeChangeAggregationFiltered {
return new NodeChangeAggregationFiltered(this, dependencyTree);
}
}

export class NodeChangeAggregationFiltered<
TRequestContext extends object = any,
> extends NodeChangeAggregation<TRequestContext> {
public constructor(
unfiltered: NodeChangeAggregation<TRequestContext>,
public readonly dependencyTree: DependencyTree,
) {
const visitedChanges = dependencyTree.visitChanges(unfiltered);

super(
Array.from(dependencyTree.summary.changes).flatMap((node) =>
Array.from(unfiltered.changesByNode.get(node)?.values() ?? []).filter(
(change) => dependencyTree.dependsOn(change, visitedChanges),
),
),
);

visitedChanges.clear();
}

@Memoize()
public get target(): NodeFilter {
return new NodeFilter(
this.dependencyTree.node,
OrOperation.create(
Array.from(this, (change) => this.dependencyTree.target(change).filter),
),
);
}
}
Loading

0 comments on commit beb2dc5

Please sign in to comment.