@connection上的AWS Amplify Graphql查询 [英] AWS Amplify Graphql query on @connection

查看:143
本文介绍了@connection上的AWS Amplify Graphql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Graphql/AppSync使用AWS Amplify(前端使用Cli和Angular 7),想知道当超过10个项目时如何获取所有连接的项目吗?

I am using AWS Amplify (with Cli and Angular 7 for front end) for Graphql/AppSync and wondering how to get all connected items when it exceeds 10 items?

比方说,我已经创建了一个schema.graphql像这样:

Let's say I have created a schema.graphql like this:

type User @model {
  id: ID!
  firstname: String
  lastname: String
  project: Project @connection(name: "ProjectUsers")
}

type Project @model {
  id: ID!
  title: String
  task: String
  members: [User] @connection(name: "ProjectUsers")
}

运行放大推送时,它会生成查询和变异.当运行具有项目ID(从生成的API.service.ts文件)的GetProject查询时,它将返回具有连接的Users的Project项.但是,如果该项目的用户数超过10,则只会给我10个第一个用户和一个下一个令牌:

When running amplify push it generates queries and mutations. When running the GetProject query with the id of a project (from the generated API.service.ts file) it returns the Project item with the connected Users. But if the project have more than 10 Users, it only gives me the 10 first users and a next token:

{
  id: "67b1fc0a-fd1f-4e8b-9bd7-b82b2aea5d3b",
  title: "Test",
  task: "test",
  members: {
    items: {
      0: {__typename: "User", id: "f245809a...}
      1: ...
      (2-8: ...)
      9: ...
      nextToken: "qwerj23r2kj....3223oop32kjo",
       __typename: "ModelUserConnection";
    }
  }
  __typename: "Project"
}

我可以看到多种解决方案,但看不到如何解决:

I can see multiple solutions for this, but not how to do them:

  1. 是否可以更改schema.grapql来更改代码生成,以便它可以生成更改限制的功能,例如. 100,而不是标准的10?

  1. Is it possible to change schema.grapql to change the codegen so that it can generate the ability to change the limit, ex. 100 instead of the standard 10?

使用nextToken从生成的API.service.ts文件中对结果进行分页吗?

Use the nextToken to paginate the results, from the generated API.service.ts file?

更改schema.graphql文件,以便生成的ModelUserFilterInput具有userProjectId字段(在生成的ListUsers查询中使用)吗?

Change the schema.graphql file so that the generated ModelUserFilterInput has the userProjectId field (to use in the generated ListUsers query)?

或者是否有其他解决方案可以通过自动生成的文件(API.service.ts)中的查询来获取项目的所有用户?

Or are there any other solutions to get all the Users of a Project with the queries in the automatically generated file (API.service.ts)?

到目前为止,我所看到的唯一解决方案是首先运行ListUsers查询(不使用任何过滤器),然后遍历所有这些查询以检查其是否具有正确的项目ID.但是,如果用户数据库很大,则可能增长为大量数据,而且速度很慢,并且使用@connection的好处还不存在.

As of now the only solution I can see is to first run the ListUsers query (without any filters), and then loop through all of them to check if it has the correct project id. But if the users database is large this can grow to be a lot of data and be really slow, and the benefits to use @connection isn't really there.

很抱歉很长一段时间,我希望我已经对这个问题做了足够的解释.

Sorry for long post and I hope I have explained the problem enough.

推荐答案

A)更改查询

query {
  getProjet(id: "123") {
    id
    members(limit: 50) {
      items {
        firstname
      }
    }
 }

B)附加一个解析器

AWS AppSync控制台中,在架构"部分的右端.通过UserConnection或类似的查找UserConnection.items进行过滤,然后单击Attach.

In the AWS AppSync console, at the right end side of the Schema section. Filter by UserConnection or similar find UserConnection.items and click Attach.

1)数据源:UserTable0

2)请求映射模板:ListItems

2) Request mapping template: ListItems

{
    "version" : "2017-02-28",
    "operation" : "Scan",
    "limit": $util.defaultIfNull(${ctx.args.limit}, 50),
    "nextToken": $util.toJson($util.defaultIfNullOrBlank($ctx.args.nextToken, null))
}

使用限制作为参数ctx.args.limit,如果为空则使用50.

Use the limit coming as an argument ctx.args.limit or if its null use 50.

3)响应映射模板

$util.toJson($ctx.result.items)

通过这样做,您可以更改基础表的扫描/获取方式.

By doing this you can change how the underlying table is being scanned/fetched.

C)分页

另一种解决方案是在应用程序级别进行分页,并保留10个项目的限制.

Another solution would be to paginate at the application level and leave the 10 items limit.

注意:我可能会缺少其他解决方案.

Note: I may be missing other solutions.

更新:与Amplify Console一起使用此解决方案.

现在,您可以在本地更新解析器,并使用Amplify CLI将更新推送到您的帐户中.运作方式如下.

Now, you can update your resolvers locally and use the Amplify CLI to push the updates into your account. Here’s how it works.

在创建AWS AppSync API之后,您现在将在API文件夹中的Amplify项目中创建一个名为resolvers的新空文件夹.要创建自定义解析器,请创建一个文件(即 API项目的 resolvers目录中的 Query.getTodo.req.vtl ).下次运行amplify push或ampl api gql-compile时,将使用您的解析器模板,而不是自动生成的模板.您可以类似地创建一个 Query.getTodo.res.vtl 文件,以更改解析器的响应映射模板的行为.

After creating your AWS AppSync API, you will now have a new empty folder called resolvers created in your Amplify project in the API folder. To create a custom resolver, create a file (i.e. Query.getTodo.req.vtl) in the resolvers directory of your API project. The next time you run amplify push or amplify api gql-compile, your resolver template will be used instead of the auto-generated template. You may similarly create a Query.getTodo.res.vtl file to change the behavior of the resolver’s response mapping template.

<amplify-app>
    |_ amplify
      |_ .config
      |_ #current-cloud-backend
      |_ backend
        |_ api
          |_ resolvers
             Query.getProject.req.vtl
             Query.getProject.res.vtl
      team-provider-info.json

查看全文

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