From 6ebfe790c7fd9d0c108894858abe17ec6a22f125 Mon Sep 17 00:00:00 2001 From: Darrell Warde Date: Tue, 20 Jul 2021 10:16:26 +0100 Subject: [PATCH] Fix primitive arrays being returned from custom Cypher fields --- .../translate/create-projection-and-params.ts | 8 +-- .../integration/custom-resolvers.int.test.ts | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/packages/graphql/src/translate/create-projection-and-params.ts b/packages/graphql/src/translate/create-projection-and-params.ts index 6fd9e04965..aab26fa07a 100644 --- a/packages/graphql/src/translate/create-projection-and-params.ts +++ b/packages/graphql/src/translate/create-projection-and-params.ts @@ -239,14 +239,14 @@ function createProjectionAndParams({ projectionStr ? `| ${param} ${projectionStr}` : "" }`; - if (cypherField.typeMeta.array) { - res.projection.push(`${key}: [${apocStr}]`); + if (isPrimitive || isEnum) { + res.projection.push(`${key}: ${apocStr}`); return res; } - if (isPrimitive || isEnum) { - res.projection.push(`${key}: ${apocStr}`); + if (cypherField.typeMeta.array) { + res.projection.push(`${key}: [${apocStr}]`); return res; } diff --git a/packages/graphql/tests/integration/custom-resolvers.int.test.ts b/packages/graphql/tests/integration/custom-resolvers.int.test.ts index 0f58bec346..e41c777270 100644 --- a/packages/graphql/tests/integration/custom-resolvers.int.test.ts +++ b/packages/graphql/tests/integration/custom-resolvers.int.test.ts @@ -524,5 +524,62 @@ describe("Custom Resolvers", () => { await session.close(); } }); + + test("should return an array of primitive values from a cypher directive (field level)", async () => { + const id = generate({ + charset: "alphabetic", + }); + const string1 = generate({ + charset: "alphabetic", + }); + const string2 = generate({ + charset: "alphabetic", + }); + const string3 = generate({ + charset: "alphabetic", + }); + + const typeDefs = ` + type Type { + id: ID + strings: [String] @cypher(statement: """ + RETURN ['${string1}', '${string2}', '${string3}'] + """) + } + `; + + const query = ` + query { + types(where: { id: "${id}" }) { + id + strings + } + } + `; + + const session = driver.session(); + + const neoSchema = new Neo4jGraphQL({ + typeDefs, + }); + + try { + await session.run(` + CREATE (:Type {id: "${id}"}) + `); + + const gqlResult = await graphql({ + schema: neoSchema.schema, + source: query, + contextValue: { driver }, + }); + + expect(gqlResult.errors).toBeFalsy(); + + expect((gqlResult.data as any).types[0]).toEqual({ id, strings: [string1, string2, string3] }); + } finally { + await session.close(); + } + }); }); });