2

I'm trying to write fluent gremlin queries in nodejs for cosmos db, even though they get submitted as string. I've been through the documentation and I've seen it mentioned in a few github threads that although bytecode isn't yet supported, it is possible to submit it as scrip.

The code I have so far:

Configuring the client function:


export const CosmosConn = async (): Promise<driver.Client> => {
    try {
        const cosmosKey: string = await GetSecret('cosmos-key');
        const cosmosEndpoint: string = await GetSecret('cosmos-endpoint');

        const authenticator: driver.auth.PlainTextSaslAuthenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
            '/dbs/main/colls/main',
            cosmosKey
        );
        const client: driver.Client = new gremlin.driver.Client(cosmosEndpoint, {
            authenticator,
            traversalsource: 'g',
            rejectUnauthorized: true,
            mimeType: 'application/vnd.gremlin-v2.0+json'
        });

        return client;
    } catch (err) {
        console.error(err);
    }
};

Now these two below are temporary as i'll await that CosmosConn several times for every query, but this is for an Azure Function so i'm not optimizing yet:

export const Graph = async (query: gremlin.process.Bytecode): Promise<any> => {
    const db = await CosmosConn();
    const translator = new gremlin.process.Translator(
        new gremlin.process.AnonymousTraversalSource()
    );
    return db.submit(translator.translate(query));
};

export const getGremlin = async () => {
    const db = await CosmosConn();
    return gremlin.process.traversal().withRemote(db);
};

Now when I try to use it:

    const g = await getGremlin();
        const query = g
            .V()
            .hasLabel('client')
            .getBytecode();

        const test = await Graph(query);

This of course throws out an error:

Gremlin Query Syntax Error: Script compile error: Unexpected token: 'Object'; in input: '[objectObject'. @ line 1, column 9.

SebastianG
  • 8,563
  • 8
  • 47
  • 111

4 Answers4

1

Have you tried to print the translator.translate(query) prior submitting?

From my experience, the translator is very limited in its support for non-trivial queries.

According to Microsoft, they plan to support fluent API on Dec 19', so probably better to wait for official support.

Kfir Dadosh
  • 1,411
  • 9
  • 9
0

It was the types preventing me from initialising my translator in a way that works with CosmosDB.

    const translator = new gremlin.process.Translator('g' as any);

works.

SebastianG
  • 8,563
  • 8
  • 47
  • 111
0

Dawn below is an example of using Translator in TypeScript to convert bytecode query to string query for CosmosDB. I don't recommend this solution, like the other response pointed out: it's limited. Use AWS Neptune instead or wait until MS implements bytecode queries in CosmosDB.

async function test(): Promise<void> {
   // Connection:
   const traversal = Gremlin.process.AnonymousTraversalSource.traversal;
   const DriverRemoteConnection = Gremlin.driver.DriverRemoteConnection;
   const g = traversal().withRemote(new DriverRemoteConnection("ws://localhost:8182/gremlin"));

   // Create translator
   const translator = new Gremlin.process.Translator(g);

   // Convert bytecode query to string query for CosmosDB:
   console.log(translator.translate(g.V().hasLabel('person').values('name').getBytecode()))
}

test();
fermmm
  • 1,078
  • 1
  • 9
  • 17
-1

Here is the link to tests cases to get getbytecode translation working.

https://github.com/apache/tinkerpop/blob/master/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js#L31

Edit:-

Here is sample test case from above link

it('should produce valid script representation from bytecode glv steps', function () {
      const g = new graph.Graph().traversal();
      const script = new Translator('g').translate(g.V().out('created').getBytecode());
      assert.ok(script);
      assert.strictEqual(script, 'g.V().out(\'created\')');
});
iAviator
  • 1,310
  • 13
  • 31
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/29031266) – David Maze May 22 '21 at 11:32
  • Edited the answer with sample test case – iAviator May 22 '21 at 11:57