diff --git a/packages/graphql/src/translate/connection/create-connection-and-params.ts b/packages/graphql/src/translate/connection/create-connection-and-params.ts index 1a6beb3c49..1993407755 100644 --- a/packages/graphql/src/translate/connection/create-connection-and-params.ts +++ b/packages/graphql/src/translate/connection/create-connection-and-params.ts @@ -85,6 +85,10 @@ function createConnectionAndParams({ const unionSubqueries: string[] = []; unionNodes.forEach((n) => { + if (!node?.fieldsByTypeName[n.name]) { + return; + } + const relatedNodeVariable = `${nodeVariable}_${n.name}`; const nodeOutStr = `(${relatedNodeVariable}:${n.name})`; diff --git a/packages/graphql/tests/integration/connections/unions.int.test.ts b/packages/graphql/tests/integration/connections/unions.int.test.ts index 3b2811a041..93a0ab2599 100644 --- a/packages/graphql/tests/integration/connections/unions.int.test.ts +++ b/packages/graphql/tests/integration/connections/unions.int.test.ts @@ -186,10 +186,6 @@ describe("Connections -> Unions", () => { title: "Oliver Twist", }, }, - { - words: 3413, - node: {}, - }, ], }, }, diff --git a/packages/graphql/tests/tck/tck-test-files/cypher/connections/unions.md b/packages/graphql/tests/tck/tck-test-files/cypher/connections/unions.md index 099f4897bb..faa896958f 100644 --- a/packages/graphql/tests/tck/tck-test-files/cypher/connections/unions.md +++ b/packages/graphql/tests/tck/tck-test-files/cypher/connections/unions.md @@ -166,3 +166,51 @@ RETURN this { .name, publicationsConnection } as this ``` --- + +### Projecting only one member of union node and relationship properties with no arguments + +**GraphQL input** + +```graphql +query { + authors { + name + publicationsConnection { + edges { + words + node { + ... on Book { + title + } + } + } + } + } +} +``` + +**Expected Cypher output** + +```cypher +MATCH (this:Author) +CALL { + WITH this + CALL { + WITH this + OPTIONAL MATCH (this)-[this_wrote:WROTE]->(this_Book:Book) + WITH { words: this_wrote.words, node: { __resolveType: "Book", title: this_Book.title } } AS edge + RETURN edge + } + WITH collect(edge) as edges, count(edge) as totalCount + RETURN { edges: edges, totalCount: totalCount } AS publicationsConnection +} +RETURN this { .name, publicationsConnection } as this +``` + +**Expected Cypher params** + +```cypher-params +{} +``` + +---