【问题标题】:How do I add an OR condition in a graphql where statement?如何在 graphql where 语句中添加 OR 条件?
【发布时间】:2019-12-28 09:00:33
【问题描述】:

我无法通过 graphql Playground 在 Strapi 中获得使用“OR”条件的 WHERE 语句。

我想返回“标题”或“内容”字段包含 search_text 的所有结果。

我尝试了以下方法:

articles(where: { 
or: [
  {"title_contains" : "search_text"},
  {"content_contains" : "search_text"}
 ]
}) {
title
content
}

但返回错误。

错误:“您的过滤器包含一个字段‘或’,它既没有出现在您的模型定义中,也没有出现在它的关系中。

一些有效的陈述(但不是我所追求的):

where: { "title_contains" : "sometext" }

工作,但表现为“与”

where: { 
"title_contains" : "search_text", 
"content_contains" : "search_text"
}

【问题讨论】:

  • 您好!此功能在 Strapi 中不可用。您能否在 GitHub 存储库上创建一个功能请求。

标签: graphql where-clause strapi


【解决方案1】:

从 7 月开始,可以这样做了

(where: { _or: [{ someField: "1" }, { someField2: "2" }] })

【讨论】:

    【解决方案2】:

    这里的解决方法是创建一个自定义查询并进行符合您需要的自定义数据库查询。

    以下是创建自定义 GraphQL 查询的方法: https://strapi.io/documentation/3.0.0-beta.x/guides/graphql.html#example-2

    要访问数据模型,您必须使用 strapi.models.article(对于 Article 模型),并且在此变量中,您将访问本机 Mongoose 或 Bookshelf 函数。因此,您将能够使用 OR 进行查询

    【讨论】:

      猜你喜欢
      • 2019-06-26
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-22
      • 2012-07-26
      • 1970-01-01
      • 2016-12-16
      相关资源
      最近更新 更多