How to update a predicate and node using go client

Hi,

I am trying to create apis for altering Dgraph nodes and predicates using go client, so my question is I can easily create an api for the create operation but if I want to update a node with a new field or to remove an existing field how can I achieve that and also if I want to change the predicate name to differentiate the type data then how can I acheive that ??

can you explain me how to achieve this in Dgraph and if possible please direct me to the right reference

You essentially need Upserts.

https://dgraph.io/docs/mutations/#upsert-block
https://dgraph.io/docs/mutations/#conditional-upsert

For the Go dgo client, here is how you would do it:

1 Like

Hi Paras, Upsert is for Query and mutation right, I am looking for altering the node itself like changing the predicates

Hi @teja, can you please tell us what do you want to achieve with a little dummy example so that we can help you better.

Hi @teja,

This can be achieved using upserts. Consider the following example.

{
  set {
    _:a <name> "alice" .
    _:a <country> "japan" .
  }
}

suppose you want to change the country of Alice, you can do this by:

upsert{
  query {
    q(func: eq(name, "alice")) {
      v as uid
      name
    }
  }

  mutation {
    set {
      uid(v) <country> "India" .
    }
  }
}

I want to change the predicate name to differentiate the type data then how can I acheive that ??

Now if you want to change the name of predicate itself, for example changing the name of predicate “name” to “first_name” by:

upsert{
  query {
    q(func: has(name)) {
      v as uid
      n as name
    }
  }

  mutation {
    set {
      uid(v) <first_name> val(n) .
    }
    
    delete{
      uid(v) <name> * .
    }
  }
}

I want to update a node with a new field or to remove an existing field how can I achieve that

Quite similar, the below example adds new predicate age, in the node with name equal to alice.

upsert{
  query {
    q(func: eq(name, "alice")) {
      v as uid
      name
    }
  }

  mutation {
    set {
      uid(v) <age> "21" .
    }
  }
}
1 Like

Heres an example,
type Person {
name
boss_of
works_for
}

type Company {
name
industry
work_here #this is an alias
}

industry: string @index(term) .
boss_of: [uid] .
name: string @index(exact, term) .
works_for: [uid] .
work_here: [uid] .

Now I want to change the “name” to “person_name” and “company_name”, so that I can distinguish these two predicates

so I want to get the schema and alter the name to person_name using go client

Hi @teja,

so I want to get the schema and alter the name to person_name using go client

I wrote a simple go program which change the “name” to “person_name” for the schema that you provided, similarly you can do it for company_name or in general for any predicate.


import (
	"context"
	"log"
	"google.golang.org/grpc"
	"github.com/dgraph-io/dgo/v200"
	"github.com/dgraph-io/dgo/v200/protos/api"
)

func main() {
	conn, err := grpc.Dial("127.0.0.1:9080", grpc.WithInsecure())
	if err != nil {
		log.Fatal("While trying to dial gRPC")
	}

	dc := dgo.NewDgraphClient(api.NewDgraphClient(conn))
	ctx := context.Background()

	q := `
		query {
	  		user as var(func: type(Person)){
	  			n as name
	  		}
	  	}`

	mu := &api.Mutation{
  		SetNquads: []byte(`uid(user) <person_name>  val(n) .`),
  		DelNquads: []byte(`uid(user) <name> * .`),
	}

	req := &api.Request{
	  Query: q,
	  Mutations: []*api.Mutation{mu},
	  CommitNow:true,
	}
	if _, err := dc.NewTxn().Do(ctx, req); err != nil {
  		log.Fatal(err)
	}
}