Use Environment variables inside of GraphQL variables

I’m trying to define GraphQL in the Body of a POST I’m making via Postman. An example of a valid “GraphQL Variables” section of my post is as follows:

{
    "buildingId": 8237,
    "accountId": 71
}

However, I’d like to specify environment variables in this section as well, since “buildingId” is going to vary between different environments. For example, I’d like my “GraphQL Variables” to look like this instead, where I can define “building_id” for each of my environments:

{
    "buildingId": {{building_id}},
    "accountId": {{account_id}}
}

Is there a way to accomplish this?

Hi @wfresch,

Welcome to the community! :clap:

From the looks of what you wrote, that is exactly the way you would accomplish it, same syntax and all. Difference is, that you create an environment for each of the environments you have, specify the variable with the same exact name, and give it its appropriate value. Then when you change environments, you will see that the variables’ value also changes.

If you need me to clarify more, I’d be more than happy to do so.

Regards,
Orest

1 Like

It’s not letting me use environment variables inside of that GraphQL box. I use environment variables inside my route URLs and headers, but Postman doesn’t allow them inside of the GraphQL box. I’ll add a screenshot tomorrow.

2 Likes

Is this what you’re looking to do?

I’m just using the Pokemon GraphQL endpoint here to use as an example but the same should work for any other example.

My GraphQL Request Body:

query ($name: String!){
  pokemon(name: $name) {
    number
    name
  }
}

My GraphQL Variable:

{
	"name": "{{name}}"
}

I just have a Global name variable set as Pikachu and once that request is sent, it’s picking that up and using that variable in the body.

5 Likes

Yes, I’m sorry. The variables DO actually work. It’s just that my current version (7.10.0) marks the variable names in red inside of the “GraphQL Variables” section, and it seemed like the variables were unrecognized. I also had some unnecessary quotes around an integer field, so that was a separate error altogether.

Hi guys, could you help me please with GraphQL variables, I don’t understand my mistake, but the query is not working with variables…

Will be very grateful for your help!!!

Hi @nataliamerkulov! Welcome to our community :wave:

I am just guessing from the error message - is id given ID type in schema?

It seems that you are passing a string but the code is expecting number.
Please let me know if there is any relevant information that you can share :slightly_smiling_face:

Hi @taehoshino, Thank you for your respond! I am new to Postman and GraphQL as well :see_no_evil:
Do I did a mistake when I wrote the variables? Or in the body? In GraphQL it is looking this way:

@taehoshino , I tried without variables and it is working fine…

query{
findDestinationOneCommunity(findOneCommunityOptions: {id: “99247”} )
{_id
id
accommodations{
amenities
commonsAreas

Could you give an advice please what I should to change?

Hey @nataliamerkulov!
Thanks for getting back to me and letting me know that passing the value directly works!

I just realised in the screenshot below you have double-quotes (" ") around 99247.
I think this is the problem - when passed to the variable id in GRAPHQL VARIABLES, {{id}} will be replaced by the global value "99247", which means id is set to ""99247"" instead of "99247".

Please try removing the double-quotes from global variable definition and see if that helps :slightly_smiling_face:

1 Like

Hi @taehoshino , I will try, Thank you!

@taehoshino THANK YOU SO MUCH!!! IT IS WORKING!!! :smiley:

1 Like

That’s awesome! @nataliamerkulov
Happy to help!
Please feel free to post in community or reach out to us if you have any further questions!

This thread is a bit old but it was my first search result when I started trying to figure out why my environment variable would not work in a GraphQL call even when I tried to add it as a GraphQL variable, and I tried to use the examples above as a guide to no avail.
I have set up the tests themselves to run through the collection runner and compare data from a CSV to the result of a GraphQL call, which is working fine, but what I also need to do is be able to change what value I am sending in a test request depending on the environment. Eventually I’ll need to make this dynamic to have tests creating new users, but first, just sending a variable to retrieve data would be very valuable.

When I format it this way

{
   "refID": {{recruitmentID}}
}

I get

{
"errors": [
    {
        "path": [
            null
        ],
        "locations": [],
        "data": null,
        "type": "ServiceError",
        "message": "Must provide Source. Received: undefined"
    }
]

}

but when I format it with double quotes around

{
   "refID": "{{recruitmentID}}"
}

then I get

{
    "errors": [
        {
            "path": [
                null
            ],
            "locations": [],
            "data": null,
            "type": "ServiceError",
            "message": "Variable \"$refID\" is not defined."
        }
    ]
}

At this point it feels like there is just something “stupid” I am missing.
In my environment variables, I have recruitmentID set to a string (that of the user’s recruitment record ID, a mix of letters and numbers) with no quotes around it.

In my request body, I’ve tried various permutations of

    {
        getRecord(ReferenceID: $refID,
        ) {

After looking at more examples, I tried

{
   "ReferenceID": "{{recruitmentID}}"
}

and

 {
        getRecord($ReferenceID: String!
        ) {

and got no closer with

{
    "errors": [
        {
            "path": [
                null
            ],
            "locations": [],
            "data": null,
            "type": "ServiceError",
            "message": "Syntax Error: Expected Name, found $"
        }
    ]
}

It seems like the examples I’m finding for how to use GraphQL variables with environment variables don’t contain the format I have to work with, so my question may boil down to syntax, as the above examples are sending a string, and I’m trying to send a key-value pair.

When I send the request as below, it does work:

  getRecord((ReferenceID: "actual_string here"
    ) {

Does anyone have suggestions on what to try next?

Hey @imageekgirl :wave:
Thanks for posting in the community!

I understand you are trying to pass an environmental variable to GraphQL variable and then to use it in your query, correct?

Please refer to the following example and check if you are defining the GraphQL variable correctly.

I hope this helps!

Thanks for replying! I am trying to pass environment variables to graphql variables for the query since there did not seem to be a way to just make an environment variable work with GraphQL --although if that’s sufficient and it works, I’d call it good!

II hate to admit this, but I’m a little confused by the fact that $name is used twice in the query on the left.

Should I assume that {{name}} under the graphql variables is the environment variable?

My difficulty seems to be the format/syntax of what I’m trying to use, so I’m wondering how to do it or if Postman doesn’t support it. Basically all of my queries are endpoint(ReferenceID: “reference_id_value” ) or very similar, not endpoint (“12345”) as shown above.

That’s correct!

Please let me know if the API endpoint that you are using is publicly available - I am happy to test it out with your specific case :wink:

It’s not publicly available. Do you have any suggestions as to how I can try formatting the variables to make it work? I could post responses without revealing anything confidential.

I just edited this to clarify that when it works, I’m not using variable notation.

Hi there,

I believe you can do something similar to my example above, but if you are still having the issue, can you share the schema and the current query body so that I can understand better? :smiley: