infinityloop-dev/graphpinator-where-directives

用于在列表中过滤值的可执行指令。

v1.0.4 2023-11-10 19:48 UTC

This package is auto-updated.

Last update: 2024-09-11 13:20:20 UTC


README

⚡🌐⚡ 用于在列表中过滤值的可执行指令。

简介

此包允许客户端使用指令过滤列表值。过滤在服务器上执行,最常见的用例是在客户端确切知道需要哪些数据时节省带宽。

安装

使用 composer 安装包

composer require infinityloop-dev/graphpinator-where-directives

如何使用

⚠️ 这些指令并不替代由参数和底层技术提供的标准最优过滤。 这些指令仅作为补充。

⚠️ 这些指令是可执行指令。它们的调用是由客户端请求的。 在服务器上包含此功能之前请三思。

为了在您的服务器上启用 where 指令,您需要做的只是将选定的指令放入您的 Container。您可以全部使用或只使用一些。

ListWhereDirectiveinfinityloop-dev/graphpinator-constraint-directives 有特殊要求,如果您想使用 @listWhere,则需要首先启用它。

此包包含以下指令

  • \Graphpinator\WhereDirectives\StringWhereDirective
  • \Graphpinator\WhereDirectives\IntWhereDirective
  • \Graphpinator\WhereDirectives\FloatWhereDirective
  • \Graphpinator\WhereDirectives\BooleanWhereDirective
  • \Graphpinator\WhereDirectives\ListWhereDirective

指令选项

通用选项

所有指令都有三个通用参数

  • field (String) - 此可选参数允许过滤嵌套结构。表示导航到过滤值的路径。
    • 有关更多信息,请访问以下 部分
  • orNull (Boolean) - 是否接受满足条件的 null 值(默认 false)。
  • not (Boolean) - 取消值条件的计算结果(默认 false)。

特定选项

  • @stringWhere
    • equals
    • contains
    • startsWith
    • endsWith
  • @intWhere & @floatWhere
    • equals
    • greaterThan
    • lessThan
  • @booleanWhere
    • equals
  • @listWhere
    • minItems
    • maxItems

操作优先级

可以使用以下代码片段描述优先级

$conditionIsMet = $value === null
    ? $orNull
    : satisfiesSpecificConditions($value);
$valueIsFilteredOut = $conditionIsMet === $not;

这意味着 not 参数具有最低优先级,甚至取消 orNull 参数。

字段参数

假设我们有一个包含一些字段的 BlogPost 对象列表。然后,为了接收所有博客文章,我们可以有一个这样的查询。

{ blogposts { title content likeCount author { name } } }

如果客户端只需要热门博客文章,即有超过 100 个赞,而服务器没有提供相关的过滤选项怎么办?

{ blogposts @intWhere(field: "likeCount", greaterThan: 100) { title content likeCount author { name } } }

如果客户端只需要名为 "Foo Bar" 的作者的帖子怎么办?

{ blogposts @stringWhere(field: "author.name", equals: "Foo Bar") { title content likeCount author { name } } }