存储错误:应用程序尝试写入未提供 ID 的对象,但存储已包含<connectionName>此对象的 ID



注意-我是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-amplift-cli自动生成的,并且我遵循文档中的方法

我只想让查询从数据库中获取项目。我该怎么办?

我想明白了。问题出现在GraphQLSchema定义中,我将@auth参数设置为只允许某个管理员访问列表,这就是我返回空数组的原因。我想这个特殊的错误是由于我没有在客户端定义中的cacheoptions中留下。问题末尾指定的cacheoptions将解决此问题。

最新更新