AWS Amplify Graphql - TypeError: Must provide Source. Received: undefined

雨燕双飞 提交于 2020-01-14 04:03:05

问题


I am doing my queries exactly as in the documentation (https://aws-amplify.github.io/docs/js/api#amplify-graphql-client) but I keep receiving the following error:

TypeError: Must provide Source. Received: undefined

The query is as follows:

export const listUsers = `query ListUsers(
  $filter: ModelUserFilterInput
  $limit: Int
  $nextToken: String
) {
  listUsers(filter: $filter, limit: $limit, nextToken: $nextToken) {
    items {
      id
      firstName
      prefix
      lastName
      phone
      cigarettesDay
      enableNotifications
      age
      coach {
        id
        name
      }
      stopDate {
        nextToken
      }
      notifications {
        nextToken
      }
    }
    nextToken
  }
}
`;

Using API.graphql I call the query as follows:

  async componentDidMount() {
    try {
      const result = await API.graphql(graphqlOperation(listUsers));
      console.log(result);
    } catch (err) {
      console.log(err);
    }
  }

According to this issue https://github.com/graphql/graphql-js/issues/1038 I need to add a source, but I cannot find any example or documentation on how to do that. Any help is much appreciated!

Edit

It worked when I did it as follows:

 async componentDidMount() {
   try {
     const result = await API.graphql(graphqlOperation(queries.listUsers));
     console.log(result);
   } catch (err) {
     console.log(err);
   }
 }

And imported it as import * as queries from '../../../graphql/queries';

Why?

Solution

It was because I imported the query as

import listUsers from '../../../graphql/queries';

Instead of

import { listUsers } from '../../../graphql/queries';

来源:https://stackoverflow.com/questions/54457878/aws-amplify-graphql-typeerror-must-provide-source-received-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!