Skip to content

Commit

Permalink
fix delete reduce for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
angrykoala committed Mar 8, 2022
1 parent 3bee346 commit 2b594ef
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ export function publishEventsToPlugin(
plugin: Neo4jGraphQLSubscriptionsPlugin | undefined
): void {
if (plugin) {
const metaData: EventMeta[] = executeResult.records[0]?.meta || [];
const metadata: EventMeta[] = executeResult.records[0]?.meta || [];

for (const meta of metaData) {
const serializedMeta = serializeEvent(meta);
for (const rawEvent of metadata) {
const subscriptionsEvent = serializeEvent(rawEvent);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
plugin.publish(serializedMeta);
plugin.publish(subscriptionsEvent);
}
}
}
Expand All @@ -46,7 +46,7 @@ function serializeEvent(event: EventMeta): SubscriptionsEvent {
old: serializeProperties(event.properties.old),
new: serializeProperties(event.properties.new),
},
} as SubscriptionsEvent;
} as SubscriptionsEvent; // Casting here because ts is not smart enough to get the difference between create|update|delete
}

function serializeProperties(properties: Record<string, any> | undefined): Record<string, any> | undefined {
Expand Down
13 changes: 6 additions & 7 deletions packages/graphql/src/translate/create-delete-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,13 @@ function createDeleteAndParams({
);

if (context.subscriptionsEnabled) {
const metaObjectStr = createEventMetaObject({
event: "delete",
nodeVariable: "n",
});
const reduceStr = `REDUCE(m=${META_CYPHER_VARIABLE}, n IN ${nodeToDelete} | m + ${metaObjectStr}) AS ${META_CYPHER_VARIABLE}`;
res.strs.push(
`WITH ${[
...filterMetaVariable(withVars),
nodeToDelete,
]}, REDUCE(m=${META_CYPHER_VARIABLE}, n IN ${nodeToDelete} | m + ${createEventMetaObject({
event: "delete",
nodeVariable: "n",
})}) AS ${META_CYPHER_VARIABLE}`
`WITH ${[...filterMetaVariable(withVars), nodeToDelete].join(", ")}, ${reduceStr}`
);
}

Expand Down
20 changes: 11 additions & 9 deletions packages/graphql/src/translate/translate-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import createDeleteAndParams from "./create-delete-and-params";
import translateTopLevelMatch from "./translate-top-level-match";
import { createEventMeta } from "./subscriptions/create-event-meta";

function translateDelete({ context, node }: { context: Context; node: Node }): [string, any] {
export default function translateDelete({ context, node }: { context: Context; node: Node }): [string, any] {
const { resolveTree } = context;
const deleteInput = resolveTree.args.delete;
const varName = "this";
Expand Down Expand Up @@ -89,16 +89,18 @@ function translateDelete({ context, node }: { context: Context; node: Node }): [
deleteStr,
allowStr,
`DETACH DELETE ${varName}`,
...(context.subscriptionsEnabled
? [
`WITH ${META_CYPHER_VARIABLE}`,
`UNWIND ${META_CYPHER_VARIABLE} AS m`,
`RETURN collect(DISTINCT m) AS meta`,
]
: []),
...getDeleteReturn(context),
];

return [cypher.filter(Boolean).join("\n"), cypherParams];
}

export default translateDelete;
function getDeleteReturn(context: Context): Array<string> {
return context.subscriptionsEnabled
? [
`WITH ${META_CYPHER_VARIABLE}`,
`UNWIND ${META_CYPHER_VARIABLE} AS m`,
`RETURN collect(DISTINCT m) AS ${META_CYPHER_VARIABLE}`,
]
: [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("Subscriptions delete", () => {
source: query,
contextValue: { driver, req, driverConfig: { bookmarks: session.lastBookmark() } },
});
console.log(JSON.stringify(gqlResult, null, 4));

expect((gqlResult.errors as any[])[0].message).toBe("Forbidden");
} finally {
await session.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("Subscriptions metadata on delete", () => {
OPTIONAL MATCH (this)<-[this_actors0_relationship:ACTED_IN]-(this_actors0:Actor)
WHERE this_actors0.name = $this_deleteMovies.args.delete.actors[0].where.node.name
WITH this, meta, collect(DISTINCT this_actors0) as this_actors0_to_delete
WITH this,this_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
WITH this, this_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
FOREACH(x IN this_actors0_to_delete | DETACH DELETE x)
DETACH DELETE this
WITH meta
Expand Down Expand Up @@ -178,13 +178,13 @@ describe("Subscriptions metadata on delete", () => {
OPTIONAL MATCH (this_actors0_movies0)<-[this_actors0_movies0_actors0_relationship:ACTED_IN]-(this_actors0_movies0_actors0:Actor)
WHERE this_actors0_movies0_actors0.name = $this_deleteMovies.args.delete.actors[0].delete.movies[0].delete.actors[0].where.node.name
WITH this, meta, this_actors0, this_actors0_movies0, collect(DISTINCT this_actors0_movies0_actors0) as this_actors0_movies0_actors0_to_delete
WITH this,this_actors0,this_actors0_movies0,this_actors0_movies0_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_movies0_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
WITH this, this_actors0, this_actors0_movies0, this_actors0_movies0_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_movies0_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
FOREACH(x IN this_actors0_movies0_actors0_to_delete | DETACH DELETE x)
WITH this, meta, this_actors0, collect(DISTINCT this_actors0_movies0) as this_actors0_movies0_to_delete
WITH this,this_actors0,this_actors0_movies0_to_delete, REDUCE(m=meta, n IN this_actors0_movies0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
WITH this, this_actors0, this_actors0_movies0_to_delete, REDUCE(m=meta, n IN this_actors0_movies0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
FOREACH(x IN this_actors0_movies0_to_delete | DETACH DELETE x)
WITH this, meta, collect(DISTINCT this_actors0) as this_actors0_to_delete
WITH this,this_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
WITH this, this_actors0_to_delete, REDUCE(m=meta, n IN this_actors0_to_delete | m + { event: \\"delete\\", id: id(n), properties: { old: n { .* }, new: null }, timestamp: timestamp() }) AS meta
FOREACH(x IN this_actors0_to_delete | DETACH DELETE x)
DETACH DELETE this
WITH meta
Expand Down

0 comments on commit 2b594ef

Please sign in to comment.