如何在 AWS Amplify GraphQL 客户端中进行过滤 [英] How to do filteration in AWS Amplify GraphQL Client
问题描述
我正在尝试使用 Amplify GraphQL 客户端实现 GraphQL 过滤器.我得到了一个待办事项列表,想检索状态为已完成的待办事项列表.
I'm trying to implement GraphQL filter using Amplify GraphQL Client. I got a list of todos and wanted to retrieve list of todos that has status complete.
文档只展示了如何获取所有项目和单个项目
The Documentation only show how to get all items and single item
const allTodos = await API.graphql(graphqlOperation(queries.listTodos));
console.log(allTodos);
有人可以指点我如何将过滤器应用于 listTodos 以便它仅返回状态为完成的待办事项.
Could someone please point me how to apply filter to the listTodos so that it return todos with status complete only.
我尝试执行以下操作,但这是错误的.
I tried to do the following but it is wrong.
API.graphql(graphqlOperation(queries.listTodos(filter: {
status: {
eq: "completed"
}
})));
推荐答案
我想我已经回答了您的问题,但我也有一个关于 AWS Amplify codegen
查询、突变、等等.如果你查看在 ~/graphql
文件夹中生成的代码,你会发现一个类似于这样的声明文件:
I think I have an answer to your question, but I also have a similar question regarding the AWS Amplify codegen
queries, mutations, etc. If you look at the code that was generated inside of ~/graphql
folder, you'll find a declaration file similar to this:
export const listOrganizations = `query ListOrganizations(
$filter: ModelOrganizationFilterInput
$limit: Int
$nextToken: String
) {
listOrganizations(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
address
}
nextToken
}
}
`;
您可以在此处看到 ListOrganizations
查询的第一个参数(在您的情况下为 ListTodos
查询)采用 filter 的第一个参数:$filter代码>.到目前为止,我已经发现您可以通过执行以下操作来修改此查询...
You can see here that the first parameter to the ListOrganizations
query (in your case, ListTodos
query) takes a first argument of filter: $filter
. I have figured out so far that you can modify this query by doing the following...
API.graphql(graphqlOperation(queries.listTodos, {
filter: {
status: {
eq: "completed"
}
}
})));
这应该过滤掉所有 Todo,除了那些 status
等于 completed
的 Todo.我遇到的问题是我想启用不同级别的访问控制,以便任何拥有 Admin
的 Cognito 用户池组的人都可以查看 @model
以及@owner
.我能够使用 @auth
转换器使这一切正常工作,但现在我的问题是在某些屏幕上,我只想显示某些属于 owner
的实体那个实体的,因为我也是 Admin
,API 默认让我得到一切.我想使用这个 @filter
或 ModelOrganizationFilterInput
只给我我是所有者的数据.我发现这样做的唯一方法是将 owner
字段添加到我的架构中,但是 API 总是提供所有者字段,我想过滤掉该字段.
This should filter out all Todos except the ones where their status
is equal to completed
. The problem that I am having is that I want to enable different levels of access control such that anyone with a Cognito User Pool Group of Admin
may view @model
as well as the @owner
. And I was able to get this all working using the @auth
transformer, but now my problem is that on some screens, I only want to display certain entities that are the owner
of that entity and because I am also an Admin
, the API defaults to getting me everything. I want to use this @filter
or ModelOrganizationFilterInput
to only give me the data where I am the owner. The only way I have found to do this was to add the owner
field to my Schema, but then the API always provides the owner field and I want to filter that field out.
我能找到的关于 aws-amplify
API
和 graphqlOperation
方法的唯一文档在这里:https://aws-amplify.github.io/docs/js/api 但例子并不多并且它们没有展示 API 如何在客户端上工作.我被卡住了.
The only documentation that I can find on how the aws-amplify
API
and graphqlOperation
methods is here: https://aws-amplify.github.io/docs/js/api but there are not many examples and they don't show much of how the API works on the client. I'm stuck.
这篇关于如何在 AWS Amplify GraphQL 客户端中进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!