DEV Community

Cover image for Trace-Based Tests with GraphQL in Action!
Adnan Rahić for Kubeshop

Posted on • Edited on • Originally published at tracetest.io

Trace-Based Tests with GraphQL in Action!

Trace-based Tests with GraphQL in Action 💣

Join us for the GraphQL and Playwright test trigger webinar on September 11th!

GraphQL 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.

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!
Or, check out the guided example to run it in your own environment.

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.

You have two options: upload your own schema or use schema introspection. With the latter, Tracetest automatically fetches the schema from your API URL.

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.

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.

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

https://demo-pokeshop.tracetest.io/graphql
Enter fullscreen mode Exit fullscreen mode

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

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

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
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

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:

- 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}}}'
Enter fullscreen mode Exit fullscreen mode

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.

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.

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

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 it. 🪄

GraphQL API Trigger with the Playwright Engine and Test Observability

With Tracetest, you can use the new GraphQL trigger alongside any other feature. Whether you want Tests as part of a Test Suite, plan to use them with the Synthetic Monitor Framework, or aim to include them in GitHub Actions—it's all possible.

A game-changing feature would be combining tests using both the GraphQL trigger and Playwright engine trigger into a Run Group. Then, add these to a Synthetic Monitor 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 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 and try it out by signing up today!

Also, please feel free to join our Slack community, give Tracetest a star on GitHub, or schedule a time to chat 1:1.

Top comments (0)