我有一个带有用户和事件的AppSync graphql模式,该模式应该具有双向连接。我的架构如下:
type User @model {
id: ID!
email: String
events: [EventUser] @connection(name: "EventUser", keyName: "byUser", fields: ["id"])
}
type Event @model {
id: ID!
name: String
description: String
registeredUsers: [EventUser] @connection(name: "EventUser", keyName: "byEvent", fields: ["id"])
invitedUsers: [User]
}
type EventUser
@model(queries: null)
@key(name: "byEvent", fields: ["eventId", "userId"])
@key(name: "byUser", fields: ["userId", "eventId"]) {
id: ID!
eventId: ID!
userId: ID!
event: Event! @connection(fields: ["eventId"])
user: User! @connection(fields: ["userId"])
}
CLI生成的查询如下:
export const listEvents = /* GraphQL */ `
query ListEvents(
$filter: ModelEventFilterInput
$limit: Int
$nextToken: String
) {
listEvents(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
registeredUsers {
nextToken
}
createdAt
updatedAt
}
nextToken
}
}
`;
当我使用以下命令运行此查询时:
const result = await API.graphql(graphqlOperation(queries.listEvents, { limit: 10}))
我得到这些结果:
[
{
"createdAt": "2020-08-17T21:36:04.502Z",
"description": "Event description",
"id": "some-unique-id",
"name": "Event name",
"registeredUsers": Object {
"nextToken": null,
},
"updatedAt": "2020-08-17T21:36:04.502Z",
},
{
"createdAt": "2020-08-17T21:36:04.502Z",
"id": "some-other-unique-id",
"name": "event 2 name",
"registeredUsers": Object {
"nextToken": null, // <- This seems wrong.
},
"updatedAt": "2020-08-17T21:36:04.502Z"
}
]
registeredUsers
字段应为项目数组。我可以像这样在AppSync控制台中运行查询:
query ListEvents {
listEvents( limit: 10) {
items {
id
name
registeredUsers {
items {
userId
}
}
}
}
}
并返回正确的结果:
{
"data": {
"listEvents": {
"items": [
{
"id": "some-unique-id",
"name": "event name",
"registeredUsers": {
"items": [
{
"userId": "user"
},
{
"userId": "user1"
}
]
}
},
{
"id": "some-other-unique-id",
"name": "event 2 name",
"registeredUsers": {
"items": []
}
},
]
}
}
}
很明显,控制台查询虽然没有处理nextToken
分页,但是可以产生正确的结果。
我的问题是:CLI生成的查询无效吗?如果是这样,是否是因为我的架构设置不正确?
如果生成的查询有效,我将如何调用它以获取EventUser
数据?