logo

NJP

GraphQL Variables - Learn Integrations on the Now Platform

Import · Dec 04, 2023 · video

[Music] in this video we'll look at how to make our graphql queries more flexible and reusable using variables we'll also show how to construct an HTTP request body from a third-party application to send the query and variables when we run this query to get our case with the hard-coded S ID as a parameter we see that it works and we get back this output to enable this query to receive parameters we must use the query keyword it's no longer optional following that we'll add the function name of the query that's expecting the variable followed by parentheses containing the variable Name colon and type in this case it's dollar sign my ID we recognize the variable because it starts with a dollar sign if multiple variables are being passed they're comma separated inside the query we simply cut the hard coded CIS ID and replace it with dollar sign my ID now down in the lower left of the graphql Explorer we'll click variables and open this panel here we enter the Json name value pairs for our variables for this example we have my ID and the CIS ID we just copied to the buffer although it's rather anticlimactic when we run the query it shows the same result as before proving our new variable driven query works exactly the same the advantage now is in instead of saving a new query or constructing the query at runtime using Code we can simply replace the variable value and get a different record Now One technical note so far we've been using the graphql Explorer to send our request from the client to the server and getting a result back but what exactly is happening in an earlier video we learned that graphql is an HTTP based protocol the client is sending an http post with the query as the request payload the payload has a property named query whose value is the content we've been using in the graphql Explorer for example our last query would look something like this if tabs and Extra Spaces and new lines were removed in addition variables are passed in as a variables property of that object however because the variable object itself includes quotes those need to be escaped to avoid syntax errors so the variables property has these extra backslashes before the inner quotes we'll see more of this later in the video graphql in action that's it we've seen how variables are passed in a client query making our queries much more flexible and reusable and I hope you'll join me in the next video for more ways to make your queries easier to build and maintain

View original source

https://www.youtube.com/watch?v=f-NGjA1GR74