Skip to content

Commit 91e24e8

Browse files
authored
Merge pull request #114 from wp-graphql/feat/add-pre-filter-for-resolve-type
feat: add pre filter for resolve type
2 parents d09512b + 00388b6 commit 91e24e8

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/AcfGraphQLFieldType.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,23 @@ public function __construct( string $acf_field_type, $config = [] ) {
5858
* @return void
5959
*/
6060
public function set_config( $config = [] ): void {
61+
$_config = [];
62+
63+
if ( is_callable( $config ) ) {
64+
$config = $config( $this->get_acf_field_type(), $this );
65+
}
66+
6167
if ( is_array( $config ) ) {
62-
$this->config = $config;
63-
} elseif ( is_callable( $config ) ) {
64-
$_config = $config( $this->get_acf_field_type(), $this );
65-
if ( is_array( $_config ) ) {
66-
$this->config = $_config;
67-
}
68+
$_config = $config;
6869
}
70+
71+
/**
72+
* Filter the config before setting it on the class
73+
*
74+
* @param array $_config The Config passed to the AcfGraphQLFieldType
75+
* @param \WPGraphQL\Acf\AcfGraphQLFieldType $acf_graphql_field_type The AcfGraphQLFieldType instance
76+
*/
77+
$this->config = apply_filters( 'wpgraphql/acf/field_type_config', $_config, $this );
6978
}
7079

7180
/**
@@ -252,6 +261,24 @@ public function get_excluded_admin_field_settings(): array {
252261
public function get_resolver( $root, array $args, AppContext $context, ResolveInfo $info, self $field_type, FieldConfig $field_config ) {
253262
$acf_field = $field_config->get_acf_field();
254263

264+
/**
265+
* If external code has filtered this to return anything other than null, use the filtered type instead of the default
266+
*
267+
* @param mixed $null Filtered value. If filtered to anything other than null, use the filter response instead of the default logic below
268+
* @param mixed $root The value of the previously resolved field in the tree
269+
* @param array $args The arguments input on the field
270+
* @param \WPGraphQL\AppContext $context The Context passed through resolution
271+
* @param \GraphQL\Type\Definition\ResolveInfo $info Information about the field resolving
272+
* @param \WPGraphQL\Acf\AcfGraphQLFieldType $field_type The Type of ACF Field resolving
273+
* @param \WPGraphQL\Acf\FieldConfig $field_config The Config of the ACF Field resolving
274+
* @param array $acf_field The ACF Field config
275+
*/
276+
$pre_get_resolver = apply_filters( 'wpgraphql/acf/field_type_resolver', null, $root, $args, $context, $info, $field_type, $field_config, $acf_field );
277+
278+
if ( null !== $pre_get_resolver ) {
279+
return $pre_get_resolver;
280+
}
281+
255282
$resolver = $field_config->resolve_field( $root, $args, $context, $info );
256283

257284
if ( isset( $acf_field['graphql_resolver'] ) ) {
@@ -275,6 +302,15 @@ public function get_resolver( $root, array $args, AppContext $context, ResolveIn
275302
public function get_resolve_type( FieldConfig $field_config ) {
276303
$acf_field = $field_config->get_acf_field();
277304

305+
/**
306+
* If external code has filtered this to return anything other than null, use the filtered type instead of the default
307+
*/
308+
$pre_get_resolve_type = apply_filters( 'wpgraphql/acf/field_type_resolve_type', null, $field_config, $acf_field );
309+
310+
if ( null !== $pre_get_resolve_type ) {
311+
return $pre_get_resolve_type;
312+
}
313+
278314
$resolve_type = 'String';
279315

280316
if ( isset( $acf_field['graphql_resolve_type'] ) ) {

0 commit comments

Comments
 (0)