How to write JOIN in graphQL or get result from multiple types - AWS App sync iOS

2.2k Views Asked by At

I am using AWS AppSync for a chat app in one of the my applications. We are able to do setup and basic query successfully.

In one of the case I need to write a customized GraphQL query so that I can have additional data using reference of one type from another. For example, I can have allMessageGroup from a user and also allMessages from a particular group.

Now I want to add the last message in the group and its sender with the list of all message group just like what's app home page.

But I am not able to understand how make JOIN or write such query which give mixed results based on Conversation/Message/User types/table.

Platform:iOS Language: Swift

For detail below is my Schema and API/Query I am using

Schema

type Conversation {
  conversation_cover_pic: String
  conversation_type: String!
  createdAt: String
  id: ID!
  messages(after: String, first: Int): MessageConnection
  name: String!
  privacy: String
}
type Message {
  author: User
  content: String!
  conversationId: ID!
  createdAt: String
  id: ID!
  recipient: User
  sender: String
}
type MessageConnection {
  messages: [Message]
  nextToken: String
}

Query

query getUserConversationConnectionThroughUser($after: String, $first: Int)
{
    me
    {
        id
        __typename
        conversations(first: $first, after: $after)
        {
            __typename
            nextToken
            userConversations
            {
                __typename
                userId
                conversationId
                associated
                {
                    __typename
                    userId
                }
                conversation
                {
                    __typename
                    id
                    name
                    privacy
                    messages
                    {
                        __typename
                        id
                        conversationId
                        content
                        createdAt
                        sender
                        isSent
                    }
                }
            }
        }
    }
}
1

There are 1 best solutions below

6
On

It sounds like you need multiple requests to one or more datasources to fulfill this graphQL query. In this case, you can use AppSync's pipeline resolver feature.

With pipeline resolvers, you can create multiple functions, each of which can use the results of the previous function and query a database. These functions run in an order you specify.

An example of something you could do with a pipeline resolver:

  1. One function will query the chat group database
  2. A second function will use the results of the chat group to fetch messages
  3. Consolidate all the results into one graphQL response containing group information and messages

Here is the documentation for pipeline resolvers: https://docs.aws.amazon.com/appsync/latest/devguide/pipeline-resolvers.html