DGO issues with valid JSON mutation

Report a Dgraph Client Issue

What Dgraph client (and version) are you using?

  • Dgo

Version:
v210

What version of Dgraph are you using?

Latest

What you wanted to do

DGO Json Upsert Mutation

What you actually did

	mu := &api.Mutation{
		CommitNow: true,
		SetJson:   b,
	}

	response, err := s.DClient.NewTxn().Mutate(context.Background(), mu)
	if err != nil {
		log.Fatal(err)
	}

The byte loaded in b looks like:

{
  "query": "{ q(func: eq(containerId, 12345)) {v as uid} }",
  "set": {
    "containerId": "12345",
    "name": "test",
    "uid": "uid(v)"
  }
}

Error:

variables [v] not defined

Ratle or the http endpoint on the other hand processes the mutation without any problems (see below)

Docs:

https://dgraph.io/docs/dql/mutations/uid-upsert/

Raw-Http API

This line looks wrong. Check the Dgo repo for examples. BTW, transaction should be open before.

1 Like

JavaScript has the same issue

const dgraph = require("dgraph-js");

// Create a client stub.
function newClientStub() {
    return new dgraph.DgraphClientStub("localhost:9080");
}

// Create a client.
function newClient(clientStub) {
    return new dgraph.DgraphClient(clientStub);
}

// Create data using JSON.
async function createData(dgraphClient) {
    // Create a new transaction.
    const txn = dgraphClient.newTxn();
    try {
        // Create data.
        const p = {
            "query": "{ q(func: eq(containerId, 187263)) {v as uid} }",
            "set": {
              "containerId": "187263",
              "name": "test",
              "uid": "uid(v)"
            }
          };

        // Run mutation.
        const mu = new dgraph.Mutation();
        mu.setSetJson(p);
        const response = await txn.mutate(mu);

        // Commit transaction.
        await txn.commit();

        response
            .getUidsMap()
            .forEach((uid, key) => console.log(`${key} => ${uid}`));
        console.log();
    } finally {
        // Clean up. Calling this after txn.commit() is a no-op
        // and hence safe.
        await txn.discard();
    }
}


async function main() {
    const dgraphClientStub = newClientStub();
    const dgraphClient = newClient(dgraphClientStub);

    await createData(dgraphClient);


    // Close the client stub.
    dgraphClientStub.close();
}

main()
    .then(() => {
        console.log("\nDONE!");
    })
    .catch((e) => {
        console.log("ERROR: ", e);
    });

Error

  code: 2,
  details: 'variables [v] not defined',
  metadata: Metadata {
    internalRepr: Map(1) { 'content-type' => [Array] },
    options: {}
  }
}

I can’t find anything new in the repo.

I used the example of Mutation with SetJson Example line 181

I thought just “Do” should not work since its not for querying Line 199

Since I get the same issue in JavaScript, is it possible that the “SetJson” type in GRPC calls does not support the usage of querys ?

This approach is working well so far :smiley:

	query := JSONUpsert{
		Set: jsonMap,
	}

	b, err := json.Marshal(query)
	if err != nil {
		fmt.Println(err)
	}

	mu := &api.Mutation{
		CommitNow: true,
		SetJson:   b,
	}

	req := &api.Request{
		Query:     fmt.Sprintf("query { v as var(func: eq(containerId, %s))}", key),
		Mutations: []*api.Mutation{mu},
		CommitNow: true,
	}

	response, err := s.DClient.NewTxn().Do(context.Background(), req)

This won’t work cuz in dgraph-js it should go via a method. Only in dgraph-js-http you can do that.