Angular AWS放大GraphQl查询未使用API服务返回预期数据
我很难使用AWS AMPLIFY GRAPHQL API查询所需的数据。
我设法在AWS AppSync控制台中编写了测试查询,但是我不知道如何使用API服务在代码中复制此查询。
查询
query MyQuery {
listGymnastMeetSessions(filter: {meetSessionID: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
items {
gymnastID
meetSessionID
gymnast {
firstName
lastName
meetScores(filter: {meetSessionId: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
items {
balanceBeam
floor
unevenBars
floor
}
}
}
}
}
}
我在AppSync控制台中创建的
"data": {
"listGymnastMeetSessions": {
"items": [
{
"gymnastID": "a4acbcb9-3b42-4a31-8448-d300e57b5b97",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Sophia",
"lastName": "Jones",
"meetScores": {
"items": [
{
"balanceBeam": 9.5,
"floor": 9.5,
"unevenBars": 9.5
}
]
}
}
},
{
"gymnastID": "e212e9cf-6620-4b1f-a6b4-f0a820b265da",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Olivia",
"lastName": "Jones",
"meetScores": {
"items": [
{
"balanceBeam": 9.15,
"floor": 9.05,
"unevenBars": 9.25
}
]
}
}
},
{
"gymnastID": "d97f5cea-a330-4717-85d7-b21ccc31a871",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Emily",
"lastName": "Jones",
"meetScores": {
"items": []
}
}
}
]
}
}
这是 使用API服务:
this.api
.ListGymnastMeetSessions({
meetSessionID: {
eq: this.id
}
})
.then((event) => {
this.gymnastMeetSessions = event.items as GymnastMeetSessions[];
console.log('gymnastMeetSessions ', this.gymnastMeetSessions);
});
这是API调用返回的内容的示例:
0:
createdAt: "2022-05-16T13:19:06.364Z"
gymnast:
createdAt: "2022-04-28T17:09:03.758Z"
dateOfBirth: "2020-03-28"
firstName: "Sophia"
gender: "female"
id: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
lastName: "Jones"
level: "Level 2"
private: null
teamId: "f1d61c2a-fbe9-4439-8870-fef776a1d5c9"
updatedAt: "2022-04-28T17:09:03.758Z"
__typename: "Gymnast"
[[Prototype]]: Object
gymnastID: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
id: "013e5f11-a06a-4bc6-b337-8ea834d5ccd0"
meetSession:
createdAt: "2022-05-13T20:10:57.081Z"
division: "Senior"
id: "3aea7082-60b8-4268-a816-00661e7a6e4d"
level: "Level 2"
meetId: "e0911c5c-1ffc-4acf-8b61-5c85016a1e16"
sessionName: "Session 1"
startDate: "2022-05-15"
startTime: "13:00:00.000Z"
updatedAt: "2022-05-13T20:10:57.081Z"
__typename: "MeetSession"
[[Prototype]]: Object
meetSessionID: "3aea7082-60b8-4268-a816-00661e7a6e4d"
updatedAt: "2022-05-16T13:19:06.364Z"
__typename: "GymnastMeetSessions"
您可以查看我的schema.graphql文件在这里a>。
寻找有关我可以尝试的事情的指导。有人可以将我指向正确的方向吗?
I am having a hard time trying to query the data I need using AWS Amplify Graphql API.
I have managed to write a test query in the AWS AppSync Console, but I do not know how to replicate this in code using the API Service.
Here is the query that I created in the AppSync Console and I want to call from my Angular app using the API service:
query MyQuery {
listGymnastMeetSessions(filter: {meetSessionID: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
items {
gymnastID
meetSessionID
gymnast {
firstName
lastName
meetScores(filter: {meetSessionId: {eq: "4aea7082-60b8-4268-a816-00661e7a6e4d"}}) {
items {
balanceBeam
floor
unevenBars
floor
}
}
}
}
}
}
Results of the above query returns what I am looking for:
"data": {
"listGymnastMeetSessions": {
"items": [
{
"gymnastID": "a4acbcb9-3b42-4a31-8448-d300e57b5b97",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Sophia",
"lastName": "Jones",
"meetScores": {
"items": [
{
"balanceBeam": 9.5,
"floor": 9.5,
"unevenBars": 9.5
}
]
}
}
},
{
"gymnastID": "e212e9cf-6620-4b1f-a6b4-f0a820b265da",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Olivia",
"lastName": "Jones",
"meetScores": {
"items": [
{
"balanceBeam": 9.15,
"floor": 9.05,
"unevenBars": 9.25
}
]
}
}
},
{
"gymnastID": "d97f5cea-a330-4717-85d7-b21ccc31a871",
"meetSessionID": "3aea7082-60b8-4268-a816-00661e7a6e4d",
"gymnast": {
"firstName": "Emily",
"lastName": "Jones",
"meetScores": {
"items": []
}
}
}
]
}
}
Here is the API call i'm making from my Angular app using the API service:
this.api
.ListGymnastMeetSessions({
meetSessionID: {
eq: this.id
}
})
.then((event) => {
this.gymnastMeetSessions = event.items as GymnastMeetSessions[];
console.log('gymnastMeetSessions ', this.gymnastMeetSessions);
});
Here is an example of what's returned from the API call:
0:
createdAt: "2022-05-16T13:19:06.364Z"
gymnast:
createdAt: "2022-04-28T17:09:03.758Z"
dateOfBirth: "2020-03-28"
firstName: "Sophia"
gender: "female"
id: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
lastName: "Jones"
level: "Level 2"
private: null
teamId: "f1d61c2a-fbe9-4439-8870-fef776a1d5c9"
updatedAt: "2022-04-28T17:09:03.758Z"
__typename: "Gymnast"
[[Prototype]]: Object
gymnastID: "b4acbcb9-3b42-4a31-8448-d300e57b5b97"
id: "013e5f11-a06a-4bc6-b337-8ea834d5ccd0"
meetSession:
createdAt: "2022-05-13T20:10:57.081Z"
division: "Senior"
id: "3aea7082-60b8-4268-a816-00661e7a6e4d"
level: "Level 2"
meetId: "e0911c5c-1ffc-4acf-8b61-5c85016a1e16"
sessionName: "Session 1"
startDate: "2022-05-15"
startTime: "13:00:00.000Z"
updatedAt: "2022-05-13T20:10:57.081Z"
__typename: "MeetSession"
[[Prototype]]: Object
meetSessionID: "3aea7082-60b8-4268-a816-00661e7a6e4d"
updatedAt: "2022-05-16T13:19:06.364Z"
__typename: "GymnastMeetSessions"
You can view my schema.graphql file here.
Looking for some guidance on things I can try. Can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以编写自己的GraphQl语句,以包括MeetScores信息。假设您的GraphQL模式是正确的,您可以执行类似的操作:
您可以将ID添加到查询中,而是可以使用该方法中的过滤器参数,而GraphQL语句中的$ filter则可以使用$ filter。
You can write your own graphql statement to include the meetscores info. Assuming your GraphQL schema is correct, you can do something like this:
Instead of explicitly adding the id to the query you can use your filter param that goes into the method, and $filter in your Graphql statement.
我能够通过在GraphQL架构中创建一个新模型来完成所需的工作。
这是我所做的:
I was able to do what I needed by created a new model in the graphql schema.
Here is what I did: