Dgraph-http-client setJson creates duplicate nodes

When using the dgraph-http-js-client to perform mutation like this simple example:

try {
        const person = {
            'dgraph.type': 'Customer',
            'uid': '_:michael',
            'Customer.username': "Michael"
        }

        // Perform any number of queries and mutations
        const response = await txn.mutate({ setJson: [
            person
          ]
        });
       
        await txn.commit();
    } catch (e) {
        if (e === dgraph.ERR_ABORTED) {
          
        } else {
            throw e;
        }
    } finally {
        await txn.discard();
    }

Results: duplicate “Michael” nodes are created.
However, when using the online interface to do this:

mutation {
  addCustomer(input: [{ username: "Michael"}]) {
    customer {
      username
    }
  }
}

The result is:

{
  "errors": [
    {
      "message": "mutation addCustomer failed because Found multiple nodes with ID: 0xfffd8d6acce814d2",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "addCustomer"
      ]
    }
  ],
  "data": {
    "addCustomer": null
  },.....

Which is correct because “Michael” already exists.
The question is why the first method of mutate keeps creating the “Michael” nodes?
Many thanks in advance.

You should not mix GraphQL and DQL before knowing how they work.

DQL won’t upsert based in the username. You can do it manually using the UID of the user or using Upsert Mutation. Otherwise it will create new nodes all the time. DQL is different from GraphQL. In order to mix those, you need to understand de model.

Cheers. DQL and GraphQL wont be mixed…was learning to see the difference. It is now obvious that an upsert needs to be performed from with Javascript client: get the UID then perform the mutation.