如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL [英] How to Filter List/Queries With AND/OR operators AWS Amplify JavaScript GraphQL

查看:20
本文介绍了如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 AWS Amplify 和 GraphQL.也刚刚开始构建 React Native App - 这很有趣!

I am new to using AWS Amplify and GraphQL. Also just started building out React Native App - which is a lot of fun!

我有一个名为 TimePeriods 架构的表,它看起来像这样

I have a table called TimePeriods schema for it looks like this

type TimePeriod @model {
  id: ID!
  name: String!
  startYear: String!
  endYear: String!,
  artworks: [ArtWorkTimePeriod] @connection (name: "TimePeriodArtWorks") #Many to Many Relationship
  artists: [ArtistTimePeriod] @connection (name: "TimePeriodArtists") #Many to Many Relationship
}

在由 amplify 生成的查询文件中,我有一个名为 listTimePeriods 的函数.

In the queries file generated by amplify I have a function called listTimePeriods.

export const listTimePeriods = /* GraphQL */ `
  query ListTimePeriods(
    $filter: ModelTimePeriodFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listTimePeriods(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        name
        startYear
        endYear
        artworks {
          nextToken
        }
        artists {
          nextToken
        }
      }
      nextToken
    }
  }
`;

我想做的是按条件过滤,例如我想获取 ID 等于 1、2 或 3 的所有时间段的列表.我假设它可以通过以下方式完成

What I am trying to do is filter this by condition for example I'd like to get list of all Time Periods where IDs equal 1, 2 or 3. I was assuming it could be done in a following manner

export async function GetTimePeriodsByIds(idArr=[]){
    let filter = {
        id: {
            eq: [1,2,3]
        }
    };
    return await API.graphql(graphqlOperation(listTimePeriods, {limit: 20, filter:filter}));
}

但我认为你做不到.如果您对此有任何解决方案,那将意味着很多 - 即使只是像

but I do not think you can do that. If you have any kinds of solution regarding this, it would mean a lot - even just the insight like

  1. 如果它根本不起作用 - 他们是否有任何理由决定不实施它?
  2. 在这种情况下使用 for 循环并调用会更好吗

  1. If it does not work at all - is there any reason why they decided not to implement it?
  2. Would it be better in the case to use a for loop and call

await API.graphql(graphqlOperation(getTimePeriod, {id: id}));

await API.graphql(graphqlOperation(getTimePeriod, {id: id}));

还是获取整个列表并自己过滤掉会更好?更好的是我的意思是效率 - 也许这取决于将在 TimePeriod 表中列出的数据数量(如果有很多条目然后从数据库中一个一个地获取,如果少量条目获取所有并过滤掉它?)

or would it be better to get the whole list and filter it out myself? And by better I mean efficiency - maybe it depends on the number of data that will be listed in the TimePeriod table (if many entries then get one by one from DB, if small number of entries get all and filter it out?)

推荐答案

大家好,调查这个问题的人 - 经过大量研究我发现可以这样实现:

Hello to everyone looking into this question - after a lot of research I found out that it can be achieved like this:

     let filter = {
            or: [
                {
                    id: {eq:1}
                },
                {
                    id: {eq:2}
                }]
        };
return await API.graphql(graphqlOperation(listTimePeriods, {limit: 20, filter:filter}));

为了了解您可以使用 GraphQL List 使用 AND 和 OR 操作做什么,您需要在文件中进行类似的定义

in order to find out what you can do with AND and OR operations with GraphQL List you need to similar definition in your files

input ModelTimePeriodFilterInput {
  id: ModelIDInput
  name: ModelStringInput
  startYear: ModelStringInput
  endYear: ModelStringInput
  and: [ModelTimePeriodFilterInput]
  or: [ModelTimePeriodFilterInput]
  not: ModelTimePeriodFilterInput
}

应该位于 backend/api/projectname/build/schema.graphql

which should be located in backend/api/projectname/build/schema.graphql

这意味着您可以以类似的方式编写 AND 和 OR 过滤器.希望这会有所帮助 - 我希望 AWS 文档使这更容易理解

This means that in a similar manner you can write AND and OR filters. Hope this helps - I wish AWS Documentation made this easier to understand

如果您有类似的问题但在这里找不到答案,也许这篇文章会有所帮助 - GraphQL:过滤数组中的数据

If you have a similar question but cannot find your answer here maybe this post will help - GraphQL: Filter data in an array

这篇关于如何使用 AND/OR 运算符过滤列表/查询 AWS Amplify JavaScript GraphQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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