-
Notifications
You must be signed in to change notification settings - Fork 824
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
DOCS: custom graphql filter fields #10192
Closed
unclecheese
wants to merge
1
commit into
silverstripe:4
from
unclecheese:pulls/4/filter-nearly-killed-her
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -189,6 +189,82 @@ MyProject\Models\ProductCategory: | |||||
title: true | ||||||
``` | ||||||
|
||||||
[info] | ||||||
You can add all fields with `'*': true`, just like with standard model definitions. | ||||||
[/info] | ||||||
|
||||||
##### Adding non-native filter fields | ||||||
|
||||||
Sometimes you may want to add a filter field that may stem from a custom getter, or a complex computation that | ||||||
isn't easily addressed by simple field comparisons. For cases like this, you can add the custom field as long | ||||||
as you provide instructions on how to resolve it. | ||||||
|
||||||
*app/_graphql/models.yml* | ||||||
```yaml | ||||||
MyProject\Models\Product: | ||||||
fields: | ||||||
title: true | ||||||
price: true | ||||||
operations: | ||||||
read: | ||||||
plugins: | ||||||
filter: | ||||||
fields: | ||||||
title: true | ||||||
hasReviews: true | ||||||
resolve: | ||||||
hasReviews: | ||||||
type: Boolean | ||||||
resolver: ['MyApp\Resolvers\Resolver', 'resolveHasReviewsFilter'] | ||||||
``` | ||||||
|
||||||
We've added the custom field `hasReviews` as a custom field in the `fields` section of the plugin config. A custom field | ||||||
like this that does not exist on the `Product` dataobject will cause the plugin to throw unless you've provided | ||||||
a `resolve` directive for it. | ||||||
|
||||||
In the `resolve` section, we need to provide two vital pieces of information: | ||||||
|
||||||
* What data type will the filter value be? (boolean in this case) | ||||||
* Where is the code that will apply this filter? (A static function in our `Resolver` class) | ||||||
|
||||||
The code to resolve the filter will get two relevant pieces of information in its `$context` parameter: | ||||||
|
||||||
* `filterComparator`: e.g. "eq", "ne", "gt", etc. | ||||||
* `filterValue`: What value we're comparing (true or false, in this case, since it's a boolean) | ||||||
|
||||||
Here's how we can resolve this custom filter: | ||||||
|
||||||
*app/src/Resolvers/Resolver.php* | ||||||
```php | ||||||
namespace MyApp\Resolvers; | ||||||
|
||||||
class Resolver | ||||||
{ | ||||||
public static function resolveHasReviewsFilter($list, $args, $context) | ||||||
{ | ||||||
$onlyWithReviews = $context['filterValue']; | ||||||
$comparator = $context['filterComparator']; | ||||||
|
||||||
if (!in_array($comparator, ['eq','ne'])) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
throw new \Exception('Invalid comparator for hasReviews: ' . $comparator); | ||||||
} | ||||||
if ($comparator === 'ne') { | ||||||
$onlyWithReviews = !$onlyWithReviews; | ||||||
} | ||||||
|
||||||
return $onlyWithReviews | ||||||
? $list->filter('Reviews.Count():GreaterThan', 0) | ||||||
: $list->filter('Reviews.Count()', 0); | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
[info] | ||||||
Custom filter fields are also a good opportunity to implement something like `filterByCallback` on your list for | ||||||
particularly complex computations that cannot be done at the database level. | ||||||
[/info] | ||||||
|
||||||
|
||||||
#### Disabling the filter plugin | ||||||
|
||||||
Just set it to `false` in the configuration. | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should put in param types in e.g.
array $context