amplify-js icon indicating copy to clipboard operation
amplify-js copied to clipboard

Proper Way to Combine / Consolidate GraphQL Queries in DynamoDB - Custom Amplify Resolver? - React Native

Open ChristopherGabba opened this issue 1 year ago • 5 comments

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

GraphQL API

Amplify Version

v6

Amplify Categories

api

Backend

Amplify Gen 2 (Preview)

Environment information

# Put output below this line


Describe the bug

I'm really searching for best practices to improve on API efficiency with Amplify.

I am using the Api GraphQL to query specific messages in DynamoDB (refer to simplified code below in reproducible demo). I would very much like to combine these queries into a single GraphQL query that's just called "listUnreadMessagesForUser". I call this API a lot within the app and if I can combine into a single call (instead of 2), it will increase the app efficiency.

To be clear, this code below works great and functions, so this is not a bug. I just don't like how I have to combine two different GraphQL Queries everywhere in the app.

Expected behavior

Can you provide a step-by-step or guidance how I can improve this API call in the proper manner to be a single call?

Reproduction steps

N/A. Refer to code below.

Code Snippet

Here is my function that uses (2) different queries to look for messages that:

  1. Are received by current user but not yet viewed
  2. Are sent by current user but not yet viewed
 async fetchUnreadMessagesFromDB() {
      try {
        const currentUser = await getCurrentUser()

        const client = generateClient()
        const [unreadReceived, unreadReturned] = await Promise.all([
          client.graphql({
            query: queries.messagesByReceiverId,
            variables: {
              receiverId: currentUser.userId,
              filter: {
                viewedTimestamp: { attributeExists: false },
              },
            },
          }),
          client.graphql({
            query: queries.messagesBySenderId,
            variables: {
              senderId: currentUser.userId,
              filter: {
                and: [
                  { viewedTimestamp: { attributeExists: true } },
                ],
              },
            },
          }),
        ])

        return newUnreadResults = [
          ...unreadReceived.data.messagesByReceiverId.items,
          ...unreadReturned.data.messagesBySenderId.items,
        ]
}

Here is my schema:

type Message @model @auth(rules: [{allow: public}]) {
  id: ID!
  viewedTimestamp: AWSDateTime
  body: String
  senderId: ID! @index(name: "bySender")
  sender: User! @belongsTo(fields: ["senderId"])
  receiverId: ID! @index(name: "byReceiver")
  receiver: User! @belongsTo(fields: ["receiverId"])
}

Here are both the queries:

export const messagesBySenderId = /* GraphQL */ `query MessagesBySenderId(
  $senderId: ID!
  $sortDirection: ModelSortDirection
  $filter: ModelMessageFilterInput
  $limit: Int
  $nextToken: String
) {
  messagesBySenderId(
    senderId: $senderId
    sortDirection: $sortDirection
    filter: $filter
    limit: $limit
    nextToken: $nextToken
  ) {
    items {
      id
      body
      viewedTimestamp
      senderId
      sender {
        id
        profileImage
        firstName
        lastName
        username
        birthdate
        phoneNumber
        searchTerm
        pushToken
        createdAt
        updatedAt
        __typename
      }
      receiverId
      receiver {
        id
        profileImage
        firstName
        lastName
        username
        birthdate
        phoneNumber
        searchTerm
        pushToken
        createdAt
        updatedAt
        __typename
      }
      createdAt
      updatedAt
      __typename
    }
    nextToken
    __typename
  }
}
` as GeneratedQuery<
  APITypes.MessagesBySenderIdQueryVariables,
  APITypes.MessagesBySenderIdQuery
>;
export const messagesByReceiverId = /* GraphQL */ `query MessagesByReceiverId(
  $receiverId: ID!
  $sortDirection: ModelSortDirection
  $filter: ModelMessageFilterInput
  $limit: Int
  $nextToken: String
) {
  messagesByReceiverId(
    receiverId: $receiverId
    sortDirection: $sortDirection
    filter: $filter
    limit: $limit
    nextToken: $nextToken
  ) {
       items {
      id
      body
      viewedTimestamp
      senderId
      sender {
        id
        profileImage
        firstName
        lastName
        username
        birthdate
        phoneNumber
        searchTerm
        pushToken
        createdAt
        updatedAt
        __typename
      }
      receiverId
      receiver {
        id
        profileImage
        firstName
        lastName
        username
        birthdate
        phoneNumber
        searchTerm
        pushToken
        createdAt
        updatedAt
        __typename
      }
      createdAt
      updatedAt
      __typename
    }
    nextToken
    __typename
  }
}
` as GeneratedQuery<
  APITypes.MessagesByReceiverIdQueryVariables,
  APITypes.MessagesByReceiverIdQuery
>;

Log output

N/A

aws-exports.js

N/A

Manual configuration

No response

Additional configuration

No response

Mobile Device

iPhone 12

Mobile Operating System

iOS 17

Mobile Browser

Safari

Mobile Browser Version

No response

Additional information and screenshots

No response

ChristopherGabba avatar Mar 14 '24 13:03 ChristopherGabba

Hello again, @ChristopherGabba 👋. Appreciate you providing details on your queries, schema, and code snippet already. At first glance, it sounds like Compound Filters might be able to accomplish what you're looking for to allow your queries to be combined into a single one with and, or, and not Boolean logic. Have you tried incorporating this yet?

Also, is your viewedTimestamp: AWSDateTime within your schema intended to have both the sender AND the receiver have only 1 shared value for viewedTimestamp?

cwomack avatar Mar 25 '24 19:03 cwomack

@ChristopherGabba, wanted to check in and see if you're still blocked by this or had a chance to review the above comment.

cwomack avatar Apr 01 '24 18:04 cwomack

@cwomack I think your Compound Filters link is broken and just takes me back to this issue page. Regardless, I think I found some information on the topic. I previously was using compound filters with multiple ors and ands but ran into an issue with the limit filter. Eventually I stumbled across using the secondary @index to improve your query efficiencies, although I'm no expert on this topic. That's why I eventually made two different queries using @index. Going back to compound filters is of course possible, but is that better than what I'm doing now as far as query efficiency (database reads / writes / etc.)?

ChristopherGabba avatar Apr 01 '24 18:04 ChristopherGabba

Hi @ChristopherGabba , generally leveraging secondary indexes are more efficient than using a list + compound filters because a list will trigger a Scan operation on the DynamoDB table whereas secondary indexes will perform a Query instead which is more cost effective.

Perhaps something like this might work for your use case?

Gen 2 Schema

  Message: a
    .model({
      senderId: a.id().required(),
      recieverId: a.id().required(),
      content: a.string().required(),
      status: a.string().required(),
      createdAt: a.datetime().required(),
    })
    .secondaryIndexes((index) => [
      index("senderId"),
      index("recieverId").sortKeys(["status", "createdAt"]),
    ]),

Client Query

image

chrisbonifacio avatar Apr 15 '24 18:04 chrisbonifacio

@chrisbonifacio Thanks for providing this, I actually had not looked into Gen 2's API structure.

What I'm looking for essentially is a way for me to query messages in the most efficient way possible. If that is with two different secondary index calls (as shown in my example above), then that is what I'm looking to do.

I was just wondering if there is an even more efficient way to perform my "double" query like:

clients.models.Message.listUsersByReceiverOrSenderId({
     receiverOrSenderId: "123", // this effectively queries both situations above
     senderFilter: {
         viewedTimestamp: { attributeExists: true },
     },
     receiverFilter: {
        viewedTimestamp: { attributeExists: false },
     }
})

My situation is kind of unique. Effectively, my app sends a media to the user, the user looks at that media, and then it sends that same media back with a response. So when I query the database table for the actual user on the app, I am checking to see all items that have been sent by the user, but have been responded to, OR a message that this particular user is supposed to respond to.

My end goal is just to make sure that at scale, I'm being as efficient as possible with my queries using secondary indexes etc. in order to get all of my data back to the user.

ChristopherGabba avatar Apr 17 '24 15:04 ChristopherGabba

Closing this as not a bug, but more of a question. While AppSync supports query batching, making use of it would require manually writing the graphql string. Using the client.graphql API, you can pass in a custom string to the query parameter to combine the calls into one query and thus one network call.

You can try this but it would still result in two separate operations in DynamoDB because each query maps to one can only map to one resolver which can only map to one DynamoDB operation.

Otherwise, in the Gen 2 client.models API, you would have to split up the queries into separate calls:

schema

  Message: a
    .model({
      senderId: a.string().required(),
      receiverId: a.string().required(),
      viewedTimeStamp: a.timestamp().required(),
      content: a.string().required(),
      status: a.string().required(),
      createdAt: a.datetime().required(),
    })
    .authorization((allow) => [
      allow.ownerDefinedIn("senderId"),
      allow.ownerDefinedIn("receiverId").to(["read"]),
    ])
    .secondaryIndexes((index) => [
      index("senderId").sortKeys(["viewedTimeStamp"]),
      index("receiverId").sortKeys(["viewedTimeStamp"]),
    ]),

queries

    const { data: recieveUnreadMessages } =
      await client.models.Message.listByReceiverIdAndViewedTimeStamp({
        receiverId: currentUser.userId,
        viewedTimeStamp: {
          attributeExists: false,
        },
      });

    const { data: sentUnreadMessages } =
      await client.models.Message.listBySenderIdAndViewedTimeStamp({
        senderId: currentUser.userId,
        viewedTimeStamp: {
          attributeExists: true,
        },
      });

    console.log({ recieveUnreadMessages, sentUnreadMessages });

Unfortunately DynamoDB only accepts one filter expression per Scan/Query so at least two different operations must occur.

chrisbonifacio avatar May 14 '24 17:05 chrisbonifacio