如何在AWS Amplify GraphQL Client中进行过滤 [英] How to do filteration in AWS Amplify GraphQL Client
问题描述
我正在尝试使用Amplify GraphQL Client实现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"
}
}
})));
这应该过滤掉所有待办事项,除非它们的status
等于completed
.我遇到的问题是我想启用不同级别的访问控制,以使具有Admin
的Cognito用户池组的任何人都可以查看@model
和@owner
.而且我可以使用@auth
转换器使所有这些工作正常,但是现在我的问题是,在某些屏幕上,我只想显示属于该实体的owner
的某些实体,因为我也是一个@filter
或ModelOrganizationFilterInput
仅向我提供我作为所有者的数据.我发现这样做的唯一方法是将owner
字段添加到我的架构中,但是API始终提供了owner字段,而我想将该字段过滤掉.
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 Client中进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!