Skip to content

Commit

Permalink
add optional generate field to input_schema (#198)
Browse files Browse the repository at this point in the history
Introduces a new 'transform' function field to query input schemas, enabling dynamic transformation of query inputs before execution. This allows for flexible input modification, such as automatically appending file extensions or other runtime transformations.
  • Loading branch information
shekharnwagh authored Nov 21, 2024
1 parent 07b2146 commit 46daa9b
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 2 deletions.
4 changes: 4 additions & 0 deletions inc/Config/QueryContext/HttpQueryContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class HttpQueryContext implements QueryContextInterface, HttpQueryContextInterfa
'type' => 'array',
'required' => false,
],
'transform' => [
'type' => 'function',
'required' => false,
],
],
],
],
Expand Down
39 changes: 37 additions & 2 deletions inc/Editor/DataBinding/BlockBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,45 @@ private static function apply_query_input_overrides( array $query_input, array $
return array_merge( $query_input, $overrides );
}

public static function execute_query( array $block_context, string $operation_name ): array|null {
/**
* Transform the query input for a block binding before executing the query if
* a transform function is provided. This allows the query input to be
* transformed in some way before the query is executed. This runs after the
* query input overrides have been applied.
*/
private static function transform_query_input(
array $query_input,
object $query_config
): array {
$transformed_query_input = [];

foreach ( $query_config->input_schema as $query_input_key => $query_input_schema ) {
if (
isset( $query_input_schema['transform'] ) &&
is_callable( $query_input_schema['transform'] )
) {
$transformed_query_input[ $query_input_key ] = $query_input_schema['transform'](
$query_input
);
}
}

return array_merge( $query_input, $transformed_query_input );
}

private static function get_query_input( array $block_context, object $query_config ): array {
$block_name = $block_context['blockName'];
$query_input = $block_context['queryInput'];
$overrides = $block_context['queryInputOverrides'] ?? [];

$query_input = self::apply_query_input_overrides( $query_input, $overrides, $block_name );
$query_input = self::transform_query_input( $query_input, $query_config );

return $query_input;
}

public static function execute_query( array $block_context, string $operation_name ): array|null {
$block_name = $block_context['blockName'];
$block_config = ConfigStore::get_configuration( $block_name );

if ( null === $block_config ) {
Expand All @@ -164,7 +199,7 @@ public static function execute_query( array $block_context, string $operation_na

try {
$query_config = $block_config['queries']['__DISPLAY__'];
$query_input = self::apply_query_input_overrides( $query_input, $overrides, $block_name );
$query_input = self::get_query_input( $block_context, $query_config );

$query_runner = $query_config->get_query_runner();
$query_results = $query_runner->execute( $query_input );
Expand Down
Loading

0 comments on commit 46daa9b

Please sign in to comment.