首页 > 解决方案 > 如何将 graphql 条件数组转换为 Eloquent Builder 类?

问题描述

我在我的 lighthouse-php 项目中使用构建器类进行 graphql 查询。以下是此方案的查询模式。

listings(
        where: _ @builder(method: "App\\GraphQL\\Builders\\ListingsSearch") @whereConditions(columnsEnum: "ListingColumn"),
        orderBy: _ @orderBy(columnsEnum: "ListingColumn") ): [Listing!]! @paginate(type:SIMPLE maxCount: 1500 defaultCount: 10)

我的ListingsSearch构建器类有两个参数,第一个是查询构建器类,第二个是一个值,在这种情况下,它包含用户给出的 where 条件的数组。

例如对于以下查询;

{
  "where": {
      "AND": [{"column":"OWNERSHIP", "operator":"EQ", "value":["office","owned","fsbo","other"]}]
  },
  "orderBy": [],
  "first": 25,
  "page": 1
}

$value 参数内容为

array:2 [
  "operator" => "="
  "AND" => array:1 [
    0 => array:3 [
      "column" => "ownership"
      "operator" => "="
      "value" => array:4 [
        0 => "office"
        1 => "owned"
        2 => "fsbo"
        3 => "other"
      ]
    ]
  ]
]

我需要的是解释这个数组并将其转换为包含给定条件的查询构建器类。如果有人能带领我,我会很高兴。

标签: phplaraveleloquentgraphqllaravel-lighthouse

解决方案


推荐阅读