Nested aggregation filter / Value Propagation

I’m trying to create a chat system. I want to show the number of new messages (unread). Each time a user reads a message, I update a field called readTime with the current time. This way I can count the number of messages that come after that time.

There can be multiple channels in a group. I would like to show the number of unread messages for each channel based on the readTime of each channel. Also, I would like to show the total number of unread messages for the entire group.

The following query works when there is a single channel in the group and errors when there is more:

gt expects only 1 argument. Got: [ ]

Any suggestions would be appreciated!


{
		q(func: type(ChannelMember))
		@filter(uid_in(ChannelMember.group,0x3347bd) and uid_in(ChannelMember.user,0x2e631))  {
      readTime : RT as  ChannelMember.readTime
			channel :  ChannelMember.channel { 
        uid 
        newMessages : count(Channel.messages) @filter(gt(Message.createdAt,val(RT)))
        messages : Channel.messages (first: 1, orderdesc: Message.createdAt) {
		    	id : uid
		        author : Message.author {
		        	id : uid
		        }
		        msg : Message.msg
		        createdAt : Message.createdAt
		    }
		    members : Channel.members {
		    	id : uid
		        user : ChannelMember.user {
		        	id : uid
		            firstName : User.firstName
		            lastName : User.lastName
		            photoURL : User.photoURL
		        }
		        joined : ChannelMember.joined
		    }
      }
		}
}

Schema?

Removed extra fields for brevity:

type User  {
  id:ID!
	     
  groups: [Group] @hasInverse(field:creator) 
  
}

type Group  {
  id:ID!
  creator: User

  channels:[Channel] @hasInverse(field:group)
}

type ChannelMember {
  id:ID!
  user:User
  joined:DateTime
  channel:Channel 
  group:Group 
  readTime:DateTime @search  
}

type Channel {
  id:ID!  
  group:Group 
  members:[ChannelMember] @hasInverse(field:channel) 
  messages:[Message] @hasInverse(field:channel)
}

type Message {
  id:ID!
  channel:Channel 
  author:User 
  msg:String @search(by: [fulltext])
  createdAt:DateTime @search
}
1 Like

You have to use multiple blocks. As you can’t always use a variable from the same block. I have created this Promise a nested block (under construction - I'm still working in the use case) related to this topic.

Thanks Michel! I’ll take a look