neo4j / neo4j-javascript-driver

Neo4j Bolt driver for JavaScript
https://neo4j.com/docs/javascript-manual/current/
Apache License 2.0
849 stars 148 forks source link

[Bug] Unable to query relationships with JavaScript driver #1021

Closed ajmeese7 closed 1 year ago

ajmeese7 commented 1 year ago

I am not able to retrieve relationships with the Neo4j JavaScript driver.

Neo4j Version: 5.1.0 Enterprise Neo4j Mode: Single instance Driver version: JS driver 5.2.0 Operating System: Zorin 16 Pro

Steps to reproduce

Run a query for MATCH (n) RETURN n with a JavaScript driver session.

Expected behavior

The same data returned by the Neo4j Browser:

Neo4j data with relationships

Actual behavior

No relationships are returned:

[
  {
    "n": {
      "identity": {
        "low": 0,
        "high": 0
      },
      "labels": [
        "Selector"
      ],
      "properties": {
        "notes": "Google DNS resolver",
        "realm": "ipv4",
        "selector": "8.8.8.8",
        "uuid": "15cee84d-b4fc-4d47-9002-f1539d01bf92"
      },
      "elementId": "4:ba878390-d801-48ca-ad97-f4a8046c3a96:0"
    }
  },
  {
    "n": {
      "identity": {
        "low": 1,
        "high": 0
      },
      "labels": [
        "Selector"
      ],
      "properties": {
        "notes": "This is the devil's IP, subleased by Google.",
        "realm": "ipv4",
        "selector": "6.6.6.6",
        "uuid": "a43b2c17-27bf-49b8-a1a6-e74741804f2d"
      },
      "elementId": "4:ba878390-d801-48ca-ad97-f4a8046c3a96:1"
    }
  }
]

To get the above data, I used the following code (simplified):

const query = "MATCH (n) RETURN n";
const session = driver.session();
let queryResult;

await session
    .run(query, parameters)
    .then((result) => {
        // The `log` doesn't show anything related to relationships, just `records` and `summary`
        console.log("Result:", result);
        queryResult = result.records.map((record) => record.toObject());
    })
    .catch((error) => {
        throw error;
    })
    .finally(() => {
        session.close();
    });

return queryResult;

If you're interested, this is the raw output returned by the console.log statement above:

{
  records: [
    Record {
      keys: [Array],
      length: 1,
      _fields: [Array],
      _fieldLookup: [Object]
    },
    Record {
      keys: [Array],
      length: 1,
      _fields: [Array],
      _fieldLookup: [Object]
    }
  ],
  summary: ResultSummary {
    query: { text: 'MATCH (n) RETURN n', parameters: {} },
    queryType: 'r',
    counters: QueryStatistics { _stats: [Object], _systemUpdates: 0 },
    updateStatistics: QueryStatistics { _stats: [Object], _systemUpdates: 0 },
    plan: false,
    profile: false,
    notifications: [],
    server: ServerInfo {
      address: 'localhost:7687',
      agent: 'Neo4j/5.1.0',
      protocolVersion: 5
    },
    resultConsumedAfter: Integer { low: 1, high: 0 },
    resultAvailableAfter: Integer { low: 0, high: 0 },
    database: { name: 'neo4j' }
  }
}
bigmontz commented 1 year ago

This is not a bug. Browser has a configuration called Connect result nodes. With this options enabled, Browser does additional queries to enrich the visualisation with connected data.

Screenshot 2022-11-25 at 12 54 16
ajmeese7 commented 1 year ago

@bigmontz noted, could you explain how to query relationships with the JavaScript driver then? I want to be able to fetch everything in a query, nodes and relationships alike.


Update: I know you can use MATCH (n) MATCH (n)-[r]-() RETURN n,r to get all the nodes and their relationships from a query, that's not an issue. What I'm hoping is to have the data returned in a format that is compatible with the HTTP endpoint. I have a library that currently needs the data in the returned HTTP format.

If that's something that I need to make into a separate issue instead of discussing here, I wouldn't mind doing so, just let me know. Sorry for the confusion! :)

bigmontz commented 1 year ago

The only way of getting the relationship from the nodes using cypher. The Javascript Driver is responsible for the network communication with the Neo4j Database. For instance, Browser and Bloom use this driver for communicating with the database.