AWS Amplify Graphql - 类型错误:必须提供源。收到:未定义

Fem*_*mke 7 api amazon-web-services graphql aws-amplify

我正在按照文档(https://aws-amplify.github.io/docs/js/api#amplify-graphql-client)进行查询,但我一直收到以下错误:

TypeError: Must provide Source. Received: undefined
Run Code Online (Sandbox Code Playgroud)

查询如下:

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
  }
}
`;
Run Code Online (Sandbox Code Playgroud)

使用API.graphqlI 调用查询如下:

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

根据这个问题https://github.com/graphql/graphql-js/issues/1038我需要添加一个source,但我找不到任何关于如何做到这一点的示例或文档。任何帮助深表感谢!

编辑

当我这样做时它工作如下:

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

并将其导入为 import * as queries from '../../../graphql/queries';

为什么?

解决方案

这是因为我将查询导入为

import listUsers from '../../../graphql/queries';
Run Code Online (Sandbox Code Playgroud)

代替

import { listUsers } from '../../../graphql/queries';
Run Code Online (Sandbox Code Playgroud)