Get query result of upsert block

Hi,

Previously posted in: https://github.com/dgraph-io/dgo/issues/114

In Dgo, Is it possible to retrieve the query result of an upsert block? I checked the resp.Json field of the api.Response from tx.Do, it returns an empty object {}.

Here is an example code.

schema := `username: string @index(term) @upsert .
email: string @index(term) @upsert .
no: int @index(int) @upsert .
name: string @index(term) .

type User {
	name
	username
	email
	no
}`

err := c.Alter(context.Background(), &api.Operation{Schema: schema})
if err != nil {
	log.Println(err)
}

query := `{
	q2 as var(func: type(User)) @filter(eq(username, "wildan")) { uid }
	q3 as var(func: type(User)) @filter(eq(email, "wildan2711@gmail.com")) { uid }
}`
condition := `@if(eq(len(q2), 0) AND eq(len(q3), 0))`
jsonData := `{"dgraph.type":"User","name":"H3h3","username":"wildan","email":"wildan2711@gmail.com","no":1}`

tx := c.NewTxn()

assigned, err := tx.Do(context.Background(), &api.Request{
	Query: query,
	Mutations: []*api.Mutation{
		&api.Mutation{
			Cond:    conditionString,
			SetJson: jsonData,
		}
	},
	CommitNow: true,
})

log.Println(string(assigned.Json), assigned.Uids)

tx = c.NewTxn()

assigned, err = tx.Do(context.Background(), &api.Request{
	Query: query,
	Mutations: []*api.Mutation{
		&api.Mutation{
			Cond:    conditionString,
			SetJson: jsonData,
		}
	},
	CommitNow: true,
})

log.Println(string(assigned.Json), assigned.Uids)

Which returns:

2020/01/04 13:16:01 {} map[dg.675717852.13:0x4e27]
2020/01/04 13:16:01 {} map[]

I see that in https://docs.dgraph.io/master/mutations/#upsert-block is shows a return object q that returns the query result when doing an Upsert Block. I was hoping for something similar in the dgo client.

Thanks.

In no client you can do this, but theres an update that you can retrieve some information check this issue https://github.com/dgraph-io/dgraph/issues/4048

About your last comment, this is from latest releases.

BTW, if you’re using the right Dgraph version. You not getting the response cuz you’re using “var” instead of a proper name for a query. Var is hidden by default.

I hadn’t noticed block var until @amanmangal warned me about.

2 Likes

Thanks. I modified the query as below, it works now:

query := `{
	q2(func: type(User)) @filter(eq(username, "wildan")) { u2 as uid }
	q3(func: type(User)) @filter(eq(email, "wildan2711@gmail.com")) { u3 as uid }
}`
condition := `@if(eq(len(u2), 0) AND eq(len(u3), 0))`