Sumchans
Sumchans

Reputation: 3774

Flutter aws amplify not returning data when calling graphql api

On button click I have programmed to call a graphql api which is connected to a Lambda function and the function is pulling data from a dynamodb table. The query does not produce any error, but it doesn't give me any results as well. I have also checked the cloudwatch logs and I dont see any traces of the function being called. Not sure on the careless mistake I am making here.

Here is my 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');
  }
}

Here is my lambda function -

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;

Upon button click I get this enter image description here

Upvotes: 1

Views: 543

Answers (2)

Andrija
Andrija

Reputation: 1899

Moving my comment to an answer:

Can you try changing your graphQLDocumnet to

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

Your variable is $userId and then $id. Try calling it $id in both places like in your variables object.

Upvotes: 2

Ruchit
Ruchit

Reputation: 2700

Your flutter code is working fine but in lambda from the aws is returning blank string "" to not to print anything

Upvotes: 1

Related Questions