Use Environment variables inside of GraphQL variables

This is the structure of the query (when it works); there are other fields one can query for beyond name and email, but my problem is getting the ID I’m querying on into the variables correctly:

query {
   
  SERVICENAME {
   getRecord(ReferenceID: "actual_string_value_in_quotes",
    ) {
     
        LastName
        FirstName
        Email    
                        }
                       }
            }

When you click on query (in our schema docs), you select a service (in this case, shown as SERVICENAME), and then the type implements queries on different fields, including the getRecord by the ID shown above.

Does that make it more clear?

Hi there,

Maybe something like below might work?

query ($recruitmentID: String) {
   
  SERVICENAME {
   getRecord(ReferenceID: $recruitmentID,
    ) {
     
        LastName
        FirstName
        Email    
                

You can define recruitmentID as:

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

where {{recruitmentID}} is an environmental variable.

I tried the above and got the error message below; without an exclamation point after String, the error just listed the same with “String” instead.

` "message": "Variable \"$recruitmentID\" of type \"String!\" used in position expecting type \"ID!\"."`

Based on this error message, I changed String to ID and to ID! to no avail.

Using ID! resulted in

  "SERVICENAME": {
            "getRecord": null
        }

while using ID! as the first error message seemed to imply I should resulted in the error message of

"message": "Variable \"$recruitmentID\" of type \"ID\" used in position expecting type \"ID!\".

It seems like setting the type of ID! at least results in my objective being understood since it returns a null as opposed to an error, so it seems that has gotten us a little closer.

I wondered if having the two variables (environment and graphql) the same value was causing problems, but editing that did not change the null either.

It looks like what was suggested is what was shown in this video. Sending GraphQL Queries in Postman - YouTube as well as the one from the Postman team.

Based on their docs, it looks like the right approach? https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/
I’m not sure what else I need to do to get a logical result.

My last attempt (a sanity check of sorts) was to replace the Postman variable in the GraphQL variable with the actual value of the ID that worked.

Using that, my query returned the expected results.

So it seems like the format of my query is not the problem at this point, but in trying to use the environment variable saved as a GraphQL variable, something goes awry.

Hey @imageekgirl! Thanks for getting back :wave:

I can see you are no longer seeing an error but getting “null” back - this implies that the value passed to $recruitmentID at least satisfies the schema but there is no matching result.

Can you share a screenshot showing the environment - I wonder if the variable is set correctly :slightly_smiling_face:

I think your question above led me to what was wrong with it.


In the example above of the environment variable itself, you can see there is an initial value and a current value.
The same is true for the auth token; I just update the current value each day or so when working on this (and that is why it’s a tiny screenshot, as I didn’t want to show that).
The recruitmentID had an initial value, but it did not persist to the current value column as you can see the environment variable does in this screenshot.
It looks like when I copy and paste the ID so it’s in both the initial and current value fields for the {{recruitmentID}}, it works. The variables are where you suggested, and I’m getting the expected response body.
Yes, I’m totally :woman_facepalming:t2: here . . .

That’s awesome! Glad to hear the issue was resolved :tada:

THANK YOU a million for helping me with this! :smiley:

1 Like