Monday, December 3, 2018

Adding Graphql Support in Postman

Postman is no doubt one of the best application in order to test the REST Api's. And the features of testing and pre-request scripts are one of the best out there in the market.

But after the success of Graphql, the importance of Postman is going down, since the point of passing the data and changing the URL and all is gone because graphql works in 1 URL setup.

In order to use the Graphql, many vendors came up with the UI/Editor in order to interact with the GraphQl server. Some of the apps are namely:
  1. GraphIql : Which is an electron app and needs to be installed using brew in Mac by command, brew cask install graphiql and you will get a Desktop App that looks something like:



    You can add the URL in the Text box, have the Docs and Pass the Query/Mutation in Left Hand Box and Variables in Left Bottom Box namely, Query Variables and Headers in Top Right Section and call it.
  2. Altair Chrome Extension which can be installed from here
There are many more but these 2 are the most popular. Although I personally use Altair, since it does not install an Application and rather installs a plugin and works like a charm. But some says since we have all the Test cases configured in Postman in order to run with Newman we need the call with Postman. Here are the steps to get run a simple Graphql query in Postman.

  1. Open postman
  2. Pass the URL in the URL text box.
  3. Make the method as POST
  4. Go to body section and just write {{graphqlquery}}
    1. This is reading of the Environment Variable named graphqlquery that we create in the further section.
  5. Go to Pre-Request Script section and here is the catch, add the code in the following manner:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    //Add the query/mutation here, this is dummy query
    let query = `
        query test{  
            areas{ 
                uuid
            }
        }
    `;
    //Add the Variables here
    let variables = {};
    
    postman.setEnvironmentVariable("graphqlquery", JSON.stringify({query, variables, operationName: ''}));
    

  6. If you see at line no: 12 we are setting the same variable (graphqlquery) that we are using in 4.1
  7. You can pass the variables (if required) in line 10. 
  8. After all the things are done, you can Send and it will return the JSON.
Here are the attached snapshots for the same:





Happy Coding.
:)

No comments:

Post a Comment