Responsive

Trace-Based Tests with GraphQL in Action!

Trace-Based Tests with GraphQL in Action!
Sep 5, 2024
4 min
read
Adnan Rahic
Staff Developer Advocate
Tracetest

Check out our new GraphQL trigger, enabling trace-based testing for GraphQL APIs. Create tests using schema introspection, automate workflows, and validate database interactions with real-time tracing

Share on X
Share on LinkedIn
Share on Reddit
Share on HackerNews
Copy URL

Table of Contents

Get Started with Observability-Driven Development

Get Started with Observability-Driven Development

> [*Join us for the GraphQL and Playwright test trigger webinar on September 11th!*](https://www.linkedin.com/events/7237397975470112768/comments/)

>

[GraphQL](https://graphql.org/) is a query language for APIs and a runtime for fulfilling those queries with your existing data. It offers a clear, comprehensive description of your API's data, allowing clients to request exactly what they need—nothing more, nothing less. This approach simplifies API evolution and enables powerful developer tools.

Wouldn't it be great if Tracetest had a GraphQL trigger for testing GraphQL APIs?

Our community certainly thinks so. In fact, three community members have requested this feature in recent months.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725450638/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/Screenshot_2024-09-04_at_13.49.44_qgkzmv.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725450638/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/Screenshot_2024-09-04_at_13.49.44_qgkzmv.png)

## Introducing the Tracetest GraphQL Trigger 🔥

Great news! The team has just released a brand-new GraphQL trigger type.

> **[Join our demo environment to try it yourself!](https://app.tracetest.io/organizations/ttorg_2179a9cd8ba8dfa5/invites/CB01dP6IR/accept)**

> **[Or, check out the guided example to run it in your own environment.](https://docs.tracetest.io/examples-tutorials/recipes/running-tests-with-tracetest-graphql-pokeshop)**

You can now effortlessly test GraphQL APIs using OpenTelemetry and trace-based testing. This feature is a natural addition to your existing HTTP and gRPC API tests.

This update enables you to run tests using GraphQL queries, expanding your API testing capabilities.


![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451528/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_ph7zhk.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451528/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_ph7zhk.png)

You have two options: upload your own schema or use schema introspection. With the latter, Tracetest automatically fetches the schema from your API URL.
![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451653/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_1_dxy8c4.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451653/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_1_dxy8c4.png)
From here, you can create test specs just as you would with other triggers. To streamline the creation and execution process and take advantage of any supported integrations. Navigate to the Automate tab to download or copy the definition file and the trigger script.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451945/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_2_mpco4o.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725451945/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_1_trigger_triggerTab_body_2_mpco4o.png)

## Testing GraphQL APIs

Running trace-based tests against GraphQL APIs has never been simpler. With this new trigger you can finally get the same test coverage for your GraphQL APIs as for your existing HTTP and gRPC APIs.

There are 4 steps to create and run a GraphQL trigger test:

1. Create a test with the GraphQL trigger 🕵️
2. Add your GraphQL API endpoint 🔗

3. Use the GraphQL Schema Introspection 🔍

4. Add a GraphQL query ❓

5. Hit run 🏃

6. Add test specs 🧪

7. Profit 🤑

Let me walk you through it all step-by-step.

To run a test with the GraphQL trigger, click the GraphQL trigger type when creating a new test.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725452405/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_tests_page_1_cgdeys.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725452405/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_tests_page_1_cgdeys.png)

Add your GraphQL API endpoint. Let’s use the Pokeshop demo as a sample.

```
https://demo-pokeshop.tracetest.io/graphql
```

Click the `Use GraphQL Introspection` button on the `Schema` tab to load the GraphQL schema.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725452589/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_tests_page_1_1_n2lhlm.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725452589/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_tests_page_1_1_n2lhlm.png)

Go back to the `Query` tab. Let’s add a mutation with two fields.

```graphql

mutation create {
 createPokemon(name: "Charizard", type: "flying", isFeatured: false, imageUrl: "https://img.pokemondb.net/artwork/large/charizard.jpg") {
   id
   name
   isFeatured
   type
   imageUrl
 }
 importPokemon(id: 38) {
   id
 }
}
```

This will trigger the test and show you a trace response to start building test specs. The kicker here is that the two fields in the mutation will show two flows in the trace response.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531147/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_9_test_selectedSpan_af7c8389b85460c7_txlhe1.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531147/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_9_test_selectedSpan_af7c8389b85460c7_txlhe1.png)

