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.