前提
amplify add apiでgraphqlを追加して、
const todos = await API.graphql(graphqlOperation(queries.listTodos));
console.log(todos)
のようなコードでAPIを呼び出すときに、
"TypeError: Must provide Source. Received: undefined"
のエラーが出た場合の対処について。
対応
結論、サンプルコピペのままで、存在しないクエリを設定してました。。
queries.listTodosじゃなくて、別のだった。
追記
またおんなじエラーでハマった。。
export const createAnswers = `mutation CreateAnswers(
$input: CreateAnswersInput!
$condition: ModelAnswersConditionInput
) {
...
今回は、生成されたコードが上記のような場合に、
await API.graphql(graphqlOperation(mutations.CreateAnswers, {input: result}));
だとダメで、
await API.graphql(graphqlOperation(mutations.createAnswers, {input: result}));
が正解ですね。
何が違うかというと、createのcが大文字か小文字かの違いです。いずれにしても名前の間違いなんですが、間違って"`mutation CreateAnswers("の方をコピペしてました。正しくは"export const createAnswers"の方ですね。
というわけで
名前を間違えないようにしましょう。エラーメッセージが大変わかりづらいので、もうちょっとがんばってほしいところです。