3
0

More than 3 years have passed since last update.

AWS AmplifyのAPI.graphqlで"TypeError: Must provide Source. Received: undefined"

Last updated at Posted at 2020-01-25

前提

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"の方ですね。

というわけで

名前を間違えないようにしましょう。エラーメッセージが大変わかりづらいので、もうちょっとがんばってほしいところです。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0