Golang SDK - Get ID after creating new node

77 views
Skip to first unread message

Laura Poss

unread,
Jun 27, 2023, 2:08:21 AM6/27/23
to Gremlin-users
Hi! I'm using gremlin-go to interact with my graph and was wondering if there is an easier way to get the ID of a new node.

Right now I'm creating the node like so:
promise := g.AddV("my_node_type").Property("name", "foobar").Iterate()
err := <-promise
if err != nil {
  return err
}

But to get the ID I need to read the new node back from the graph:
result, err := g.V().HasLabel("my_node_type").Has("name", "foobar").ToList()
if err != nil {
  return err
}
if len(result) == 0 {
  return fmt.Errorf("no results")
}
id := result[0].GetString()

Seems like in other languages the SDK will return the ID of the new node after its created. Is there a better way than what I'm doing in Golang?

Yang Xia

unread,
Jun 27, 2023, 5:50:41 PM6/27/23
to gremli...@googlegroups.com
Hi,

The step "Iterate()" is a terminal step which sends the traversal through the remote connection, by definition it does not return anything (same across all clients), see https://tinkerpop.apache.org/docs/current/reference/#terminal-steps.

So if you want to get the id returned within the same query, use another terminal step that returns a result, e.g. "Next()" inteas of "Iterate()".

result, err := g.AddV("my_node_type").Property("name", "foobar").Next()
if err != nil {
  return err
}
id := result.GetString()

Hopefully this helps. 

Yang

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/6127c2d5-e50c-46f1-b269-93334bdb14a5n%40googlegroups.com.

Laura Poss

unread,
Jun 28, 2023, 2:24:01 AM6/28/23
to Gremlin-users
This worked perfectly! Thanks for flagging the terminal steps!

Yang Xia

unread,
Jun 28, 2023, 6:30:18 PM6/28/23
to gremli...@googlegroups.com
You are welcome, glad it works for you! 

Reply all
Reply to author
Forward
0 new messages