【发布时间】:2021-07-06 17:02:50
【问题描述】:
注意 - 我对 GraphQL 还很陌生,我已经看到其他有关此错误的 stackoverflow 问题被报告,但他们使用的是 Apollo。在这里,我使用的是 AWS Amplify 和 AppSync 自己的 GraphQL 客户端。所以我不能使用这些解决方案。
tldr - 我正在尝试从数据库中获取项目列表,但我不断收到一个神秘的网络错误和一个我不理解的存储错误。详情:-
这是我对 AWS Appsync 客户端的客户端定义:-
export const client = new AWSAppSyncClient({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: awsconfig.aws_appsync_authenticationType,
jwtToken: async () =>
(await Auth.currentSession()).getAccessToken().getJwtToken(),
},
});
这是我的查询方法:-
listInstitutions = () => {
client
.query({
query: queries.ListInstitutions,
})
.then((res: any) => {
this.institutions = res.data.listInstitutions.items;
console.log('this.institutions', this.institutions);
})
.catch((err) => {
console.error(err);
this.institutions = [];
});
};
这是我的查询定义:-
query ListInstitutions(
$filter: ModelInstitutionFilterInput
$limit: Int
$nextToken: String
) {
listInstitutions(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
location
city
website
phone
logo
bio
admins {
id
name
title
bio
createdAt
updatedAt
owner
}
classes {
nextToken
}
learners {
nextToken
}
createdAt
updatedAt
}
nextToken
}
}
控制台中的错误如下所示:-
Error: Network error: Error writing result to store for query:
query ListInstitutions($filter: ModelInstitutionFilterInput, $limit: Int, $nextToken: String) {
listInstitutions(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
location
city
website
phone
logo
bio
admins {
id
name
title
bio
createdAt
updatedAt
owner
__typename
}
classes {
nextToken
__typename
}
learners {
nextToken
__typename
}
createdAt
updatedAt
__typename
}
nextToken
__typename
}
}
Store error: the application attempted to write an object with no provided id but the store already contains an id of ModelInstitutionConnection:undefined for this object. The selectionSet that was trying to be written is:
listInstitutions(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
location
city
website
phone
logo
bio
admins {
id
name
title
bio
createdAt
updatedAt
owner
__typename
}
classes {
nextToken
__typename
}
learners {
nextToken
__typename
}
createdAt
updatedAt
__typename
}
nextToken
__typename
}
at new ApolloError (ApolloError.js:37)
at QueryManager.js:326
at QueryManager.js:698
at Array.forEach (<anonymous>)
at QueryManager.js:697
at Map.forEach (<anonymous>)
at QueryManager.push.lq9a.QueryManager.broadcastQueries (QueryManager.js:692)
at QueryManager.js:275
at ZoneDelegate.invoke (zone-evergreen.js:372)
at Object.onInvoke (core.js:28510)
我必须注意,当我将 cacheOptions 配置添加到 AWS Appsync 客户端定义时,此错误会消失,如下所示:-
export const client = new AWSAppSyncClient({
url: awsconfig.aws_appsync_graphqlEndpoint,
region: awsconfig.aws_appsync_region,
auth: {
type: awsconfig.aws_appsync_authenticationType,
jwtToken: async () =>
(await Auth.currentSession()).getAccessToken().getJwtToken(),
},
cacheOptions: {
dataIdFromObject: (obj: any) => `${obj.__typename}:${obj.myKey}`,
},
});
但即使错误消失了,它实际上并没有从 dynamoDB 中获取项目。它总是返回一个空数组。
我不知道为什么会出现这种错误,即使我的所有 graphql 代码都是使用 aws amplify-cli 自动生成的,并且我正在遵循 documentation 中看到的方法
我只想让查询从数据库中获取项目。我该怎么办?
【问题讨论】:
标签: amazon-web-services graphql aws-amplify aws-appsync