Finding hard time deleting one node from array friend list

I am using dgraph golang client “dgo”.

Based on example here - https://godoc.org/github.com/dgraph-io/dgo#DeleteEdges

It shows, “Alice” has “friend” predicate and it has 2 edges going outwards to “Bob” and “Charlie”.

In example there, it shows, “Alice” deletes his “friend” list. But, how would I delete and unfriend only 1 people from the list.

In below example, it removes all friend and location data, but that is not how it works always. I only want to remove “Charlie” and “Alice” connection in “Friend” edge But Alice still remain friends with "Bob"

If I only want to remove Charlie from the friendship of “Alice”, how could one do that?

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

dc := api.NewDgraphClient(conn)
dg := dgo.NewDgraphClient(dc)

op := &api.Operation{}
op.Schema = `
			age: int .
			married: bool .
			name: string @lang .
			location: string .
		`

ctx := context.Background()
err = dg.Alter(ctx, op)
if err != nil {
    log.Fatal(err)
}

type School struct {
    Uid  string `json:"uid"`
    Name string `json:"name@en,omitempty"`
}

type Person struct {
    Uid      string    `json:"uid,omitempty"`
    Name     string    `json:"name,omitempty"`
    Age      int       `json:"age,omitempty"`
    Married  bool      `json:"married,omitempty"`
    Friends  []Person  `json:"friends,omitempty"`
    Location string    `json:"location,omitempty"`
    Schools  []*School `json:"schools,omitempty"`
}

// Lets add some data first.
p := Person{
    Name:     "Alice",
    Age:      26,
    Married:  true,
    Location: "Riley Street",
    Friends: []Person{{
        Name: "Bob",
        Age:  24,
    }, {
        Name: "Charlie",
        Age:  29,
    }},
    Schools: []*School{&School{
        Name: "Crown Public School",
    }},
}

mu := &api.Mutation{}
pb, err := json.Marshal(p)
if err != nil {
    log.Fatal(err)
}

mu.SetJson = pb
mu.CommitNow = true
mu.IgnoreIndexConflict = true
assigned, err := dg.NewTxn().Mutate(ctx, mu)
if err != nil {
    log.Fatal(err)
}

alice := assigned.Uids["blank-0"]

variables := make(map[string]string)
variables["$alice"] = alice
const q = `query Me($alice: string){
		me(func: uid($alice)) {
			name
			age
			loc
			married
			friends {
				name
				age
			}
			schools {
				name@en
			}
		}
	}`

resp, err := dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
    log.Fatal(err)
}

// Now lets delete the friend and location edge from Alice
mu = &api.Mutation{}
dgo.DeleteEdges(mu, alice, "friends", "loc")

mu.CommitNow = true
_, err = dg.NewTxn().Mutate(ctx, mu)
if err != nil {
    log.Fatal(err)
}

resp, err = dg.NewTxn().QueryWithVars(ctx, q, variables)
if err != nil {
    log.Fatal(err)
}

type Root struct {
    Me []Person `json:"me"`
}

var r Root
err = json.Unmarshal(resp.Json, &r)
fmt.Println(string(resp.Json))

how can this be accomplished?

If you’re using JSON just:

{
  "uid": "0x123", Alice UID
  "friend": {
    "uid": "0x456"  Charlie UID
  }
}

https://docs.dgraph.io/mutations/#edges-between-nodes

1 Like

I am using dgo client. How can I pass with JSON, can you please provide full code to delete using JSON

As I said before, I have no practice with GoLang. But look, in the example there is “mu.SetJson = pb” That is, it is sending a mutation in JSON format. Looking at the api.proto I can see that there is a operation, called “delete_json”. In this case, test with this syntax.

Take a look on the api.proto

and