Python library syntax and string interpolation


#1

Hi,

I am working with the python client and I am a bit confused about how to use it appropriately.

The example code contains what looks like string interpolation using query syntax that is a bit different from the query syntax in the rest of the documentation. Is there somewhere I can read about how this works?

For example, the following is code from the github page for pydgraph:

query1 = """query all($a: string)
 {
   all(func: eq(name, $a)) 
    {
      uid
    }   
  }"""
  
variables1 = {'$a': 'Bob'}

res1 = txn.query(query1, variables=variables1)

ppl1 = json.loads(res1.json)

#For mutation to delete node, use this: 
txn.mutate(del_obj= person)

This works for me, but it’s unclear to me why the query all($a: string) is needed at all and how to extend this to multiple variables (my attempts failed).


(Michel Conrado) #2

This is GraphQL Variables. You can use it or not. There’s no obligation on it. It’s just a way to inject values in the query without need to edit the query itself.

To use multiple variables you need to set variables1 = {'$a': 'Bob', '$b': '5'} and define the type in the query eg. $b: int.

The word “all” is just a custom name for the query.


#3

Thanks Michel. Sorry, I missed that part of the documentation.


(TW) #4

How do I define multiple values for the same variable? E.g. to "show me everything for these $a "


(Michel Conrado) #5

You can’t. A key can hold just one value. But in master/v1.1 you can do
eq(predicate, [$var1, "value", ..., $varN]). So you can add several graphql variables and use them in the same target.

So you can use multiple variables in a single function.