Flutter aws amplify 在调用 graphql 时不返回数据 api

Flutter aws amplify not returning data when calling graphql api

单击按钮时,我已编程调用连接到 Lambda 函数的 graphql api,该函数正在从 dynamodb table 中提取数据。查询不会产生任何错误,但也不会给我任何结果。我还检查了 cloudwatch 日志,但没有看到任何被调用函数的痕迹。不确定我在这里犯的粗心错误。

这是我的 api

void findUser() async {
  try {
    String graphQLDocument = '''query getUserById($userId: ID!) {
    getUserById(userId: $id) {
    id
    name
  }
}''';

    var operation = Amplify.API.query(
        request: GraphQLRequest<String>(
            document: graphQLDocument,
            variables: {'id': 'USER-14160000000'}));

    var response = await operation.response;
    var data = response.data;

    print('Query result: ' + data);
  } on ApiException catch (e) {
    print('Query failed: $e');
  }
}

这是我的 lambda 函数 -

const getUserById = require('./user-queries/getUserById');
exports.handler = async (event) => {
   var userId = event.arguments.userId;
   var name = event.arguments.name;
   var avatarUrl = event.arguments.avatarUrl;
   //console.log('Received Event - ', JSON.stringify(event,3));
   console.log(userId);
   switch(event.info.fieldName) {
       case "getUserById": 
           return getUserById(userId);
   }
 };

const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'ca-central-1'});

async function getUserById(userId) {
    const params = {
        TableName:"Bol-Table",
        KeyConditionExpression: 'pk = :hashKey and sk = :sortKey',
        ExpressionAttributeValues: {
            ':hashKey': userId,
            ':sortKey': 'USER'
        }
    };
    try {
        const Item = await docClient.query(params).promise();
        console.log(Item);
        return { 
            id: Item.Items[0].pk, 
            name: Item.Items[0].details.displayName,
            avatarUrl: Item.Items[0].details.avatarUrl,
            createdAt: Item.Items[0].details.createdAt,
            updatedAt: Item.Items[0].details.updatedAt
        };
    } catch(err) {
        console.log("BOL Error: ", err);
    }
 }
 
 module.exports = getUserById;

单击按钮后我得到了这个

您的 flutter 代码工作正常,但在 aws 的 lambda 中返回空白字符串“”以不打印任何内容

将我的评论移至答案:

能否尝试将 graphQLDocumnet 更改为

String graphQLDocument = '''query getUserById($id: ID!) {
     getUserById(userId: $id) { 
            id
            name
     }
}'''; 

您的变量是 $userId,然后是 $id。尝试在变量对象中的两个地方都调用它 $id