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 {idusernamephonefirstNamelastName}}}
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 {idusernamephonefirstNamelastName}}}
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 thisConclusion
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 .