# F-strings vs variables

**URL:** <https://discuss.dgraph.io/t/f-strings-vs-variables/11606>\
**Category:** Dgraph Clients\
**Tags:** kind:question, status:accepted, area:documentation, pydgraph, ticket:created\
**Created:** [November 24, 2020, 10:53am UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606 "2020-11-24T10:53:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [November 24, 2020, 10:53am UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/1 "2020-11-24T10:53:04Z")

</div>

Hi,

I am using the python SDK.  
when creating queries or mutations I can use f-string to alter the query on runtime.  
is it ok or should I use variables?

Thaks,  
Or

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [November 24, 2020, 12:30pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/2 "2020-11-24T12:30:54Z")

</div>

Hi @spinelsun

DQL upserts don’t support variables yet, so IMO F-strings (string interpolation) is a good option here.  
For queries, using variables is better programming practice as it allows independent testing of query artifacts.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [November 25, 2020, 3:45pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/3 "2020-11-25T15:45:23Z")

</div>

Hi @anand

> The `txn.do_request` function allows you to run upserts consisting of one query and one mutation. Query variables could be defined and can then be used in the mutation.  
> To know more about upsert, we highly recommend going through the [mutations docs](https://docs.dgraph.io/mutations/#upsert-block).

It says one query and **one mutation** but in the upsert documentation, it said that an upsert can have several mutations. The SDK gets an array of `Mutation` in `txn.create_request` function.  
Also, the SDK allows to pass variables in `txn.create_request` so will it work or fail if using this option instead of `f-string`?

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [November 25, 2020, 4:26pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/4 "2020-11-25T16:26:32Z")

</div>

1. We can send multiple mutation in the upsert block.
2. I could not find an example using a `request` with variables.  
@MichelDiz Could you please advise?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [November 25, 2020, 4:32pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/5 "2020-11-25T16:32:59Z")

</div>

Not sure, @martinmr might help.

One thing to mention is that DQL Vars (A.K.A. old “GraphQL Variables”) can be used in dgo client. I remember that some user was using this. Not sure if you guys are talking about same variable tho.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [November 25, 2020, 5:44pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/6 "2020-11-25T17:44:45Z")

</div>

@anand:

1. Ok, so it will be grate if you update the readme file in pydgraph github page.

@MichelDiz @martinmr, this is the code that I used to implement an upsert with condition:

```auto
    with db_utils.get_client() as client:
        query = """
        {
            KEY as var(func: uid($uid))
        }
        """
        condition = "@if(eq(len(key), 1))"
        variables = {'$uid': uid}
        data = f"uid(KEY) <ApiKeySet.title> {new_title}"
        txn = client.txn()
        try:
            mutation = txn.create_mutation(cond=condition, set_nquads=data)
            db_request = txn.create_request(mutations=[mutation],
                                            query=query,
                                            variables=variables,
                                            commit_now=True)
            result = txn.do_request(db_request)
        except pydgraph.AbortedError as pdae:
            msg = f'Operation failed due to the following error: {str(pdae)}'

            return Response(msg, status.HTTP_500_INTERNAL_SERVER_ERROR)

    response = json.loads(result.json)
    succeeded = response['code'].lower() == 'success'
    response_msg = response['message']

    if not succeeded:
        return Response(response_msg, status.HTTP_500_INTERNAL_SERVER_ERROR)

    return Response(_SUCCESS_MESSAGE, status.HTTP_200_OK)

```

I used DQL Vars and query Vars, which according to the documentation, both should work.  
If you something that is not correct please let me know

Also for some reasone the upsert with condition doesn’t work in Ratel do you know why?  
This is what I do:

```auto
upsert{
  query {
    U as q(func: uid("0x222ef")){
       uid
    }
  }

  mutation @if(eq(len(U), 1)) {
    set {
        uid(U) <ApiKeySet.access_key> "eoxsp1XJoVCKBSwmoasj" .
    }
  }
}

```

The condition is always skipped and the set works no matter which uid I give even if it doesn’t exist in the db

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [November 25, 2020, 5:48pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/7 "2020-11-25T17:48:39Z")

</div>

> [@spinelsun](#):
>
> Also for some reasone the upsert with condition doesn’t work in Ratel do you know why?

Ratel uses dgraph-js-http. The HTTP API badly supports the DQL Vars. That’s something we need to take a look at. One of the points is that the mutation operation was never meant to accept DQL Vars. Just the Query operation. In Ratel you just use a single operation per query(in a gRPC client you can run several operations), which in this case is Mutation.

---

<div class="post-metadata">

**Author:** ![anand](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anand/32/5674_2.png) [@anand](https://discuss.dgraph.io/u/anand)\
**Post date:** [November 25, 2020, 5:52pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/8 "2020-11-25T17:52:19Z")

</div>

> [@spinelsun](#):
>
> Ok, so it will be grate if you update the readme file in pydgraph github page.

Hi @spinelsun  
I have accepted this and marked this for documentation changes. Thanks for the feedback.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [November 25, 2020, 5:56pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/9 "2020-11-25T17:56:17Z")

</div>

@MichelDiz Ok so for now you say that Ratel can’t handle the upsert operation?  
If that’s what you mean I would be happy to know when you fix it.

Thanks alot

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [November 25, 2020, 6:19pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/10 "2020-11-25T18:19:18Z")

</div>

> [@spinelsun](#):
>
> Ok so for now you say that Ratel can’t handle the upsert operation?

It can, what it can’t is using DQL Vars in Upsert queries.

---

<div class="post-metadata">

**Author:** ![spinelsun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/spinelsun/32/4596_2.png) [@spinelsun](https://discuss.dgraph.io/u/spinelsun)\
**Post date:** [November 26, 2020, 6:17am UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/12 "2020-11-26T06:17:35Z")

</div>

By saying DQL vars you mean the variable like GQL?  
I didn’t use them in my upsert, I used only query variables which is the only way I can pass the uid of the quiried node.  
The upsert I mentioned above doesn’t work - it ignores the condition and always set the triple even if the query returns nothing

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [November 26, 2020, 4:06pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/14 "2020-11-26T16:06:06Z")

</div>

> [@spinelsun](#):
>
> By saying DQL vars you mean the variable like GQL?

No, I’m talking about this [GraphQL Variables - Query language](https://dgraph.io/docs/query-language/graphql-variables/)

So, as you don’t know what it was. Just ignore what I have said.

> [@spinelsun](#):
>
> The upsert I mentioned above doesn’t work - it ignores the condition and always set the triple even if the query returns nothing

You should not rely on your conditions in the UID `U as q(func: uid("0x222ef")){` cuz the UID will always be true in Dgraph. Change it to a value that you know that the entity should have. Or use `@cascade` directive and add some fields.

---

<div class="post-metadata">

**Author:** ![acarey](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/acarey/32/7089_2.png) [@acarey](https://discuss.dgraph.io/u/acarey)\
**Post date:** [December 23, 2020, 5:43pm UTC](https://discuss.dgraph.io/t/f-strings-vs-variables/11606/15 "2020-12-23T17:43:10Z")

</div>

Hi, @spinelsun! That section of the readme file for pydgraph is now updated in the following PR: [Docs (discuss feedback): fix incorrect statement about upsert blocks allowing one query and one mutation by aaroncarey · Pull Request #161 · dgraph-io/pydgraph · GitHub](https://github.com/dgraph-io/pydgraph/pull/161)

Thanks for raising this issue.
