Overview
In the final stretch! Let's write the resolver for the User.isOnline
field in our messages
subgraph.
🎯 Goal 1: Implement the resolver function for User.isOnline
field, using the other User
fields isLoggedIn
and lastActiveTime
Create the isOnline
resolver
- Jump into
resolvers/User.ts
. Create some space for a new resolver function for theisOnline
field.
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: () => {},},};
- The value returned by
__resolveReference
is passed into this function as itsparent
argument. We've asked the router to provide theisLoggedIn
andlastActiveTime
field values on our entity representation, so we can destructure theparent
argument for those values here.
isOnline: ({ isLoggedIn, lastActiveTime }) => {},
- 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 resolver, 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 subscription, 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 operation collection.
mutation ToggleUserLoggedIn {changeLoggedInStatus {timesuccessmessage}}
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 mutation, 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 subscription operation again (SubscribeToMessagesInConversationIncludeOnline
in your Operation 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) {textsentTimesentTo {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 subscription. (This time we'll be sure to include the isOnline
field!)
mutation SendMessageToConversationIncludeOnline($message: NewMessageInput!) {sendMessage(message: $message) {textsentTo {idnameisOnline}}}
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 mutation 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 operation tab with the mutation ToggleUserLoggedIn
.
Let's run it again, making sure to update our Headers to log out user "ripley".
mutation ToggleUserLoggedIn {changeLoggedInStatus {timesuccessmessage}}
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
operation, 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 subscription 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}}}
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.