我正在完全按照文档(https://aws-amplify.github.io/docs/js/api#amplify-graphql-client)中的方法进行查询,但是我不断收到以下错误:
TypeError: Must provide Source. Received: undefined
查询如下:
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
}
}
`;
使用API.graphql
我按如下方式调用查询:
async componentDidMount() {
try {
const result = await API.graphql(graphqlOperation(listUsers));
console.log(result);
} catch (err) {
console.log(err);
}
}
根据此问题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);
}
}
并将其导入为import * as queries from '../../../graphql/queries';
为什么?
解决方案
这是因为我将查询导入为
import listUsers from '../../../graphql/queries';
代替
import { listUsers } from '../../../graphql/queries';