8. Codealong - Requiring external fields
10m

Overview

Time to put our federated setup to work for us! We'll get some data from the accounts , and use it to calculate the value of a in our messages .

A visual of the router subscribing to the messages subgraph, but fetching from accounts as well

Reviewing the User entity

In schema.graphql, we'll find the current definition of the User .

type User @key(fields: "id") {
id: ID!
}

Add the isOnline field

Next, we want our messages to resolve a new called isOnline. Let's add that 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 from the accounts to help us resolve this : isLoggedIn and lastActiveTime. We'll add the @requires , and specify those as the 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 , we need to include them on our User definition here as marked with the @external .

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 in Studio!

rover subgraph publish APOLLO_GRAPH_REF --name messages --schema ./src/schema.graphql
Task!

Now when we return to Explorer, we can build (but not yet run) a using the isOnline . (This is SubscribeToMessagesInConversationIncludeOnline in your Collection.)

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

But actually building out the for User.isOnline is a job for you to tackle on your own!

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.