有没有办法将复杂的 where 条件定义为变量? [英] Is there a way define complex where conditions as variables?

查看:29
本文介绍了有没有办法将复杂的 where 条件定义为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 GraphQL 查询,它的 where 条件变得复杂,但在 Apollo GraphQL iOS 客户端中,编译项目后无法更改查询.Apollo GraphQL iOS 客户端提供了更改查询中定义的变量的机会,而不是查询本身.

I have created a GraphQL Query that gets complex where conditions but in Apollo GraphQL iOS client there is no way of changing query after compiling the project. Apollo GraphQL iOS client gives chance to change defined variables in the query but not the query itself.

我原来的 Lighthouse GraphQL 查询如下所示;

My original Lighthouse GraphQL query is like following;

query ($first:Int, $page:Int) {
    my_listings(
        where: {
            AND: [
                { column: NET_AREA, operator: GTE, value: 200 }
            ]
        }
        orderBy: [
            { column: ID, order: ASC }
            ]
        first: $first
        page: $page
    ) {
        data {
            ...listingFields
        }
        paginatorInfo {
            currentPage
            lastPage
        }
    }
}

Altered Lighthouse GraphQL 查询是这样的,只添加了 MyListingsWhereConditions 类型的 $conditions 变量.

Altered Lighthouse GraphQL query is like this, only added $conditions variable which is MyListingsWhereWhereConditions type.

query($first: Int, $page: Int, $conditions:MyListingsWhereWhereConditions) {
  my_listings(
    where: $conditions
    orderBy: [{ column: ID, order: ASC }]
    first: $first
    page: $page
  ) {
    data {
      ...listingFields
    }
    paginatorInfo {
      currentPage
      lastPage
    }
  }
}

当我将以下变量提供给第二个查询时,Lighthouse 服务器返回以下消息

When I give the following variables into the second query Lighthouse server returns me the following message

{
  "first": 1,
  "page": 1,
  "conditions": {"AND": {"column": "PRICE", "operator": "LTE","value": "190"}}
}

错误提示

Variable \"$conditions\" got invalid value {\"AND\":{\"column\":\"PRICE\",\"operator\":\"LTE\",\"value\":\"190\"}}; Expected type MyListingsWhereWhereConditions to be an object at value.AND.column.

有没有办法在变量中给出这些条件?没有它,我就无法在 iOS 客户端上找到改变 where 条件的方法.

Is there a way of giving these conditions in a variable ? Without it I couldn't find a way of changing where condition on iOS client.

推荐答案

AND"应该包含对象数组,而不是对象.

"AND" should contain array of objects, not object.

"conditions": {"AND": [{"column": "PRICE", "operator": "LTE","value": "190"}}]

这篇关于有没有办法将复杂的 where 条件定义为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