Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional generate field to input_schema #198

Merged
merged 8 commits into from
Nov 21, 2024
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,
],
'generate' => [
Copy link
Contributor Author

@shekharnwagh shekharnwagh Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about naming this as transform or filter, but ended up naming it as generate to match with name for similar field in the output_schema -

'generate' => [
'type' => 'function',
'required' => false,
],

Wouldn't mind using another name if there is an good reason to do it.

'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 {
/**
* Generate the query input for a block binding before executing the query if
* a generate function is provided. This allows the query input to be generated
* from combined input variables or 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_with_generators(
array $query_input,
object $query_config
): array {
$generated_query_input = [];

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

return array_merge( $query_input, $generated_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_with_generators( $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