Pydgraph mutation with query+variables


Report a Dgraph Client Bug

What Dgraph client (and version) are you using?

(put “x” in the box to select)

  • PyDgraph

Version:
21.3.2

What version of Dgraph are you using?

v21.12

Have you tried reproducing the issue with the latest release?

No

Steps to reproduce the issue (command/config used to run Dgraph).

txn = dql_client.txn()

mutation = txn.create_mutation(
    set_obj={ 'value': 'this is a test' },
)

request = txn.create_request(
    query=r'{ TEST as var(func: eq(slug, $slug)) }',
    mutations=[mutation],
    variables={ '$slug': 'a-slug' },
    commit_now=True
)

res = txn.do_request(request)

Expected behaviour and actual result.

I was hoping to be able to use mutation with query+variables.
This is the error I’m getting:

           grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
               status = StatusCode.UNKNOWN
               details = "Variable not defined $slug"
               debug_error_string = "UNKNOWN:Error received from peer  {created_time:"2023-08-31T18:58:44.532179682+00:00", grpc_status:2, grpc_message:"Variable not defined $slug"}"

1 Like

Check the pydgraph readme file, it contains several examples: GitHub - dgraph-io/pydgraph: Official Dgraph Python client
It seems you are missing the variable declaration (query query_name($myvariable: string)) before the actual query.

Thanks that was it!

Do you know by any chance how to do this over the /mutate endpoint? I can’t get it to receive the variables list:

$ curl -v -H "Content-Type: application/json"  -X POST localhost:8080/mutate?commitNow=true --data-binary @- << EOF
{
    "query": "query lookup($slug: string!) { TEST as var(func: eq(slug, $slug)) }",
    "mutations": [
        {
            "value": "this is a test"
        }
    ],
    "variables": {
        "$slug": "a-slug"
    }
}
EOF

Result:

{"errors":[{"message":"Variable $slug should be initialised","extensions":{"code":"ErrorInvalidRequest"}}],"data":null}
1 Like

No idea, sorry. I usually use the Golang client.

@markushedlund Check out this thread: Support DQL variables in mutations - #15 by Poolshark

You’ll have to settle for string transforms in your python code unfortunately.

1 Like