7 Contract testing for GraphQL

 

This chapter covers

  • A brief introduction to GraphQL
  • GraphQL application overview
  • Consumer contract tests between a consumer and GraphQL layer
  • Provider contract tests between a consumer and GraphQL layer

In previous chapters, you’ve seen how to write contract tests for web and mobile consumers, but the underlying data provider has always been a representational state transfer (REST) API, which is quite common. However, you might work on a project involving GraphQL instead of REST. While REST APIs have been seen as the de facto standard for companies providing APIs, many companies are switching to GraphQL as an alternative. However, REST APIs and GraphQL can also work together, so GraphQL shouldn’t be seen as a competitor or a complete replacement of REST. A common approach the authors have seen in work projects is using GraphQL as a middle layer between consumers and various backend data services using REST or other APIs such as gRPC or Simple Object Access Protocol (SOAP).

In this chapter, we provide a brief introduction to GraphQL and explain how to introduce contract testing to GraphQL applications, covering both the consumer and provider tests. As in previous chapters, we’ll be using the sample movies application.

7.1 A brief introduction to GraphQL

7.2 GraphQL application overview

7.2.1 GraphQL server overview

7.2.2 Running the GraphQL server

7.2.3 Consumer overview

7.3 Consumer contract tests between a consumer and GraphQL layer

7.3.1 Importing the required dependencies

7.3.2 Setting up the mock provider

7.3.3 Registering the consumer expectation

7.3.4 Verifying the consumer test and generating the contract

7.3.5 Publishing the contract to Pact Broker

7.4 Provider contract tests between a consumer and GraphQL layer

7.5 Chapter exercise

Summary