Nothing changes when making mutations on Slash GraphQL with PyDgraph

Anyone using the Python Drgraph client?

I’ve been trying to get it to work and I can only get queries to work, with mutations I get a response back that has weird UIDS and nothing actually changes.

I’m doing super simple ones to just to test.

addState = {
  "State.block_num": 1000
}


txn = client.txn()
res = txn.mutate(set_obj=addState)
print(res)
txn.commit()
txn.discard()
client_stub.close()

The response I get is this:

uids {
  key: "dg.3860428495.25"
  value: "0x138b3"
}

But nothing has changed.

I am using pydgraph.DgraphClientStub.from_slash_endpoint to create a stub, including an API code. Been banging my head on this for a while and the docs are no help.

Hey @billybobthorton,

can you please specify the dgraph.type in the addState and then look for data changes?

addState = {
  "State.block_num": 1000,
  "dgraph.type": "State",
}

Please let me know if you still face any issues.

1 Like

That did it! It created a new record, how do I tell it to update an existing record instead of creating a new one? For this particular type, I have no primary key, it’s simply one field that is used as a global app stage to know how far along it is.

Also, what does this output mean? Is this because I have no primary key (ID)?

uids {
  key: "dg.1020851461.4"
  value: "0x15f95"
}

Hi @billybobthorton,
Let’s try a small variation as below.

addState = {
  "uid" : "_:newNode",
  "State.block_num": 1000
}
txn = client.txn()
res = txn.mutate(set_obj=addState)
print(res)
txn.commit()

When you run this, you will see a response similar to the one below.

uids {
  key: "newNode"
  value: "0x7"
}

We asked Dgraph to create a new node with the name newNode. The _:newNode is called a blank node. Dgraph reports the uid of the new node created. You can query for the node created using the uid function. The key you saw in your query’s response is an autogenerated id. You could use a blank node for clarity.

DQL does not enforce the concept of a primary key. If you want to search for a node and update, (and create the node if it does not exists), please review the upsert block example. Here is some more documentation. Please give this a try.

1 Like

Ahh, I thought upsert was just to do a query and mutate together, didn’t know it was for finding something to update.

Let’s try a small variation as below.

Doesn’t work without "dgraph.type": "State"

When I add that though, I get this

uids {
  key: "newNode"
  value: "0x186a4"
}

Hi @billybobthorton,

Nodes are still created if you don’t pass dgraph.type and can be queried via Ratel, but not via GraphQL explorer. Just FYI.