Skip to content

Commit

Permalink
Feature: Custom Facet Config (#25)
Browse files Browse the repository at this point in the history
* Add filter for facet connection

* Add docs for custom facet config
  • Loading branch information
matteodem committed Jun 19, 2022
1 parent fa26837 commit f5b3402
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,43 @@ query GetPosts($query: FacetQueryArgs, $after: String, $search: String, $orderBy
}
```

## WooCommerce Support

Support for WooCommerce Products can be added with following configuration:

```php
add_action('graphql_register_types', function () {
register_graphql_facet_type('product');
});

add_filter('facetwp_graphql_facet_connection_config', function (array $default_graphql_config, array $config) {
$type = $config['type'];
$singular = $config['singular'];
$field = $config['field'];
$plural = $config['plural'];

return [
'fromType' => $field,
'toType' => $singular,
'fromFieldName' => lcfirst($plural),
'connectionArgs' => Products::get_connection_args(),
'resolveNode' => function ($node, $_args, $context) use ($type) {
return $context->get_loader($type)->load_deferred($node->ID);
},
'resolve' => function ($source, $args, $context, $info) use ($type) {
$resolver = new PostObjectConnectionResolver($source, $args, $context, $info, $type);

if ($type === 'product') {
$resolver = Products::set_ordering_query_args( $resolver, $args );
}

return $resolver
->set_query_arg('post__in', $source['results'])
->get_connection();
},
];
}, 100, 2);
```

## Limitations
Currently the plugin only has been tested using Checkbox and Radio facet types. Support for additional types is in development.
12 changes: 10 additions & 2 deletions class-facetwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private function register_facet_connection($config)
$field = $config['field'];
$plural = $config['plural'];

register_graphql_connection([
$default_facet_connection_config = [
'fromType' => $field,
'toType' => $singular,
'fromFieldName' => lcfirst($plural),
Expand All @@ -261,7 +261,15 @@ private function register_facet_connection($config)
->set_query_arg('post__in', $source['results'])
->get_connection();
},
]);
];

$graphql_connection_config = apply_filters(
'facetwp_graphql_facet_connection_config',
$default_facet_connection_config,
$config
);

register_graphql_connection($graphql_connection_config);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions wp-graphql-facetwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ function () use ($versions) {
return false;
}
});

add_filter('facetwp_graphql_facet_connection_config', function (array $default_graphql_config) {
return $default_graphql_config;
}, 10, 1);

0 comments on commit f5b3402

Please sign in to comment.