graphql - 在 AWS Amplify GraphQL 中对结果进行排序而不进行过滤

标签 graphql aws-amplify

graphql.schema 中提供了一个非常简单的模型,我将如何执行简单的排序查询?

type Todo @model
  id: ID!
  text: String!
}
queries.js 中生成以下内容.
export const listTodos = /* GraphQL */ `
  query ListTodos(
    $filter: ModelTodoFilterInput
    $limit: Int
    $nextToken: String
  ) {
    listTodos(filter: $filter, limit: $limit, nextToken: $nextToken) {
      items {
        id
        text
      }
      nextToken
    }
  }
`;
我发现多个消息来源将我指向 @key 的方向。 directive .这个类似的问题解决了这种方法( GraphQL with AWS Amplify - how to enable sorting on query )。
虽然这看起来很有希望并且成功地生成了我可以使用的新查询,但我尝试过的所有方法都要求我在对数据进行排序之前对其进行过滤。我想要做的就是在给定的列名上对我的待办事项结果进行排序,并使用给定的排序方向(ASC/DESC)。
这是我执行简单(未排序)查询的方式:const todos = await API.graphql(graphqlOperation(listTodos));我希望按照以下方式做一些事情:const todos = await API.graphql(graphqlOperation(listTodos, {sortField: "text", sortDirection: "ASC"} )) .

最佳答案

使用 @searchable 指令装饰您的模型,如下所示:

type Todo @model @searchable
{
  id: ID!
  text: String!
}
之后,您可以使用如下排序功能查询数据:
import { searchToDos } from '../graphql/queries';
import { API, graphqlOperation } from 'aws-amplify';

const toDoData = await API.graphql(graphqlOperation(searchToDos, {
  sort: {
    direction: 'asc',
    field: 'text'
  }
}));
console.log(toDoData.data.searchToDos.items);
有关更多信息,请参阅
  • https://github.com/aws-amplify/amplify-cli/issues/1851#issuecomment-545245633
  • https://docs.amplify.aws/cli/graphql-transformer/directives#searchable
  • 关于graphql - 在 AWS Amplify GraphQL 中对结果进行排序而不进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62867425/

    相关文章:

    javascript - 如何在类型定义中为 GraphQL 字段定义数组数据类型

    GraphQL 忽略了一些(但不是全部)任意分配的 markdown frontmatter 数据

    javascript - Apollo 客户端 - 使用对象列表中的缓存结果来响应对单个对象的查询

    javascript - Electron Amplify AppSync "Realtime disabled when in a server-side environment"

    amazon-web-services - 开 Jest 匆忙 map : Haste module naming collision (AWS, RN)

    javascript - 应用程序首次打开时 AWS Datastore 不查询

    reactjs - AWS Amplify ReactJS 应用程序无法重新加载页面

    objective-c - 以编程方式访问 AWS IAM 用户策略和访问权限

    node.js - 将 Rover 与 graphql-shield 结合使用

    reactjs - WebSocket 连接到 'ws:<URL>/_next/webpack-hmr' 失败 : WebSocket is closed before the connection is established