3

I have created an AWS AppSync graphql api which on being called will run an AWS Lambda function and that function will create a vertex and edge using query language Gremlin but I am unable create edge after vertex is successfully created and AWS AppSync giving me this error "message": "g.addE(...).from is not a function"

This is my lambda function code please check it if there is any problem in my code? tell me where is my mistake?

import { process as gprocess } from 'gremlin';
import Post from './Post'
const gremlin = require('gremlin')
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection
const Graph = gremlin.structure.Graph
const uri = process.env.WRITER
const { t, P, } = gprocess;
const __ = gprocess.statics;

async function createPost(post: Post) {

    let dc = new DriverRemoteConnection(`wss://${uri}/gremlin`, {})

    const graph = new Graph()
    const g = graph.traversal().withRemote(dc)  

    let vertex = await g.addV('posts').property('title',post.title).property('content', post.content).property('id', post.id).next()

    let edge = await g.addE('post_to_post').from(g.V().hasLabel('posts').next()).to(g.V().hasLabel('posts').next()).next()

    dc.close()

    return post;
}

export default createPost
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38

1 Answers1

1

In Javascript from is a reserved word. When Gremlin has such conflicts the convention for step naming in Javascript is to append a underscore to the suffix of the step. Therefore you would refer to it as from_(). You can see that in the documentation for from() here and note that there are other similar differences in Javascript for other steps denoted here.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • 1
    thank you for answer me @stephen mallette but my problem is not solved when i used this step-modulator from_() is give me an erorr like this Server error: {\"code\":\"InternalFailureException\",\"requestId\":\"85e86728-f995-4e71-a850-a9899a34a9bb\",\"detailedMessage\":\"Could not locate method: NeptuneGraphTraversal.from(LinkedHashMap)\"} (599) – Hamza Ahmed Sheikh Sep 29 '21 at 16:45
  • i think you have some other errors in your code still that i didn't notice at first (or the recent edit added - not sure). anyway, you want to use an anonymous traversal, to steps that take them as arguments, therefore: `from_(__.V().hasLabel('posts'))` - in other words you don't use "g" or call `next()` - it just becomes a child to the main traversal. – stephen mallette Sep 29 '21 at 19:27
  • 1
    when you call `next()` to get `v0` and `v1` you are assign a `Promise` to those values so you can't pass that to `from_()` and `to()` as those expect a `Vertex` you would need to do something like `v0.value` to access the `Vertex` object. – stephen mallette Jan 10 '22 at 14:55
  • Thank you @stephenmallette, you pointed out my mistake, the following works as you explained (access `v0.value)`: `e = await g.addE("e").from_(v0.value).to(v1.value).next()` – Tristan Jan 10 '22 at 15:10