You can see both the `create` and `import` flow in the trace.

To validate the GraphQL API, you can create a test spec like this:

```yaml
- selector: span[tracetest.span.type="http" name="POST /graphql" http.method="POST"]
 name: Validate route is "/graphql" and data is valid.
 assertions:
   - attr:http.route   =   "/graphql"
   - attr:http.response.body =
     '{"data":{"createPokemon
{"id":224318,"name":"Charizard","isFeatured":false,"type":"flying","imageUrl":"https://img.pokemondb.net/artwork/large/charizard.jpg"},"importPokemon":{"id":38}}}'
```

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531380/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_9_test_selectedAssertion_0_selectedSpan_af7c8389b85460c7_owussk.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531380/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_9_test_selectedAssertion_0_selectedSpan_af7c8389b85460c7_owussk.png)

You can also create specific test specs for the async flows in the Pokeshop demo like validating the RabbitMQ queue and the async API triggered behind the RabbitMQ queue.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725453670/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_4_test_selectedSpan_a4d91c60322547c8_anuze4.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725453670/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_4_test_selectedSpan_a4d91c60322547c8_anuze4.png)

But, what’s more exciting is that you can validate database interactions. In this sample you can validate both the `import` and `create` database calls in the same test. Here’s the validation for the `create` flow.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531708/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_10_test_selectedAssertion_3_selectedSpan_715302ccdb9e6025_fvgxi5.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531708/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_10_test_selectedAssertion_3_selectedSpan_715302ccdb9e6025_fvgxi5.png)

And, by selecting the database span below the RabbitMQ queue, you can validate the `import` flow as well.

![https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531832/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_10_test_selectedAssertion_3_selectedSpan_715302ccdb9e6025_1_wzjapo.png](https://res.cloudinary.com/djwdcmwdz/image/upload/v1725531832/Blogposts/Trace-based-Tests-with-GraphQL-in-Action/app.tracetest.io_organizations_ttorg_2179a9cd8ba8dfa5_environments_ttenv_231b49e808c29e6a_test_H_Xijq3IR_run_10_test_selectedAssertion_3_selectedSpan_715302ccdb9e6025_1_wzjapo.png)

Just like that, in a few minutes, you’ve created a GraphQL API test that validates two flows with multiple database interactions, async APIs and a message queue. If that isn’t magic, I don’t know what is. 🪄

## GraphQL API Trigger with the Playwright Engine and Test Observability

With [Tracetest](https://app.tracetest.io/), you can use the new GraphQL trigger alongside any other feature. Whether you want [Tests](https://docs.tracetest.io/concepts/tests) as part of a [Test Suite](https://docs.tracetest.io/concepts/test-suites), plan to use them with the [Synthetic Monitor Framework](https://tracetest.io/blog/tracetest-monitors-trace-based-testing-meets-synthetic-monitoring), or aim to include them in [GitHub Actions](https://docs.tracetest.io/ci-cd-automation/github-actions-pipeline-with-secrets)—it's all possible.

A game-changing feature would be combining tests using both the GraphQL trigger and [Playwright engine trigger](https://docs.tracetest.io/examples-tutorials/recipes/running-tests-with-tracetest-playwright-engine) into a [Run Group](https://docs.tracetest.io/concepts/run-groups). Then, add these to a [Synthetic Monitor](https://docs.tracetest.io/concepts/monitors) and run them on a schedule to continuously validate your applications in real time!

## Try This in Tracetest Yourself!

Check out the [GraphQL trigger documentation](https://docs.tracetest.io/examples-tutorials/recipes/running-tests-with-tracetest-graphql-pokeshop) to get started with your own tests. We're eager to hear your thoughts on this first version of the GraphQL Trigger!

Last, but not least, do you want to learn more about Tracetest and what it brings to the table? Check the [docs](https://docs.tracetest.io) and try it out by [signing up](https://app.tracetest.io/) today!

Also, please feel free to join our [Slack community](https://dub.sh/tracetest-community), give [Tracetest a star on GitHub](https://github.com/kubeshop/tracetest), or schedule a [time to chat 1:1](https://calendly.com/ken-kubeshop/tracetest-walkthrough).

Stay in the Loop

Join the Tracetest Community in one of these channels: