Overview
Time to put our federated setup to work for us! We'll get some data from the accounts
subgraph, and use it to calculate the value of a field in our messages
subgraph.
Reviewing the User
entity
In schema.graphql
, we'll find the current definition of the User
entity.
type User @key(fields: "id") {id: ID!}
Add the isOnline
field
Next, we want our messages
subgraph to resolve a new field called isOnline
. Let's add that field to our User
type in src/schema.graphql
.
type User @key(fields: "id") {id: ID!"The status indicating whether a user is online"isOnline: Boolean!}
Remember, we wanted to use two fields from the accounts
subgraph to help us resolve this field: isLoggedIn
and lastActiveTime
. We'll add the @requires
directive, and specify those as the fields we need.
type User @key(fields: "id") {id: ID!"The status indicating whether a user is online"isOnline: Boolean! @requires(fields: "isLoggedIn lastActiveTime")}
And because we're referencing external fields, we need to include them on our User
definition here as fields marked with the @external
directive.
type User @key(fields: "id") {id: ID!"The status indicating whether a user is online"isOnline: Boolean! @requires(fields: "isLoggedIn lastActiveTime")"The last recorded activity timestamp of the user"lastActiveTime: String @external"Whether or not a user is logged in"isLoggedIn: Boolean! @external}
We've made changes to our schema, so let's publish those changes to our graph in Studio!
rover subgraph publish APOLLO_GRAPH_REF --name messages --schema ./src/schema.graphql
Now when we return to Explorer, we can build (but not yet run) a query using the isOnline
field. (This is SubscribeToMessagesInConversationIncludeOnline
in your Operation Collection.)
subscription SubscribeToMessagesInConversationIncludeOnline($listenForMessageInConversationId: ID!) {listenForMessageInConversation(id: $listenForMessageInConversationId) {textsentTimesentTo {isOnline}}}
But actually building out the resolver for User.isOnline
is a job for you to tackle on your own!
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.