>>>graphql block and variables in a >>>variables block. hitspec handles the rest.
Prerequisites
- hitspec installed (Installation)
- Familiarity with captures and dependencies
The Complete Test File
This example tests the Countries GraphQL API, a free public GraphQL endpoint. Create a file calledgraphql.http:
graphql.http
Step-by-Step Breakdown
1
Write a GraphQL query with variables
The hitspec converts this into the standard GraphQL JSON format:You write the query naturally and hitspec handles the serialization.
>>>graphql block contains the query, and >>>variables provides the variable values as JSON.2
Assert nested response fields
GraphQL responses have a The JSON path syntax supports:
data wrapper. Use dot notation to reach nested fields.- Dot notation:
body.data.country.name - Array indexing:
body.data.country.languages[0].name - Length via gjson:
body.data.country.languages.#
3
Capture values from GraphQL responses
Captures work the same way as with REST responses. Extract values for use in later requests.These values are accessible as
{{getCountry.countryName}} and {{getCountry.continentName}} in subsequent requests.4
Query without variables
For queries with no variables, you can omit the
>>>variables block.5
Use the each operator for collections
The This verifies that every item in the
each operator applies an assertion to every element in an array.countries array is a JSON object.6
Run the GraphQL tests
GraphQL Error Handling
GraphQL APIs return errors in adata.errors array. You can assert on these:
Next Steps
Multipart and GraphQL
Full reference for GraphQL blocks and multipart uploads.
Assertions
All 26 assertion operators available in hitspec.