9. Exercise - Define the isOnline resolver
10m

Overview

In the final stretch! Let's write the for the User.isOnline in our messages .

🎯 Goal 1: Implement the function for User.isOnline , using the other User isLoggedIn and lastActiveTime

Create the isOnline resolver

  1. Jump into resolvers/User.ts. Create some space for a new function for the isOnline .
import { Resolvers } from "../__generated__/resolvers-types";
export const User: Resolvers = {
User: {
__resolveReference: async ({ id, ...attributes }, { dataSources }) => {
const user = await dataSources.db.getUserDetails(id);
return { ...attributes, ...user };
},
isOnline: () => {},
},
};
  1. The value returned by __resolveReference is passed into this function as its parent . We've asked the to provide the isLoggedIn and lastActiveTime values on our representation, so we can destructure the parent for those values here.
isOnline: ({ isLoggedIn, lastActiveTime }) => {},
  1. Now let's add some logic. We'll check whether the user is logged in AND if they've been active in the last five minutes. (Take note: if you're not using the Prisma database for your , use the code in the collapsible below.)
const now = Date.now();
const lastActiveDate = new Date(lastActiveTime).getTime();
const difference = now - lastActiveDate;
if (isLoggedIn && difference < 300000) {
return true;
}
return false;

Test it out!

It's probably been more than five minutes since any of our users have been "active" (currently a property set when they're created in the database, or when they log in or out). So before we try out our , let's update our participants logged-in status.

Logging in our message recipient

Our first conversation, id "xeno-ripley-chat", is between users "xeno" and "ripley". We're going to change the online status of our chat recipient ("ripley"), so let's open up a new tab and access ToggleUserLoggedIn from our collection.

Toggle the user's logged in status
mutation ToggleUserLoggedIn {
changeLoggedInStatus {
time
success
message
}
}

To "log in" or "log out" as this user, we're going to need to update our Authorization header. In this tab only, change the header to specify Bearer ripley.

Authorization: Bearer ripley

Now when you run the , you should see a response indicating whether the user was logged in or out.

{
"data": {
"changeLoggedInStatus": {
"success": true,
"message": "User was successfully logged in",
"time": "1727144609031"
}
}
}

Starting the subscription

With our recipient online, let's try that again (SubscribeToMessagesInConversationIncludeOnline in your Collection). Open up a new tab, and run the following operation to subscribe to conversation "xeno-ripley-chat".

subscription SubscribeToMessagesInConversationIncludeOnline(
$listenForMessageInConversationId: ID!
) {
listenForMessageInConversation(id: $listenForMessageInConversationId) {
text
sentTime
sentTo {
isOnline
}
}
}

And in the Variables panel:

{
"listenForMessageInConversationId": "xeno-ripley-chat"
}

And in your Headers panel, we want to switch back to "xeno":

Authorization: Bearer xeno

Sending a new message

Let's send a message to come through on our . (This time we'll be sure to include the isOnline !)

mutation SendMessageToConversationIncludeOnline($message: NewMessageInput!) {
sendMessage(message: $message) {
text
sentTo {
id
name
isOnline
}
}
}

And in the Variables panel:

{
"message": {
"text": "Hey, so, I've taken care of all your friends. Where are you?",
"conversationId": "xeno-ripley-chat"
}
}

And in the Headers panel:

Authorization: Bearer xeno

When we run the to send the message, we should see a response - and our participant should show as being "online"!

Logging the recipient out

Jump back to your tab with the ToggleUserLoggedIn.

Let's run it again, making sure to update our Headers to log out user "ripley".

Toggle the user's logged in status
mutation ToggleUserLoggedIn {
changeLoggedInStatus {
time
success
message
}
}

And in the Headers panel:

Authorization: Bearer ripley

Now that the user's logged out, let's send another message.

Back in the tab opened to the SendMessageToConversationIncludeOnline , update your message contents and let's send an update.

In the Variables panel:

{
"message": {
"text": "You're not ignoring me, are you?",
"conversationId": "xeno-ripley-chat"
}
}

And with that, we should see that our has updated two bits of data: we see the details about the message, as well as the fact that our recipient is now offline! We weren't necessarily subscribing to their online/offline status, but we get the update anyway.

{
"listenForMessageInConversation": {
"text": "You're not ignoring me, are you?",
"sentTime": "1727208930220",
"sentTo": {
"isOnline": false
}
}
}
Previous

Share your questions and comments about this lesson

Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.