How to return values on custom mutation resolvers that returns relationships?

This is the mutation that I am running

mutation {
  createChat (
    recipientId: "336939678036918338"
  ) {
    _id
    createdBy {
      _id
    }
    status
    users {
      data {
        _id
        firstName
        lastName
        profilePicture
      }
    }
  }
}

This is the custom mutation definition

createChat(recipientId: String): Chat! @resolver(name: "createChat")

The Type Chat:

enum ChatStatus {
  pending
  accepted
  rejected
}

type Chat @collection(name: "chats") {
  createdBy: User!
  users: [User!] @relation(name: "chat_users")
  messages: [Message!] @relation
  status: ChatStatus!
}

the Type User:

enum Gender {
  Male
  Female
  NullGender
}

enum Relationship {
  Single
  InARelationship
  Married
}

type User @collection(name: "users") {
  email: String! @unique
  username: String
  firstName: String
  middleName: String
  lastName: String
  gender: Gender
  profilePicture: String
  relationshipStatus: Relationship
  chats: [Chat] @relation(name: "chat_users")
  partner: User @relation
  lastActiveStatusEnabled: Boolean
  lastActiveAt: String
}

The FQL resolver:

Update(Function("createChat"), {
  role: "admin",
  body: Query(
    Lambda(
      ["recipientId"],
      Let(
        {
          recipientRef: Ref(Collection('users'), Var('recipientId')),
          recipient: Get(Var('recipientRef')),
          chat: Create(
            Collection('chats'),
            {
              data: {
                createdBy: CurrentIdentity(),
                status: "pending"
              }
            }
          )
        },
        Do(
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: CurrentIdentity()
              }
            }
          ),
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: Var('recipientRef')
              }
            }
          ),
          Merge(
            Var('chat'),
            {
              data: Merge(
                Select(['data'], Var('chat')),
                {
                  users: Map(
                    Paginate(
                      Match(
                        Index("chat_users_by_chat"),
                        Select(["ref", "id"], Var("chat"))
                      )
                    ),
                    Lambda(
                      "ref",
                      Get(Var("ref"))
                    )
                  )
                }
              )
            }
          )
        )
      )
    )
  )
})

What I am trying to do here is create a custom mutation that creates a new chat entry and then return the chat record with the users relationship. Note that chat_users_by_chat index was autogenerated. It works as expected when I do Call('createChat', '336939678036918338') on the shell, but when I run the graphql mutation, the users is always empty.

Graphql mutation response:

{
  "data": {
    "createChat": {
      "_id": "338302878326194246",
      "createdBy": {
        "_id": "336937667143925826"
      },
      "status": "pending",
      "users": {
        "data": []
      }
    }
  }
}

FQL response via Call()

{
  ref: Ref(Collection("chats"), "338303236583718985"),
  ts: 1658889957920000,
  data: {
    createdBy: Ref(Collection("users"), "336937667143925826"),
    status: "pending",
    users: {
      data: [
        {
          ref: Ref(Collection("users"), "336937667143925826"),
          ts: 1658885827270000,
          data: {
            firstName: "April",
            lastName: "Pineda",
            birthdate: Date("1995-02-09"),
            gender: "Male"
          }
        },
        {
          ref: Ref(Collection("users"), "336939678036918338"),
          ts: 1658721296040000,
          data: {
            firstName: "Katheryn",
            lastName: "Winnick",
            gender: "Female",
          }
        }
      ]
    }
  }
}

I have also updated the roles and ensure that the user has read access to the index chat_users_by_chat.

How do I make the graphql mutation work?

I have tried making createChat return the Chat.

Update(Function("createChat"), {
  role: "admin",
  body: Query(
    Lambda(
      ["recipientId"],
      Let(
        {
          recipientRef: Ref(Collection('users'), Var('recipientId')),
          recipient: Get(Var('recipientRef')),
          chat: Create(
            Collection('chats'),
            {
              data: {
                createdBy: CurrentIdentity(),
                status: "pending"
              }
            }
          )
        },
        Do(
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: CurrentIdentity()
              }
            }
          ),
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: Var('recipientRef')
              }
            }
          ),
          Var('chat')
        )
      )
    )
  )
})

But the graphql response is still the same.

The solution:

Make createChat return the Chat instance

Update(Function("createChat"), {
  role: "admin",
  body: Query(
    Lambda(
      ["recipientId"],
      Let(
        {
          recipientRef: Ref(Collection('users'), Var('recipientId')),
          recipient: Get(Var('recipientRef')),
          chat: Create(
            Collection('chats'),
            {
              data: {
                createdBy: CurrentIdentity(),
                status: "pending"
              }
            }
          )
        },
        Do(
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: CurrentIdentity()
              }
            }
          ),
          Create(
            Collection('chat_users'),
            {
              data: {
                chatID: Select(['ref', 'id'], Var('chat')),
                userID: Var('recipientRef')
              }
            }
          ),
          Var('chat')
        )
      )
    )
  )
})

And the chatID should hold a Ref not just the ID.

Create(
  Collection('chat_users'),
  {
    data: {
      chatID: Select(['ref'], Var('chat')),
      userID: CurrentIdentity()
    }
  }
)

Thanks to @wallslide for answering it via Discord! Discord

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.