How to serialize the `.graph()` data?

I'm using the result.graph() to try and get graph info from a cypher call.
however, even using a debugger it's hard to tell how to extract anything useful from this API.

How do I iterate through or otherwise use a neo4j.graph.Graph object?

  • it's not an array
  • it doesn't have named keys like a dict
  • it's not an iterable
  • it doesn't respond to g.relationships()
  • it doesn't have .data()

so basically wut can I do with this graph instance?

code and query are like below, debugger output to after

    def path_from(self, start_page, flow='BILL'):

        q6 = """
            MATCH
                path = (n1:page{cname:$start_page})
                -[*..5]->
                (n2:page {flow:'BILL'})
                WITH n1
                    AS startPage,
                    [p IN relationships(path) | properties(p)] AS edges,
                    n2 AS endPage,
                    path as path
            RETURN startPage, edges, endPage, path
            ORDER BY edges[0].weight DESC
            LIMIT 1
        """

        result = neolib.get_cursor(q6, start_page=start_page)
        path = result.get('path')
        g = result.graph()
        logging.info('graph %s', g)

In a bit more detail my cypher query is trying to return a few things - nodes, edges and a 'path'
but not sure if i need to apply the .graph somehow on only part of this result?
Or do I need to do an even more complex query with subgraphs for graph to return anything? Maybe that's the problem above.

see

yes seems like returning anything else with a path causes the issue.

If I change my statement to

cypher_query = "MATCH p=(n)-[r]->(m) RETURN n,r,m,p LIMIT 5"

it still works for me.

Oh I just saw, you broke the relationships :), because you only return their properties but all the other entity-data (start,end,type) is missing. if you just return relationships(path) it should work.

because you only return their properties

hmm ok how did I break it? With this filter part maybe?

I recall seeing a warning on that and elsewhere have used a WHERE clause on that.
So perhaps filtering on edge properties in the query breaks things?

Or the important part is to wrap the path in relationships(path) ?

Which actually your other query example did not do and still works:

"MATCH p=(n)-[r]->(m) RETURN p LIMIT 5"

The queries were tested out / built up in the neo-browser, so it's a bit puzzling I can see a graph there but not get the data from the same query in python driver.

The bit where you do this:

[p IN relationships(path) | properties(p)] AS edges,

There the relationship objects are replaced by simple dicts of their properties.
So there is no way to reconstruct them as relationships

just return relationships(path) as edges