How to grant access rights to a Room for exchange of Messages among Users

We have seen two scenarios to provide access rights to different users.

In this post, we will see how to grant access rights to a Room for exchange of Messages among a group of Users.

Instead of providing separate Access Rights to each user, the system admin grants one permission for the entire room.

Create a data type called Room.

type Room {
  messages: [Message!]
}

type Message {
  text: String
}

A user (User1) creates a new Room. (Make sure to login User1 before creating the Room)

mutation {
  upsert(values: { Room: [{ messages: { text: "Test" } }] }) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01G2VZDYQERG8CA56VQDQ3MZVS"
      }
    ]
  }
}

System admin grants permission to User2 to execute Mutation.link and Query.*.
Only system admin can grant this access. And system admin is the account that has created the instance.

mutation {
  upsert(
    values: {
      AccessRight: [
        {
          resource: "01G2VZDYQERG8CA56VQDQ3MZVS"
          resourceType: "Room"
          operationType: "Mutation"
          operation: "link"
          permissionType: RBP
          resourceOwnerId: "01G1TH09DPVGFPP8W5ZYC1AC81"
          approved: true          
          members: [
            { hypi: { id: "01G1TH9D0PY4GG7RJDMDTXAG5F" } }
          ]
        },
        {
          resource: "01G2VZDYQERG8CA56VQDQ3MZVS"
          resourceType: "Room"
          operationType: "Query"
          operation: "*"
          permissionType: RBP
          resourceOwnerId: "01G1TH09DPVGFPP8W5ZYC1AC81"
          approved: true
          members: [
            { hypi: { id: "01G1TH9D0PY4GG7RJDMDTXAG5F" } }
          ]
        }
      ]
    }
  ) {
    id
  }
}

System Admin needs to set resource owner id while creating the Access Right. The Resource Owner is the one who created the resource.

Add group members to the members field. Here, just one hypi.id of the Account has been provided. You may add more user accounts.

Now, User2 creates a Message and then links it to the Room.

mutation {
  upsert(values: { Message: [{ text: "Test1" }] }) {
    id
  }
}
#result
{
  "data": {
    "upsert": [
      {
        "id": "01G2VP96HHHS7J7KHW88CAW4T3"
      }
    ]
  }
}

#Add message to the Room

mutation {
  link(
    from: Room
    to: Message
    via: "messages"
    whereFromID: "01G2VZDYQERG8CA56VQDQ3MZVS"
    andToID: "01G2VP96HHHS7J7KHW88CAW4T3"
  )
}

#result
{
  "data": {
    "link": true
  }
}

find query displays the exchange of the messages.

{
  find(type: Room, arcql: "hypi.id = '01G2VZDYQERG8CA56VQDQ3MZVS'") {
    edges {
      node {
        ... on Room {
          messages {
            text
          }
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "messages": [
              {
                "text": "Test1"
              },
              {
                "text": "Test"
              }
            ]
          },
          "cursor": "01G2VZDYQERG8CA56VQDQ3MZVS"
        }
      ]
    }
  }
}