Skip to content

Commit

Permalink
Merge branch 'dev' into cypher-builder-connections
Browse files Browse the repository at this point in the history
  • Loading branch information
angrykoala authored Jul 20, 2022
2 parents 4832b03 + d98cb1d commit 5e3d8bb
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 179 deletions.
2 changes: 1 addition & 1 deletion packages/graphql/src/schema/resolvers/mutation/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createResolver({ node }: { node: Node }) {
async function resolve(_root: any, args: any, _context: unknown, info: GraphQLResolveInfo) {
const context = _context as Context;
context.resolveTree = getNeo4jResolveTree(info, { args });
const [cypher, params] = await translateCreate({ context, node });
const { cypher, params } = await translateCreate({ context, node });

const executeResult = await execute({
cypher,
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/schema/resolvers/mutation/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function deleteResolver({ node }: { node: Node }) {
async function resolve(_root: any, args: any, _context: unknown, info: GraphQLResolveInfo) {
const context = _context as Context;
context.resolveTree = getNeo4jResolveTree(info, { args });
const [cypher, params] = translateDelete({ context, node });
const { cypher, params } = translateDelete({ context, node });
const executeResult = await execute({
cypher,
params,
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/schema/resolvers/query/global-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import type { GraphQLResolveInfo } from "graphql";
import type { FieldsByTypeName} from "graphql-parse-resolve-info";
import type { FieldsByTypeName } from "graphql-parse-resolve-info";
import { parseResolveInfo } from "graphql-parse-resolve-info";
import { execute } from "../../../utils";
import { translateRead } from "../../../translate";
Expand Down Expand Up @@ -60,7 +60,7 @@ export function globalNodeResolver({ nodes }: { nodes: Node[] }) {

context.resolveTree = getNeo4jResolveTree(info, { resolveTree });

const [cypher, params] = translateRead({ context, node });
const { cypher, params } = translateRead({ context, node });
const executeResult = await execute({
cypher,
params,
Expand Down
3 changes: 1 addition & 2 deletions packages/graphql/src/schema/resolvers/query/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ export function findResolver({ node }: { node: Node }) {
async function resolve(_root: any, args: any, _context: unknown, info: GraphQLResolveInfo) {
const context = _context as Context;
context.resolveTree = getNeo4jResolveTree(info, { args });
const [cypher, params] = translateRead({ context, node });

const { cypher, params } = translateRead({ context, node });
const executeResult = await execute({
cypher,
params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function rootConnectionResolver({ node, composer }: { node: Node; compose

context.resolveTree = { ...resolveTreeForContext, args: resolveTree.args };

const [cypher, params] = translateRead({ context, node, isRootConnectionField: true });
const { cypher, params } = translateRead({ context, node, isRootConnectionField: true });

const executeResult = await execute({
cypher,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface Res {
meta: ProjectionMeta;
}

interface ProjectionMeta {
export interface ProjectionMeta {
authValidateStrs?: string[];
connectionFields?: ResolveTree[];
interfaceFields?: ResolveTree[];
Expand Down Expand Up @@ -142,7 +142,7 @@ function createProjectionAndParams({
resolveType?: boolean;
inRelationshipProjection?: boolean;
isRootConnectionField?: boolean;
}): [string, any, ProjectionMeta?] {
}): [string, any, ProjectionMeta | undefined] {
function reducer(res: Res, field: ResolveTree): Res {
const alias = field.alias;
let param = "";
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql/src/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

export { default as translateCreate } from "./translate-create";
export { default as translateRead } from "./translate-read";
export { translateRead } from "./translate-read";
export { default as translateUpdate } from "./translate-update";
export { default as translateDelete } from "./translate-delete";
export { translateDelete } from "./translate-delete";
export { default as translateAggregate } from "./translate-aggregate";
6 changes: 3 additions & 3 deletions packages/graphql/src/translate/translate-aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { AUTH_FORBIDDEN_ERROR } from "../constants";
import type { BaseField, Context, PrimitiveField, TemporalField } from "../types";
import createAuthAndParams from "./create-auth-and-params";
import { createDatetimeElement } from "./projection/elements/create-datetime-element";
import translateTopLevelMatch from "./translate-top-level-match";
import { translateTopLevelMatch } from "./translate-top-level-match";

function translateAggregate({ node, context }: { node: Node; context: Context }): [string, any] {
const { fieldsByTypeName } = context.resolveTree;
Expand All @@ -31,8 +31,8 @@ function translateAggregate({ node, context }: { node: Node; context: Context })
const cypherStrs: string[] = [];

const topLevelMatch = translateTopLevelMatch({ node, context, varName, operation: "READ" });
cypherStrs.push(topLevelMatch[0]);
cypherParams = { ...cypherParams, ...topLevelMatch[1] };
cypherStrs.push(topLevelMatch.cypher);
cypherParams = { ...cypherParams, ...topLevelMatch.params };

const allowAuth = createAuthAndParams({
operations: "READ",
Expand Down
27 changes: 16 additions & 11 deletions packages/graphql/src/translate/translate-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import createConnectionAndParams from "./connection/create-connection-and-params
import createInterfaceProjectionAndParams from "./create-interface-projection-and-params";
import { filterTruthy } from "../utils/utils";
import { CallbackBucket } from "../classes/CallbackBucket";
import * as CypherBuilder from "./cypher-builder/CypherBuilder";

export default async function translateCreate({
context,
node,
}: {
context: Context;
node: Node;
}): Promise<[string, any]> {
}): Promise<CypherBuilder.CypherResult> {
const { resolveTree } = context;
const connectionStrs: string[] = [];
const interfaceStrs: string[] = [];
Expand Down Expand Up @@ -235,16 +236,20 @@ export default async function translateCreate({

({ cypher, params: resolvedCallbacks } = await callbackBucket.resolveCallbacksAndFilterCypher({ cypher }));

return [
cypher,
{
...params,
...replacedProjectionParams,
...replacedConnectionParams,
...replacedInterfaceParams,
resolvedCallbacks,
},
];
const createQuery = new CypherBuilder.RawCypher(() => {
return [
cypher,
{
...params,
...replacedProjectionParams,
...replacedConnectionParams,
...replacedInterfaceParams,
resolvedCallbacks,
},
];
});

return createQuery.build();
}

function generateCreateReturnStatement(projectionStr: string | undefined, subscriptionsEnabled: boolean): string {
Expand Down
34 changes: 19 additions & 15 deletions packages/graphql/src/translate/translate-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import type { Context } from "../types";
import { AUTH_FORBIDDEN_ERROR, META_CYPHER_VARIABLE } from "../constants";
import createAuthAndParams from "./create-auth-and-params";
import createDeleteAndParams from "./create-delete-and-params";
import translateTopLevelMatch from "./translate-top-level-match";
import { translateTopLevelMatch } from "./translate-top-level-match";
import { createEventMeta } from "./subscriptions/create-event-meta";
import * as CypherBuilder from "./cypher-builder/CypherBuilder";

export default function translateDelete({ context, node }: { context: Context; node: Node }): [string, any] {
export function translateDelete({ context, node }: { context: Context; node: Node }): CypherBuilder.CypherResult {
const { resolveTree } = context;
const deleteInput = resolveTree.args.delete;
const varName = "this";
Expand All @@ -41,8 +42,8 @@ export default function translateDelete({ context, node }: { context: Context; n
}

const topLevelMatch = translateTopLevelMatch({ node, context, varName, operation: "DELETE" });
matchAndWhereStr = topLevelMatch[0];
cypherParams = { ...cypherParams, ...topLevelMatch[1] };
matchAndWhereStr = topLevelMatch.cypher;
cypherParams = { ...cypherParams, ...topLevelMatch.params };

const allowAuth = createAuthAndParams({
operations: "DELETE",
Expand Down Expand Up @@ -80,19 +81,22 @@ export default function translateDelete({ context, node }: { context: Context; n
};
}

const eventMeta = createEventMeta({ event: "delete", nodeVariable: varName, typename: node.name });
const deleteQuery = new CypherBuilder.RawCypher((_env: CypherBuilder.Environment) => {
const eventMeta = createEventMeta({ event: "delete", nodeVariable: varName, typename: node.name });
const cypher = [
...(context.subscriptionsEnabled ? [`WITH [] AS ${META_CYPHER_VARIABLE}`] : []),
matchAndWhereStr,
...(context.subscriptionsEnabled ? [`WITH ${varName}, ${eventMeta}`] : []),
deleteStr,
allowStr,
`DETACH DELETE ${varName}`,
...getDeleteReturn(context),
];

const cypher = [
...(context.subscriptionsEnabled ? [`WITH [] AS ${META_CYPHER_VARIABLE}`] : []),
matchAndWhereStr,
...(context.subscriptionsEnabled ? [`WITH ${varName}, ${eventMeta}`] : []),
deleteStr,
allowStr,
`DETACH DELETE ${varName}`,
...getDeleteReturn(context),
];
return [cypher.filter(Boolean).join("\n"), cypherParams];
});

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

function getDeleteReturn(context: Context): Array<string> {
Expand Down
Loading

0 comments on commit 5e3d8bb

Please sign in to comment.