Before testing a graphQL API , let’s see how a typical graphQL request look like

Using GraphQL, the client can specify exactly the data it needs in a query. Notice that the structure of the server’s response follows precisely the nested structure defined in the query.

Now, let’s see how to can automate this request using Karate.First the getQuery in graphQL. We will separate the GraphQL variables and the Feature file of the request as below:

undefined

A graphQL variable request looks like below file, save this file with name ‘queryEmployers.graphql’

{ 
getAllAWSEmployerLocation(count:1){ 
  id   
    locationName   
     locationAddress   
      employerLocationGoalValues{
     id     
           goalValue     
       employerGoalType { 
      id       
              goalType     
       }   
    } 
}

The corresponding feature file to read this request, save this file with name getEmployer.feature

Feature: GraphQL request query variable

Background: 

 * url 'AuthURL'
 * path 'token'
 * form field grant_type = 'password'
 * form field username = username
 * form field password = password
 * form field client_id = appId
 * form field client_secret = appSecret
 * form field scope = 'yourScopeURL'
 * method post
 * status 200
 * print response

  Scenario: get all employer details
    * url 'AppURL'
    Given def query = read('queryEmployers.graphql')
    #And def variables = { name: 'Charmander' }
    And request { query: '#(query)' } 
    And retry until response.id > 3
    When method get
    Then status 200
    When method post
    * print response
    Then status 200