Search code examples
node.jsgremlintinkerpop

Circular JSON issue in Node.js on Gremlin query


JanusGraph is running on localhost:8182 in a Docker container

I'm running gremlin console in a Docker container that's linked to the Janusgraph Docker container

I can run successful traversals against the imported air-routes.graphml data

I am building a Node.js/Express app that looks like this:

const express = require('express');
const app = express();
const router = express.Router();

const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const __ = gremlin.process.statics;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const column = gremlin.process.column
const direction = gremlin.process.direction
const p = gremlin.process.P
const textp = gremlin.process.TextP
const pick = gremlin.process.pick
const pop = gremlin.process.pop
const order = gremlin.process.order
const scope = gremlin.process.scope
const t = gremlin.process.t

const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));

router.get('/home', (req,res) => {
  res.send('<h1>Hello World,</h1><p>This is home router</p>');
});

router.get('/query', (req,res) => {
    // g.V().has('airport','code','DFW').values().toList()
    // .then(DFWdetail => console.log(DFWdetail));
    res.send(gremlinQuery());
    //res.send("<h1>Airport query</h1><p>g.V().has('airport','code','DFW').values()</p>");
  });

router.get('/profile', (req,res) => {
  res.send('<h1>Hello World,</h1><p>This is profile router</p>');
});

router.get('/login', (req,res) => {
  res.send('<h1>Hello World,</h1><p>This is login router</p>');
});

router.get('/logout', (req,res) => {
  res.send('<h1>Hello World,</h1><p>This is logout router</p>');
});

app.use('/', router);

app.listen(process.env.port || 3000);

console.log('Web Server is listening at port '+ (process.env.port || 3000));

function  gremlinQuery() {
    const info = g.V().has('airport', 'code', 'DFW').values();
    console.log(info);
    return(info);
}

When I run this Node code, I get a command-line response of:


Web Server is listening at port 3000
GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [RemoteStrategy] ] },
  bytecode: Bytecode {
    sourceInstructions: [],
    stepInstructions: [ [Array], [Array], [Array] ]
  },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0
}
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'GraphSON3Reader'
    |     property '_deserializers' -> object with constructor 'Object'
    |     property 'g:Traverser' -> object with constructor 'TraverserSerializer'
    --- property 'reader' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/home/greg/Development/airlines/node_modules/express/lib/response.js:1123:12)
    at ServerResponse.json (/home/greg/Development/airlines/node_modules/express/lib/response.js:260:14)
    at ServerResponse.send (/home/greg/Development/airlines/node_modules/express/lib/response.js:158:21)
    at /home/greg/Development/airlines/myapp.js:28:9
    at Layer.handle [as handle_request] (/home/greg/Development/airlines/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/greg/Development/airlines/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/greg/Development/airlines/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/greg/Development/airlines/node_modules/express/lib/router/layer.js:95:5)
    at /home/greg/Development/airlines/node_modules/express/lib/router/index.js:281:22

I've Googled CircularJSON and believe it may be something to do with await but when I try it

function  gremlinQuery() {
    const info = await g.V().has('airport', 'code', 'DFW').values();
    console.log(info);
    return(info)

VSCode claims it's superfluous and removes it

I'm confused. What can I do to build a basic Node.js/Express traversal, that returns results from the air-routes graph data?


Solution

  • I think you need to "iterate your traversal" thus:

    function  gremlinQuery() {
        const info = await g.V().has('airport', 'code', 'DFW').values().toList();
        console.log(info);
        return(info)
    

    Basically, a line like g.V().has('airport', 'code', 'DFW').values() returns a Traversal object which by itself does nothing because it is a form of Iterator (or perhaps more explicitly has the behavior of a Java Iterator). You have to supply some kind of terminating step to get results (in this case I used toList()).