3

I am using aws-amplify in my react-native application for user authentication. it works fine.

modules used for aws amplify
aws-amplify: "^3.0.23", aws-amplify-react-native: "^4.2.4"

Now I would like to connect the app or retrieve the data from a DynamoDB table and use that data in my react native application.

How can I Access data from Dynamodb tables in react native using aws-amplify is there any get methods or rest APIs to retrieve data from dynamodb.

How can I establish a connection with dB to my react-native application.

Thanks in advance.

Pratap Penmetsa
  • 1,137
  • 17
  • 41
  • Have you seen the Amplify tutorial on building a full stack React Native app? https://docs.amplify.aws/start/q/integration/react-native – Seth Geoghegan May 28 '21 at 17:16
  • no, not yet i will go through it @SethGeoghegan I referred https://docs.aws.amazon.com/aws-mobile/latest/developerguide/mobile-hub-aws-mobile-react-native.html – Pratap Penmetsa May 30 '21 at 04:47

2 Answers2

0

I used

aws-sdk/dist/aws-sdk-react-native

var AWS = require('aws-sdk/dist/aws-sdk-react-native');

AWS.config.update({
  region: 'your region',
  dynamoDbCrc32: false
});
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'your identityPool Id'
});
const gp = AWS.config.credentials.getPromise();

gp.then(() => {
    const documentClient = new AWS.DynamoDB.DocumentClient();
    const params = {
      TableName: "table name",
      Key: "your key",
    };
documentClient.get(params, function(err, data) {
 if (err){
   console.log(err)
  } else {
   console.log(data)
 }
 }
Pratap Penmetsa
  • 1,137
  • 17
  • 41
0

I m using graphql api and when i create any model, aws automatically creates queries and mutations for that.

Let's say i have this model in schema json.

type ExpoTicketsObject
    @model
    @auth(rules: [{ allow: private, provider: iam, operations: [read, create, update, delete] }]) {
    id: ID!
    tickets: AWSJSON!
}

here are related query and mutation:

enter image description here

in lambda function i defined the query:

const appsync = require("aws-appsync");
const gql = require("graphql-tag");
// inorder to use fetch api in node, we need this polyfill
require("cross-fetch/polyfill");

const getExpoToken = gql`
    query getExpoToken($token: String!) {
        # token is the primary key.
        getExpoToken(token: $token) {
            id
            token
            playerUsername
        }
    }
`;

then I call it:

const tokenRes = await graphqlClient.query({
        query: getExpoToken,
        variables: { token: event.arguments.token }
    });
Yilmaz
  • 35,338
  • 10
  • 157
  • 202