Execute Multiple Queries in Sequence
Sequence API and database queries, return a single result with the @sequence custom GraphQL directive.
@sequence is a custom StepZen directive that executes multiple queries in a sequence to return a single result. Each step in the sequence passes its data as arguments to
the next step. This enables complex sequencing of API and database calls to populate a query, without the need to implement difficult asynchronous call handling.
This topic provides the following information about @sequence:
- Configuration Properties: How to configure the
stepsthat make up a sequence. - Collecting Results: How to use data from prior
steps. - Authorization Headers in the GraphQL Schema: How to pass GraphQL arguments in HTTP request headers.
Configuration Properties
steps
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 through. 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).
The steps are executed in the order they are listed (from top to bottom). For more details on how to override the steps to include data from prior steps, see the Collecting Results section.
In some cases, the name of a property returned by a query does not match the argument name for the subsequent query.
For example, if the preceding step1 query returns lastName, but the step2 query expects an argument of surname, 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. To have the full sequence return both name and city,
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 will 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.