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

查看:270
本文介绍了如何使用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
}

在放大生成的查询文件中,我有一个名为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

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

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

还是获取整个列表并自己过滤掉会更好?更好的是,我的意思是效率-也许取决于TimePeriod表中列出的数据数量(如果有很多条目,然后从DB一张接一张,如果有少数条目全部获得并过滤掉?)

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天全站免登陆