|
| 1 | +# Execute |
| 2 | + |
| 3 | +The execute feature wires together parsing, validation, and execution so you can run FunQL requests end-to-end with a |
| 4 | +single call. It builds on the execution pipeline, ensuring that all steps are performed in the correct order and in the |
| 5 | +right way, reducing complexity for developers. |
| 6 | + |
| 7 | +This page explains how to enable the execute feature, how to run requests, and how to customize the pipeline. |
| 8 | + |
| 9 | +## Adding the feature |
| 10 | + |
| 11 | +Use `AddExecuteFeature()` to register the services required to execute FunQL requests: |
| 12 | + |
| 13 | +```csharp |
| 14 | +public sealed class ApiSchema : Schema |
| 15 | +{ |
| 16 | + protected override void OnInitializeSchema(ISchemaConfigBuilder schema) |
| 17 | + { |
| 18 | + schema.AddExecuteFeature(); |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +This sets up the execute feature with default configurations and the core execution handlers, like handlers for parsing |
| 24 | +and validating. See [Advanced configuration](#advanced-configuration) on how to customize the feature. |
| 25 | + |
| 26 | +## Executing requests |
| 27 | + |
| 28 | +To avoid duplication, the examples for executing FunQL requests (both REST-style parameters and full FunQL queries) are |
| 29 | +documented in the _Executing queries_ guide: |
| 30 | + |
| 31 | +- [Learn how to execute requests →](../../executing-queries/index.md) |
| 32 | + |
| 33 | +## Advanced configuration |
| 34 | + |
| 35 | +The `AddExecuteFeature()` method has two optional arguments: |
| 36 | + |
| 37 | +- `action`: An action to customize the feature. |
| 38 | +- `withCoreExecutionHandlers`: Whether to add all core execution handlers. Default `true`. |
| 39 | + |
| 40 | +For example, add your own execution handler: |
| 41 | + |
| 42 | +```csharp |
| 43 | +public sealed class ApiSchema : Schema |
| 44 | +{ |
| 45 | + protected override void OnInitializeSchema(ISchemaConfigBuilder schema) |
| 46 | + { |
| 47 | + schema.AddExecuteFeature(config => { |
| 48 | + // Customize the feature here |
| 49 | + config.WithExecutionHandler("MyCustomHandler", new MyCustomHandler(), 0); |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Core execution handlers |
| 56 | + |
| 57 | +With `withCoreExecutionHandlers` set to `true` (default), the execute feature adds the following core execution |
| 58 | +handlers: |
| 59 | + |
| 60 | +- **`ParseRequestExecutionHandler`**: Parses a full FunQL request string (e.g., `listSets(skip(1))`) into a `Request` |
| 61 | + AST for further processing. This is designed for full FunQL queries, treating FunQL as a query language (_QL_). |
| 62 | +- **`ParseRequestForParametersExecutionHandler`**: Builds a `Request` from given parameters (e.g., `filter`, `sort`), |
| 63 | + parsing each parameter separately. This is designed for _REST_ APIs where the FunQL parameters are extracted from the |
| 64 | + URL, like `../sets?filter=gte(price, 500)&sort=desc(price)`. |
| 65 | +- **`ValidateRequestExecutionHandler`**: Validates the parsed `Request` against the schema. |
| 66 | + |
| 67 | +!!! tip |
| 68 | + |
| 69 | + Add the [LINQ feature](linq.md) **after** the execute feature to automatically register the LINQ execution handlers. |
| 70 | + These handlers translate the `Request` into LINQ and apply filter/sort/limit/skip to the target `IQueryable`. |
| 71 | + |
| 72 | +### Custom execution handlers |
| 73 | + |
| 74 | +You can also add your own execution handlers to the pipeline. For example, a handler that measures the time it takes to |
| 75 | +execute the request: |
| 76 | + |
| 77 | +```csharp |
| 78 | +public sealed class TimingExecutionHandler : IExecutionHandler |
| 79 | +{ |
| 80 | + public async Task Execute(IExecutorState state, ExecutorDelegate next, CancellationToken cancellationToken) |
| 81 | + { |
| 82 | + var stopwatch = Stopwatch.StartNew(); |
| 83 | + try |
| 84 | + { |
| 85 | + await next(state, cancellationToken); |
| 86 | + } |
| 87 | + finally |
| 88 | + { |
| 89 | + stopwatch.Stop(); |
| 90 | + Console.WriteLine($"Request executed in {stopwatch.ElapsedMilliseconds} ms"); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +Then register the handler when adding the execute feature: |
| 97 | + |
| 98 | +```csharp |
| 99 | +public sealed class ApiSchema : Schema |
| 100 | +{ |
| 101 | + protected override void OnInitializeSchema(ISchemaConfigBuilder schema) |
| 102 | + { |
| 103 | + schema.AddExecuteFeature(config => { |
| 104 | + // Register the handler here |
| 105 | + config.WithExecutionHandler( |
| 106 | + // Name of the handler, used to identify it in the pipeline |
| 107 | + name: "TimingExecutionHandler", |
| 108 | + new TimingExecutionHandler(), |
| 109 | + // Lower order means earlier in the pipeline, so pick a very |
| 110 | + // low value to measure the whole pipeline |
| 111 | + order: int.MinValue |
| 112 | + ); |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +Now whenever a request is executed, the `TimingExecutionHandler` will log how long it takes to execute the request. |
| 119 | + |
| 120 | +**Dynamic context:** |
| 121 | + |
| 122 | +For handlers that require dynamic context or need to share data with other handlers, use `IExecutorState` to enter an |
| 123 | +`IExecuteContext`. This stores data that handlers later in the pipeline can then use. |
| 124 | + |
| 125 | +As an example, we update the `TimingExecutionHandler` to enter a `TimingContext` with a shared `Stopwatch` for a |
| 126 | +different handler to use: |
| 127 | + |
| 128 | +```csharp |
| 129 | +public sealed record TimingContext(Stopwatch Stopwatch) : IExecuteContext; |
| 130 | + |
| 131 | +public sealed class TimingExecutionHandler : IExecutionHandler |
| 132 | +{ |
| 133 | + public async Task Execute(IExecutorState state, ExecutorDelegate next, CancellationToken cancellationToken) |
| 134 | + { |
| 135 | + var stopwatch = Stopwatch.StartNew(); |
| 136 | + // Enter the context before calling the next handler |
| 137 | + state.EnterContext(new TimingContext(stopwatch)); |
| 138 | + |
| 139 | + await next(state, cancellationToken); |
| 140 | + |
| 141 | + // Exit the context to clean up the data |
| 142 | + state.ExitContext(); |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +Now create a handler that uses the `TimingContext` to log the elapsed time: |
| 148 | + |
| 149 | +```csharp |
| 150 | +public sealed class LogTimingExecutionHandler : IExecutionHandler |
| 151 | +{ |
| 152 | + public Task Execute(IExecutorState state, ExecutorDelegate next, CancellationToken cancellationToken) |
| 153 | + { |
| 154 | + // Find the context and log the elapsed time |
| 155 | + var context = state.FindContext<TimingContext>(); |
| 156 | + if (context != null) { |
| 157 | + Console.WriteLine($"Time elapsed is {context.stopwatch.ElapsedMilliseconds} ms"); |
| 158 | + } // else: Context was not entered |
| 159 | + |
| 160 | + return next(state, cancellationToken); |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +The `LogTimingExecutionHandler` logs the elapsed time only when a `TimingContext` has been entered. |
| 166 | + |
| 167 | +## What's next |
| 168 | + |
| 169 | +With the execution feature added, it's time to use it: |
| 170 | + |
| 171 | +- [Learn more about executing queries →](../../executing-queries/index.md) |
| 172 | +- [Learn more about the LINQ feature →](linq.md) |
0 commit comments