Let's say I have the following: - a node.js server that is able to query a database and receive a (possibly big) JSON - a gremlin server (configured with gremlin-server-rest-modern.yaml but maybe there must be something more in there)
My question is: is there a way to dynamically and programmatically add a new graph to be served up by the server? I want my node server to query the database and send the JSON response to the gremlin server; there, the response will be converted into a graph; from the node server, the user will be able to send other queries and receive complex responses. How can I achieve that?
I tried to use [1] but I'm not sure how can I do what I want. How do you actually send multiple (chained) commands, like a mini-script? I know how to do it using the console, but not using the server (as a REST service), since there is not an exposed API. Can you give me an advice or some examples? Thanks!
Adrian
[1] https://github.com/jbmusso/gremlin-javascript
Edit: I managed to send a POST message to the server with some simple commands. It works if I send a POST with all the commands (the graph creation and the query). However, for multiple POSTs, the context is not saved. First, I'm sending a POST with the content:
{
"gremlin" : "graph = TinkerGraph.open(); g = graph .traversal(); graph .addVertex('name', 'adrian', 'age', 21); "
}
Then, I send another POST with
{
"gremlin" : "v2 = graph.addVertex('name', 'gigel', 'age', 22); g.V()"
}
However, I get the following error:
{
"message": "No such property: graf for class: Script4",
"Exception-Class": "groovy.lang.MissingPropertyException",
"exceptions": [
"groovy.lang.MissingPropertyException"
],
"stackTrace": "groovy.lang.MissingPropertyException: No such property: graf for class: Script4\n\tat org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)\n\tat org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)\n\tat org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)\n\tat Script4.run(Script4.groovy:1)\n\tat org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:834)\n\tat org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:547)\n\tat javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)\n\tat org.apache.tinkerpop.gremlin.groovy.engine.ScriptEngines.eval(ScriptEngines.java:120)\n\tat org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.lambda$eval$2(GremlinExecutor.java:314)\n\tat java.util.concurrent.FutureTask.run(FutureTask.java:266)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)\n\tat java.lang.Thread.run(Thread.java:745)\n"
}
The context is not saved on the gremlin server? I must send the entire script to be executed? What if I want to create two graphs and query them later?
Thanks!
Graphs you create in scripts through sessionless requests (like REST) are not maintained. You can create graph instances that are maintained between requests in a session but then they are only maintained in that session and not reachable outside of that session.
If you want to create graphs globally for the server they must be:
GraphManager
instance (which involves writing some Java code)I suppose it would be also possible to build a custom endpoint that let you manage he graphs in some way, but you'd have to get more than a cursory knowledge of Gremlin Server's code to pull that off (wouldn't be too hard though).