GraphQL API Testing with Postman on your CI

On previous post, we did an exploration of tests in Postman environment to a public Rest API. Now it’s time for the famous middleware GraphQL. For those that are not familiar, in GraphQL we have 1 endpoint and we write queries to get the data that we want. Since https://gorest.co.in/ has such an public API that support GraphQL, we will do the exact same CRUD operations with the Rest API tests to find the differences.

Get a token

Since we are using the same public API, if you don’t have already a token from the previous post, you need to create a token for the Authorization.

Set the token as an environment variable

You need to set the token on Environments tab for reusability. If you already did that with the Rest API tests of previous post, you don’t have to do something. If token is already placed, you just need to add it to Authorization tab of the request.

Write your first test

On GraphQL is a little bit tricky with response statuses and its not straight forward what is happening so we need to do an assertion of response status AND response body properties. On our first test we have Get Users query:

On cool feature of Postman is that fetching the GraphQL schema and has autocomplete when you write the query. This is very helpful when exploring what response you need to get back. So we do a POST to graphql endpoint, our query describes exactly what we expect to get from the server and we can choose what properties we need.

After sending this request and we will get a response, we will see that we will get an array of nodes. Those nodes are all the users and are exactly the same with the Get Users Rest API endpoint. Now its time for the test:

First we need to assert that response that is 200. After we extract the response as Json and assert that nodes array have at least 1 length ( Those are Chai assertion syntax ). What that means practically? That we don’t get a 200 status with a response with error code and a message. We assert that we get the right structure and it’s at least 1.

Let’s create a user with random email

Now it’s time for a mutation. If you want to create, update or delete something you need do a mutation in GraphQL. To this mutation you need to pass the parameters with the type and then write the GraphQL variables as json:

After the mutation, we write also the query again with the properties that we want to get. Let’s proceed to test:

We extract again the id to use it as an environment variable for the other tests. We assert the status code and then we assert the properties of the created object.

Get user details of the created user

As we need to get the information of a user and pass as param the id we need to write the following query:

Note that the type of id param is ID, it’s not a simple string like the mutations params. if you try to pass a string you will get mismatch error

And now we car write the tests for status code and the expected response of the created user’s values:

Update a property of existing user

As we want to update a property e.g the name from John Pourdanis to John Pourdanopoulos ,we need to write the following mutation:

We need to pass to mutation the id and the name change. As you see now the id has Int type and we need to pass an integer value.

And we update a specific field of the model, we need to assert it ( except the status code 200 ) .

Delete the user that you created

Last but not least, we need to pass as GraphQL variable the id of the user that we want to delete and do the mutation:

After running the test once, to the next request you should get ‘Resource not found!’ error. The test should be like this:

As I mentioned, those tests are coupled cause one test depend to the result of the other. This means that if a test fail to the middle of the execution , will make other tests fail too. Its test should create the data that needs and then clean up itself. Now we do it for demonstration purposes.

Also note that, test execution order should be specific , first create the user, get the id, update and then delete the user. Again, this is not the best practice for your tests suites. Tests should be autonomous.

Export and run it to CI

Same as you did before, on three dots button you should see an option called export. Export the entire collection and if you open it its a huge json (Please choose collection v2.1). Save it as gorest-graphql.json and push it on empty Github repo. Now you should write the Github action for it:

//.github/workflows/main.yml

name: Newman Run

on:
  pull_request:
    branches:
    - master

jobs:
  newman:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@master
      - uses: matt-ball/newman-action@master
        with:
        collection: gorest-graphql.json
        envVar: '[{ "key": "bearerToken", "value": ${{ secrets.gorestBearerTokenValue }} }]'

You need to set the correct value for bearer Token to Github repo secret , commit and push! If all goes well you should see a successful pipeline run.
Happy Testing!

Do you want to do an exploration on this example together?

You can book some time with me to discuss your current situation and do exploratory testing of the sample of this post or any other kind of issue you met regarding Software Testing & Quality Engineering.