AWS AppSync 放大与 Transformer v2 的关系 - 无法检索相关类型数据
AWS AppSync Amplify Relationship with Transformer v2 - Can't Retrieve related type data
所以我目前正在使用 AWS AppSync 和 Amplify,一切都很好,除了新的 Transformer V2,我正在努力让事情正常工作。
这是我的架构:
type Post
@model
@auth(
rules: [
{ allow: owner, ownerField: "username" }
{ allow: public, operations: [read] }
]
) {
id: ID!
title: String!
content: String!
username: String
@index(name: "postsByUsername", queryField: "postsByUsername")
coverImage: String
comments: [Comment] @hasMany
}
type Comment
@model
@auth(
rules: [
{ allow: owner, ownerField: "createdBy" }
{ allow: public, operations: [read] }
]
) {
id: ID!
message: String
}
我可以创建 post 和评论。但是,每当我查询 post 的列表时,我都不会收到评论。
下面是一个查询示例:
query MyQuery {
listPosts {
items {
comments {
items {
id
message
createdAt
}
}
content
title
username
}
}
}
这是我得到的相应结果:
{
"data": {
"listPosts": {
"items": [
{
"comments": {
"items": []
},
"content": "Testing stuff",
"title": "Today is the day",
"username": "bawp"
},
{
"comments": {
"items": []
},
"content": "### hello\n````\n function call()\n```",
"title": "next item of the day",
"username": "paulod"
},
{
"comments": {
"items": []
},
"content": "Hello Word",
"title": "santo dia",
"username": "paulod"
}
]
}
}
}
请注意
"comments": {
"items": []
},
总是空的!
尽管如此,当我查询评论 table 时,我至少添加了一条评论。
query MyQuery {
listComments {
items {
id
message
createdBy
}
}
}
结果:
{
"data": {
"listComments": {
"items": [
{
"id": "ddddf58b-df1c-498c-97b4-6d61361b4b9e",
"message": "Thjis is may coment here\n\n```\nCode could go here also!\n```",
"createdBy": "paulod"
}
]
}
}
}
我不确定我在这里遗漏了什么。请记住,我使用的是新指令(Transformer v2),而不是像
这样的旧关系指令
@connection
非常感谢任何帮助。
非常感谢!
好的,这是对我的问题的回答,供任何可能 运行 关注其中一些问题的人使用。
首先,
架构!
由于我在表之间添加了一些关系,因此必须正确添加指令。以下是我对架构所做的更改:
type Post
@model
@auth(
rules: [
{ allow: owner, ownerField: "username" }
{ allow: public, operations: [read] }
]
) {
id: ID!
title: String!
content: String!
username: String
@index(name: "postsByUsername", queryField: "postsByUsername")
coverImage: String
comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"]) #check out: https://docs.amplify.aws/cli/graphql/data-modeling/#has-many-relationship
}
#Comment
#queries: null - removes any authorization rules for queries, allowing anyone to query for comments
#ownerField:createdBy - sets the createdBy field as the currently signed in user
type Comment
@model
@auth(
rules: [
{ allow: owner, ownerField: "createdBy" }
{ allow: public, operations: [read] }
]
) {
id: ID!
message: String
post: Post @belongsTo(fields: ["postID"])
postID: ID @index(name: "byPost")
}
接下来,因为 appsync 在每次推送时自动生成 queries.js
文件,我不得不手动编辑这个文件(这不是真正的最终解决方案)。特别是 listPosts 查询:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
username
coverImage
comments {
items {
id
message
postID
createdAt
updatedAt
createdBy
}
}
createdAt
updatedAt
}
nextToken
}
}
`;
你看,当这个文件被自动生成时,它会有“nextToken”而不是我想要查询的字段。换句话说,comments
字段将具有 { nextToken }
因此,查询将如下所示:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
username
coverImage
comments {
nextToken # --> this is what was autogenerated, which was giving me issues!!!
}
createdAt
updatedAt
}
nextToken
}
}
`;
我将 nextToke
更改为之前在 post 中向您展示的内容后,一切都完美无缺。
现在,我知道手动更改此文件似乎不对。但是,我还没有找到更好的方法来做到这一点。
但就目前而言,这就是我能想到的。
我希望这可以帮助 运行 遇到类似问题的人。
谢谢!
所以我目前正在使用 AWS AppSync 和 Amplify,一切都很好,除了新的 Transformer V2,我正在努力让事情正常工作。
这是我的架构:
type Post
@model
@auth(
rules: [
{ allow: owner, ownerField: "username" }
{ allow: public, operations: [read] }
]
) {
id: ID!
title: String!
content: String!
username: String
@index(name: "postsByUsername", queryField: "postsByUsername")
coverImage: String
comments: [Comment] @hasMany
}
type Comment
@model
@auth(
rules: [
{ allow: owner, ownerField: "createdBy" }
{ allow: public, operations: [read] }
]
) {
id: ID!
message: String
}
我可以创建 post 和评论。但是,每当我查询 post 的列表时,我都不会收到评论。
下面是一个查询示例:
query MyQuery {
listPosts {
items {
comments {
items {
id
message
createdAt
}
}
content
title
username
}
}
}
这是我得到的相应结果:
{
"data": {
"listPosts": {
"items": [
{
"comments": {
"items": []
},
"content": "Testing stuff",
"title": "Today is the day",
"username": "bawp"
},
{
"comments": {
"items": []
},
"content": "### hello\n````\n function call()\n```",
"title": "next item of the day",
"username": "paulod"
},
{
"comments": {
"items": []
},
"content": "Hello Word",
"title": "santo dia",
"username": "paulod"
}
]
}
}
}
请注意
"comments": {
"items": []
},
总是空的!
尽管如此,当我查询评论 table 时,我至少添加了一条评论。
query MyQuery {
listComments {
items {
id
message
createdBy
}
}
}
结果:
{
"data": {
"listComments": {
"items": [
{
"id": "ddddf58b-df1c-498c-97b4-6d61361b4b9e",
"message": "Thjis is may coment here\n\n```\nCode could go here also!\n```",
"createdBy": "paulod"
}
]
}
}
}
我不确定我在这里遗漏了什么。请记住,我使用的是新指令(Transformer v2),而不是像
这样的旧关系指令@connection
非常感谢任何帮助。
非常感谢!
好的,这是对我的问题的回答,供任何可能 运行 关注其中一些问题的人使用。
首先,
架构! 由于我在表之间添加了一些关系,因此必须正确添加指令。以下是我对架构所做的更改:
type Post
@model
@auth(
rules: [
{ allow: owner, ownerField: "username" }
{ allow: public, operations: [read] }
]
) {
id: ID!
title: String!
content: String!
username: String
@index(name: "postsByUsername", queryField: "postsByUsername")
coverImage: String
comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"]) #check out: https://docs.amplify.aws/cli/graphql/data-modeling/#has-many-relationship
}
#Comment
#queries: null - removes any authorization rules for queries, allowing anyone to query for comments
#ownerField:createdBy - sets the createdBy field as the currently signed in user
type Comment
@model
@auth(
rules: [
{ allow: owner, ownerField: "createdBy" }
{ allow: public, operations: [read] }
]
) {
id: ID!
message: String
post: Post @belongsTo(fields: ["postID"])
postID: ID @index(name: "byPost")
}
接下来,因为 appsync 在每次推送时自动生成 queries.js
文件,我不得不手动编辑这个文件(这不是真正的最终解决方案)。特别是 listPosts 查询:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
username
coverImage
comments {
items {
id
message
postID
createdAt
updatedAt
createdBy
}
}
createdAt
updatedAt
}
nextToken
}
}
`;
你看,当这个文件被自动生成时,它会有“nextToken”而不是我想要查询的字段。换句话说,comments
字段将具有 { nextToken }
因此,查询将如下所示:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
username
coverImage
comments {
nextToken # --> this is what was autogenerated, which was giving me issues!!!
}
createdAt
updatedAt
}
nextToken
}
}
`;
我将 nextToke
更改为之前在 post 中向您展示的内容后,一切都完美无缺。
现在,我知道手动更改此文件似乎不对。但是,我还没有找到更好的方法来做到这一点。
但就目前而言,这就是我能想到的。
我希望这可以帮助 运行 遇到类似问题的人。
谢谢!