Skip to content

Commit

Permalink
fix: issue neo4j#369 connections using cypher
Browse files Browse the repository at this point in the history
  • Loading branch information
danstarns committed Jul 30, 2021
1 parent 4d15cc8 commit db76e66
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions packages/graphql/src/schema/resolvers/cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import { isInt } from "neo4j-driver";
import { execute } from "../../utils";
import { BaseField, Context } from "../../types";
import { BaseField, ConnectionField, Context } from "../../types";
import { graphqlArgsToCompose } from "../to-compose";
import createAuthAndParams from "../../translate/create-auth-and-params";
import createAuthParam from "../../translate/create-auth-param";
import { AUTH_FORBIDDEN_ERROR } from "../../constants";
import createProjectionAndParams from "../../translate/create-projection-and-params";
import createConnectionAndParams from "../../translate/connection/create-connection-and-params";

export default function cypherResolver({
field,
Expand All @@ -37,12 +38,11 @@ export default function cypherResolver({
}) {
async function resolve(_root: any, args: any, _context: unknown) {
const context = _context as Context;
const {
resolveTree: { fieldsByTypeName },
} = context;
const { resolveTree } = context;
const cypherStrs: string[] = [];
let params = { ...args, auth: createAuthParam({ context }), cypherParams: context.cypherParams };
let projectionStr = "";
let connectionProjectionStr = "";
let projectionAuthStr = "";
const isPrimitive = ["ID", "String", "Boolean", "Float", "Int", "DateTime", "BigInt"].includes(
field.typeMeta.name
Expand All @@ -57,15 +57,35 @@ export default function cypherResolver({
const referenceNode = context.neoSchema.nodes.find((x) => x.name === field.typeMeta.name);
if (referenceNode) {
const recurse = createProjectionAndParams({
fieldsByTypeName,
fieldsByTypeName: resolveTree.fieldsByTypeName,
node: referenceNode,
context,
varName: `this`,
});
[projectionStr] = recurse;
params = { ...params, ...recurse[1] };
if (recurse[2]?.authValidateStrs?.length) {
projectionAuthStr = recurse[2].authValidateStrs.join(" AND ");
const [str, p, meta] = recurse;
projectionStr = str;
params = { ...params, ...p };

if (meta?.authValidateStrs?.length) {
projectionAuthStr = meta.authValidateStrs.join(" AND ");
}

if (meta?.connectionFields?.length) {
meta.connectionFields.forEach((connectionResolveTree) => {
const connectionField = referenceNode.connectionFields.find(
(x) => x.fieldName === connectionResolveTree.name
) as ConnectionField;

const nestedConnection = createConnectionAndParams({
resolveTree: connectionResolveTree,
field: connectionField,
context,
nodeVariable: "this",
});
const [str, p] = nestedConnection;
connectionProjectionStr = str;
params = { ...params, ...p };
});
}
}

Expand Down Expand Up @@ -101,6 +121,10 @@ export default function cypherResolver({
);
}

if (connectionProjectionStr) {
cypherStrs.push(connectionProjectionStr);
}

if (!isPrimitive) {
cypherStrs.push(`RETURN this ${projectionStr} AS this`);
} else {
Expand Down

0 comments on commit db76e66

Please sign in to comment.