我试图在NodeJS环境中使用AppSync与aws-sdk
运行一个简单的突变。我犯了一个错误,但我不知道是怎么回事。这个查询在我的AppSync控制台工作:
mutation MyMutation {
createApiKey(input: {key: "testconsole2"}) {
id
key
}
}
然后当我在NodeJS中尝试它时,我无法让它工作(尽管我所有其他类似的查询工作):
async function createApiKey({ encryptedKey }: { encryptedKey: string }) {
const createApiKeyQueryString = `mutation createApiKey(
$key: String!,
){
createApiKey(
input: {
key: $key
}
){
key
}
}`;
try {
const runMutation = await appSyncClient.mutate({
mutation: gql(createApiKeyQueryString),
variables: {
key: encryptedKey,
},
});
console.log({
response: runMutation // Throws error before we reach here
});
return (runMutation as { data }).data;
} catch (error) {
console.log(error); // ApolloError: GraphQL error: Validation error of type FieldUndefined: Field 'createApiKey' in type 'Mutation' is undefined @ 'createApiKey
}
}
我犯了一个愚蠢的错误,但我不知道我做错了什么,或者错误信息是什么意思?
我使用了错误的AppSync URL(例如"https://.appsync-api.eu-west-2.amazonaws.com/graphql")。
这意味着我的ApiKey表的模式名不存在,这就是下面试图告诉我的:
ApolloError: GraphQL error: Validation error of type FieldUndefined:
Field 'createApiKey' in type 'Mutation' is undefined @ 'createApiKey
基本上,因为我没有从我的amplify.graphql
文件部署我的ApiKey模式,Amplify没有生成相关的AppSync调用(即'createApiKey')。因此,当我试图用我的NodeJS代码调用调用时,AppSync告诉我它是未定义的。