Search code examples
javascriptgremlintinkerpoptinkerpop3gremlin-server

What is the best way to serialise a full graph as GraphSON using gremlin-javascript?


I'm looking for a single query that can create a GraphSON serialisation of a full TinkerGraph graph.

// setup

const gremlin = require('gremlin')

const connection = new gremlin.driver.DriverRemoteConnection('ws://localhost:8182/gremlin')

const g = (new gremlin.structure.Graph()).traversal().withRemote(connection)

I'm able to serialise vertices, edges and properties separately as GraphSON. The following statements create a GraphSON serialisation of all vertices only. Edges can be queried with E(), and properties with V().properties().

// placed within an async function

const writer = new gremlin.structure.io.GraphSONWriter()

const serialisedVertices = writer.write(await g.V().toList())

Is there a gremlin-javascript method that will serialise all vertices, edges and properties together (rather than separately as above) into a GraphSON string in a single step?

Additionally if a full graph can be serialised as a single GraphSON string, is there also a matching invokation that will re-hydrate a graph instance from the GraphSON string?


Solution

  • Starting gremlin 3.4.x you can use the io step:

    g.io("graph.json").read().iterate()
    g.io("graph.json").write().iterate()
    

    These commands read/write the complete graph data in graphson format, which is a json file. There are more supported formats as written in the tinkerpop documentation

    If you are running gremlin 3.3.x, you can use the following command:

    graph.io(graphson()).writeGraph("graph.json");
    

    Note that the file is stored on the gremlin server current working directory.