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.