GraphQL api automation using Postman

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

Hi @gouri.ghode!

Welcome to the community! :clap:

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/

Collection Runner Video:

GraphQL Video:

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

Let me know if this helps!

1 Like

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:

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:

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

And your variables should look like:

{
    "username": "{{username}}"
}

Hope that helps.

Best,

Kevin

1 Like

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

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

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

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.

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!