Testing a GraphQL API using Postman
You must assess the GraphQL API created is working as expected before exposing it to the API consumers. You should verify the correct status code of the response. For instance, successful HTTP requests should return 200 OK, creating a resource should return 201 CREATED, and so on.
Let’s test the Starwars GraphQL API, which was imported using a file. In this example, to test whether the Star Wars API is created successfully, let’s retrieve all the films in the Star Wars franchise using the Starwars API. The API response should list all the films in the Star Wars franchise.
Before you begin
Ensure that you have:
Postman installed in your system, or you can you use the web version of Postman.
A sample Star Wars Postman collection. For example, GraphQL.postman_collection.json.
1. Open Postman.
2. Click Import.
3. Select GraphQL and select the GraphQL.postman_collection.json file from your local folder.
4. Click Import.
5. Open the imported GraphQL API collections and select Starwars API GW.
6. Select a method. For example, select the method as POST.
7. Specify the request URL. For example, provide the Star Wars endpoint from the API you created.
8. Set the request body type as GraphQL
9. Pass the query in the QUERY section. For example, write a query to fetch a list of all the films in the Star Wars franchise.
10. Click Send.
The GraphQL API is invoked successfully and returns the status code as 200 OK HTTP. The response lists all the films in the Star Wars franchise.
The API is tested and working as expected. You can now expose the API to the consumers.