Learning LogoLearning Logo
Copy article linkShare Article on TwitterShare article on LinkedInShare article on FacebookShare article on Pinterest

GraphQL quick tip: How to pass variables in GraphiQL

David Mráz
David Mráz@davidm_ai
Author's Linkedin accountAuthor's Instagram accountAuthor's Twitter accountAuthor's Twitter account
Development

Introduction

In the previous article input object type as an argument for GraphQL mutations, we used GraphiQL for executing queries and mutations. We mentioned the following different forms of passing an argument into a mutation

  • Using inline arguments
  • Using variables

Now let’s discuss how we can perform query executions using inline arguments and variables in GraphiQL as we did not mention that in previous articles. First just clone this repository and switch to the corresponding branch to test our example mutation

git clone git@github.com:atherosai/graphql-gateway-apollo-express.git

The process of designing mutations is described in the article on GraphQL mutations. We will use the same example of a mutation, nevertheless, in this quick blog post we will focus only on our options for executing mutations and queries in GraphiQL.

Inline arguments in GraphiQL

First we can use inline arguments. The mutation execution string is then written as follows and there are no other requirements for executing mutation written in this format

mutation createUser {
createUser(input: {
username: "test",
email: "test@test.cz",
phone: "479332973",
firstName: "David",
lastName: "Test"
}) {
user {
id
username
email
phone
firstName
lastName
}
}
}

And the set-up of executing such mutation can look like this:

Variables in GraphiQL

With the variables is a little bit more complex we have to split query string into two separate parts. Our selection set includes id, username, email, phone, firstName and lastName like in the query string with inline arguments

mutation createUser($input: CreateUserInput!) {
createUser(input: $input) {
user {
id
username
email
phone
firstName
lastName
}
}
}

If we would like to use the variables in GraphiQL just click on the QUERY VARIABLES panel at the bottom of your screen and pass the following code

{
"input": {
"username": "test",
"email": "test@test.cz",
"phone": "479332973",
"firstName": "David",
"lastName": "Test"
}
}

Variables should be written using the JSON format. Therefore if you add for example a comma at the end of the last attribute, the GraphiQL invalidates the object with the variables.

The overview of your set-up can be viewed like this

Conclusion

I use passing variables using GraphQL playground all the time. We can easily copy and paste the code to our *.graphql files and instantly know that we are building them correctly. The only problem to think about is that we have to debug our input variable to be in a format which can be validated by our GraphQL schema.

Did you like this post? The repository with the examples and project set-up can be cloned from this branch. Feel free to send any questions about the topic to david@atheros.ai .

Atheros Learning Logo

Atheros Newsletter

Subscribe to our newsletter, and don't miss a beat!

message

* Signing up for Atheros newsletter indicates you agree with

Terms and Conditions and Privacy Policy including our Cookie Policy.

Ready to take next step?

Unlock your potential and master the art of development and design by joining our Classes today.

Don't miss out on the opportunity to enhance your skills and create a bright future in the digital world like thousands of others.