Dgraph graphql How to create new multiple nodes with edges to existing nodes?

example

(bragdish) #1

I am referencing the example used in this blog post to base my question :
https://discuss.dgraph.io/t/dgraph-tutorials-series-2-uid-operations-updating-nodes-and-traversals-dgraph-blog/5251

Assuming that currently we have :
Michael < follows > Pawan
Pawan < follows > Leyla
Leyla < follows > Micheal

Imagine I have a dataset of two columns in excel that I want to add to this graph

column1 : person name : (eg. Ryan)
Column 2 : follows ( either Michael /pawan/ Leyla)

now if there was a single row I could go with the query :

{
  "set":[
    {
      "uid": "RYANS_UID",
      "follows": {
        "uid": "MICHAELS_UID"
      }
    }
  ]
}

However instead of manually looking up RYAN and MICHEAL’s UID is there an upsert block that can do it programmatically ?

I tried :

upsert {
  query {
    v as var(func: eq(name, "Michael"))
  }

  mutation {
    set {
      "name" : "RYAN",
	  "age" : "26",
	  "follows" : { uid(v)}
    }
  }
}

I get the following error

Error: t: while lexing upsert { query { v as var(func: eq(name, “Michael”)) } mutation { set { “name” : “RYAN”, “age” : “26”, “follows” : { uid(v)} } } } at line 7 column 9: Invalid character ‘{’ inside mutation text

The context for this question is I have modeled master data as nodes and I would like to add events linking to existing nodes using Ratel UI


(Shekar Mantha) #2

Thank you for the post. One of the dgraph team members will respond promptly.


(Michael Compton) #3

Hi,

There is an upsert block like you describe (docs : https://docs.dgraph.io/mutations/#upsert-block). However, at current released version it works with triples and not json. There’s a PR about to be merged that adds json support.


#4

Hi,
Do the dgraph version 1.0.16 support the upsert-block?


(Michel Conrado) #5

No, only v1.1.0+ above.


(bragdish) #6

Thanks for the reply. I followed your suggestion and was able to create node with the relationship

upsert {
  query {
    v as var(func: eq(name, "Michael"))
  }

  mutation {
    set {
       _:RYAN <follows>  uid(v).
      _:RYAN <name> "RYAN" .
      _:RYAN <age> "26" .
    }
  }
}