# GraphQL api automation using Postman

**URL:** <https://community.postman.com/t/graphql-api-automation-using-postman/14775>\
**Category:** Help Hub\
**Tags:** graphql\
**Created:** [August 6, 2020, 10:44am UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775 "2020-08-06T10:44:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gouri.ghode](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@gouri.ghode](https://community.postman.com/u/gouri.ghode)\
**Post date:** [August 6, 2020, 10:44am UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/1 "2020-08-06T10:44:14Z")

</div>

Hello team, I need to execute GraphQL api with mutation and need to parameterize query variable with different values defined in Pre-request script.

Example:

```
mutation{
       createUser
      {
          name:"$username"
      }
}

Query variable used
{
     "username":"user1"
}

```

Value “user1” should get referred in name variable in mutation

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 6, 2020, 12:47pm UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/2 "2020-08-06T12:47:25Z")

</div>

Hi @gouri.ghode!

Welcome to the community! 👏

So to be clear, are you looking to execute this request more than once? If so it looks like you will want to use Collection Runner.

To start though, your Example looks mostly correct, except now for the “username” section, the value can be a postman variable, by using the curly braces, as such: `{{username}}`

```
 Query variable used
{
     "username":"{{username}}"
}

```

Once you have that in place, you can then use the Collection Runner within Postman to run against a CSV file containing different usernames.

For help with this, I would recommend more information around Collection Runner and GraphQL.

Collection Runner Docs:  
[https://learning.postman.com/docs/running-collections/intro-to-collection-runs/](https://learning.postman.com/docs/running-collections/intro-to-collection-runs/)

Collection Runner Video:

[![](https://img.youtube.com/vi/JYeiJIZIJbA/maxresdefault.jpg "What is Postman Console and Runner? Introduction to Postman") ](https://www.youtube.com/watch?v=JYeiJIZIJbA)

GraphQL Video:

[![](https://img.youtube.com/vi/GLdcIXjkb7k/maxresdefault.jpg "Sending GraphQL Queries in Postman") ](https://www.youtube.com/watch?v=GLdcIXjkb7k)

Putting these pieces together, you should be able to accomplish what you need to.

Let me know if this helps!

---

<div class="post-metadata">

**Author:** ![kevin.swiber](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevin.swiber/32/3820_2.png) [@kevin.swiber](https://community.postman.com/u/kevin.swiber)\
**Post date:** [August 6, 2020, 6:25pm UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/3 "2020-08-06T18:25:34Z")

</div>

Hi @gouri.ghode,

I think your GraphQL mutation syntax might be a bit off. I’m unclear if you want to use GraphQL variables or just Postman variables, so I’ll provide both options. If you’re just using Postman variables, your query should look something like:

```auto
mutation {
    createUser(name: "{{username}}") {
        name
    }
}

```

As long as you have the `username` variable set, this should work just fine. It sounds like you’re setting this in the pre-request script, which should be perfect.

Alternatively, you can use GraphQL variables _and_ Postman variables.

For that approach, your query should look something like:

```auto
mutation CreateUser($username: String!) {
    createUser(name: $username) {
        name
    }
}

```

And your variables should look like:

```json
{
    "username": "{{username}}"
}

```

Hope that helps.

Best,

Kevin

---

<div class="post-metadata">

**Author:** ![gouri.ghode](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@gouri.ghode](https://community.postman.com/u/gouri.ghode)\
**Post date:** [August 11, 2020, 9:08am UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/4 "2020-08-11T09:08:06Z")

</div>

First of all Thank You.

Based on your inputs i tried both approaches

1. With only postman variables(getting value from Pre-request script) api execution working properly
2. With Query variable i tried but it is not working

Also I am not able to understand exact difference between when to use Query variables in GraphQL instead of Postman variable and what is it’s significance. It will be really helpful if you can share some points on this.

Thanks again

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 12, 2020, 1:03pm UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/5 "2020-08-12T13:03:02Z")

</div>

Hi @gouri.ghode,

Can you show us the error you receive when using the Query variables? Screenshots should help us troubleshoot.

The difference between using Query variables in GraphQL instead of Postman variables is that you can use the GraphQL variables outside of Postman when making GraphQL requests. This is part of the definition of the GraphQL specification. Its the more correct way of using variables inside GraphQL queries. Thats really all there is to it. I would recommend using Postman variables in the GraphQL variables, so that when you need to export it to another client, or share it at all, you can just simply change the GraphQL variables as needed, and your GraphQL query is all ready to go.

Let me know if this makes sense.

Best,  
Orest

---

<div class="post-metadata">

**Author:** ![gouri.ghode](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@gouri.ghode](https://community.postman.com/u/gouri.ghode)\
**Post date:** [August 14, 2020, 8:57am UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/6 "2020-08-14T08:57:06Z")

</div>

Hi,

Thanks for Explanation of difference between using Query variables in GraphQL, this is really helpful.

Related to error faced while referring GraphQL variable, from Pre-request script it is getting referred to GraphQL variable declared however that value is not getting referred in request body at end and i am getting api related error as value is not getting passed.

But currently my api collection is working properly using query variables.

Thanks

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [August 14, 2020, 4:50pm UTC](https://community.postman.com/t/graphql-api-automation-using-postman/14775/7 "2020-08-14T16:50:40Z")

</div>

Hi @gouri.ghode,

You’re welcome.

It seems like you may have not set the variable in the Postman Environment you’re using. I am not exactly sure how you are getting into this issue, but I just made a video that might be helpful.

[![](https://img.youtube.com/vi/qSOCxxUHeM8/maxresdefault.jpg "Learning GraphQL With Postman") ](https://www.youtube.com/watch?v=qSOCxxUHeM8)

If you still have an issue, can you show me a screenshot of your setup? Including your Pre-request script, GraphQL query, and Postman Environment? That should clear things up for me to provide the best advice.

I hope this helps!
