How to do conditional upsert with variables?

Moved from GitHub pydgraph/103

Posted by robin-snt:

In #102 I had problems with the upsert in that example, which were resolved when setting name as a hard coded string value: NODE as q(func: eq(name, "foo")).

This confirms that what would to me seem as an intuitive way to combine query variables and upsert does not work.

Is it possible to do such an operation and if so, can you perhaps provide documentation for doing this correctly?

martinmr commented :

You should be able to include the variables declared in the query inside the condition.

See the example in this section of our docs: https://docs.dgraph.io/mutations/#conditional-upsert

EDIT: this works only with variables defined inside the query, not with GraphQL-like variables (e.g $name, etc).

martinmr commented :

@mangalaman93 can you quickly check if my answer is correct? Thanks.

robin-snt commented :

EDIT: this works only with variables defined inside the query, not with GraphQL-like variables (e.g $name, etc).

I believe this is what I’m looking for. I am referring to the pydgraph client functionality to correctly pass variables to the upsert query as shown in this standard query example: pydgraph/simple.py at master · dgraph-io/pydgraph · GitHub – I want to do the same for upsert queries.

I’ve not been able to pass python variables into an upsert query and I’ve had to resort to python string interpolation, which is far from ideal.

mangalaman93 commented :

This is correct. GraphQL variables do not work with upsert currently. There is an issue pending here Upsert query variables fails · Issue #4286 · dgraph-io/dgraph · GitHub. Let me see if we could get this fixed soon.

robin-snt commented :

I think this should be stated explicitly in documentation.

mangalaman93 commented :

This would be fixed soon, it is filed as a bug.

mangalaman93 commented :

This is already fixed in dgraph/master. Please let us know if you still face any issues.