1

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

2
  • 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. Commented Nov 4, 2021 at 4:53
  • @Andrija you are right. can you put your comment as an answer please? Commented Nov 5, 2021 at 17:24

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Sorry Ruchit, didn't quite get what you said. I am returning the Id value on the lambda. Could you elaborate with some code to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.