neo4j-graphql / neo4j-graphql-js

NOTE: This project is no longer actively maintained. Please consider using the official Neo4j GraphQL Library (linked in README).
Other
608 stars 148 forks source link

Cannot satisfy union type result with Map rather than Node #588

Open amoe opened 3 years ago

amoe commented 3 years ago

Hi, I want to satisfy a union type with a Map. Regular types can be satisfied with maps without any problem, but when a union is involved, an error Expected a Node, got: Map is thrown. I tested satisfying the union with an actual Node, which works as expected. In this example that won't work because the identifier properties in the DB have different names: one uses id for its identifier and one uses name, but when they come out of GraphQL, the identifier should be id in both cases, hence using a Map to rename them.

Sample data:

CREATE (p:Person {id: "foo", gender: "M"}),
       (c:Circuit {name: "bar", size: 32}),
       (p)-[:INVOLVED_IN]->(c);

Sample query:

{
  entities {
    ... on Person {
       id
       gender
    }
    ... on Circuit {
      id
      size
    }
  }
}

Schema definition:

type Person {
    id: ID!
    type: String
    gender: String
}

type Circuit {
    id: ID!
    type: String
    size: Int
}

union Entity = Person | Circuit

type Query {
    entities: [Entity] @cypher(
        statement: """
            MATCH (c:Circuit)
            WITH COLLECT(DISTINCT {id: c.name, type: 'CIRCUIT', size: c.size}) AS circuitNodes
            OPTIONAL MATCH (p:Person)
            WITH COLLECT(DISTINCT {id: p.id, type: 'PERSON', gender: p.gender}) AS personNodes, circuitNodes
            OPTIONAL MATCH (p:Person)-[:INVOLVED_IN]->(c:Circuit)
            UNWIND personNodes + circuitNodes AS x
            RETURN x
        """
    )
}

Error that occurs:

{
  "errors": [
    {
      "message": "Expected a Node, got: Map{gender -> String(\"M\"), type -> String(\"PERSON\"), id -> String(\"foo\")}",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "entities"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "code": "Neo.ClientError.Statement.TypeError",
          "name": "Neo4jError",
          "stacktrace": [
            "Neo4jError: Expected a Node, got: Map{gender -> String(\"M\"), type -> String(\"PERSON\"), id -> String(\"foo\")}",
            "",
            "    at captureStacktrace (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/result.js:277:15)",
            "    at new Result (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/result.js:68:19)",
            "    at newCompletedResult (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:449:10)",
            "    at Object.run (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:287:14)",
            "    at Transaction.run (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:123:32)",
            "    at _callee2$ (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-graphql-js/dist/index.js:222:35)",
            "    at tryCatch (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:63:40)",
            "    at Generator.invoke [as _invoke] (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:293:22)",
            "    at Generator.next (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:118:21)",
            "    at asyncGeneratorStep (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/@babel/runtime-corejs2/helpers/asyncToGenerator.js:5:24)"
          ]
        }
      }
    }
  ],
  "data": {
    "entities": null
  }
}

Using version 2.19.2.

michaeldgraham commented 3 years ago

https://github.com/neo4j-graphql/neo4j-graphql-js/issues/608