Execute Multiple Queries in Sequence

Sequence a series of queries into a single result

The @sequence directive is a custom StepZen directive that allows you to execute multiple queries in a sequence that returns a single result. Each step in the sequence passes data to the next, allowing data from step 1 in a sequence to be used as arguments to step 2 in a sequence and so on. This allows for complex sequencing of API and database calls to populate a query without the need to write difficult asynchronous call handling.

Configuration Properties

steps (required)

The only required argument to create a sequence is the steps that make up that sequence. This is an array of objects that the query steps. Each object in the array must contain at a minimum the name of the query that the step will call.

@sequence(
  steps: 
    [
      {query: "step1"}
      {query: "step2"}
    ]
)

The value of query must be a query that is defined on the schema. The result of the sequence will be the same as the result of the last step, step2 in this case. For more details on how to override that to include data from prior steps, see the "Collecting Results" section below.

In some cases, the name of a property returned by a query does not match the argument name for the subsequent query. For example, let's imagine the step1 query above returns lastName but the step2 query expects an argument of surname. In these scenarios, you can use arguments to map the results to the expected name.

@sequence(
  steps: 
    [
      {query: "step1"}
      {query: "step2", arguments: [{name: "surname", field: "lastName"}]}
    ]
)

Collecting Results

By default, the result of the final step is the result of the sequence. However, in some scenarios, you may need data from prior steps as part of the result of the entire sequence.

For example, imagine step1 returns user information including their name but step2 returns location information including city. If you wanted the full sequence to return both name and city, you can use an extra step in the sequence that calls a query utilizing a special echo connector.

The sequence would look like:

@sequence(
  steps: 
    [
      {query: "step1"}
      {query: "step2"}
      {query: "collect"}
    ]
)

The collect query in step 3 of the sequence would then use the echo connector:

collect (name: String!, city: String!): UserWithLocation
    @connector (type: "echo")

For a more detailed walkthrough of how @sequence can be used, follow our sequencing queries tutorial.

This site uses cookies: By using this website, you consent to our use of cookies in accordance with our Website Terms of Use and Cookie Policy.