8. Federated Subscriptions
15m

Overview

In this section, we'll cover:

  • how to leverage Federated to enable real-time data use cases
  • how to enrich real-time data via Federation

Prerequisites

  • Our running in the cloud

Federated subscriptions

enable clients to receive continual, real-time updates whenever new data becomes available. Unlike queries and , subscriptions are long-lasting. This means a client can receive multiple updates from a single subscription:

Action flow for subscriptions and real-time data delivery

are best suited to apps that rely on frequently changing, time-sensitive data (examples include: incoming reviews, stock prices, IoT sensor readings, live chat, or sports scores).

Federated subscriptions: KBT Threads

At KBT threads we have new product reviews being posted almost every second. There is a desire to showcase the latest reviews in real time.

While this sounds straightforward, there are questions about enriching the real-time data to show human-readable products and the user information that left the review. To show the real-time review data we will leverage Federated .

How it works

We have already done the work to send a review event from our NoSQL database to our products whenever a review is added. A review event is triggered every time a new review is posted. This comes from our NoSQL database to our product subgraph in a JSON format.

An example of a review event looks like this:

{
"id": 1,
"body": "This was a great find! would highly recommend.",
"product": {
"id": "143"
},
"user": {
"id": "10"
}
}

For us to show relevant data about this review, we need to enrich this event with data from our products and our users .

We will do this by creating a federated . This will enable us to return the review information in the event, such as the review body. It will also allow us to provide detailed information that was not included in the event, such as product name, product description, product images, user first name, and user last name.

✏️ 1. Open the products-schema.graphql file: in GitHub, click the products-schema.graphql file.

https://github.com

GitHub page with the products-schema.graphql file selected

Click the ✏️ pencil edit button on the top-right of the page to begin editing the file.

https://github.com

GitHub view of the products-schema.graphql file with pencil button highlighted

✏️ 2. Add the Review type.

In the GitHub text editor find the comment that says "#ADD REVIEW FIELDS AND RETURN TYPES HERE". It should be on line 142. Add the and return types for the Review type. The review should include an id, body, the product that was reviewed, and the user that created that review.

Your Review type should look like this:

products-schema.graphql
type Review @key(fields: "id") {
id: Int!
body: String!
product: Product!
user: User!
}

✏️ 3. Add the Subscription type

Now that we have a review type in our schema, we can add a that is triggered each time a review is added to our database.

In the GitHub text editor find the comment that says "#ADD SUBSCRIPTION FIELDS AND RETURN TYPES HERE". It should be on line 153. Add the and return type for the . The subscription should include a field called reviewAdded and should return a Review type.

products-schema.graphql
type Subscription {
reviewAdded: Review
}

✏️ 4. Commit changes

Click the Commit changes button in the upper right of the text editor.

https://github.com

GitHub view of the products-schema.graphql file with Commit button highlighted

Subscribing to live reviews

Now that we have added our to the schema, it is time to see it come through in real time. Navigate to Explorer in by selecting your graph and clicking the Explorer on the left-hand side:

GraphOS Studio view of home page with Explorer button highlighted

In Explorer, make a new request that retrieves the reviews. Include additional such as the name of the user who made the review, the product name, description, and price.

You can build this by selecting each you want present in the :

GraphOS Studio view of Explorer with subscription

Alternatively, you can copy and paste this into the Explorer window.

subscription ReviewSubscription {
reviewAdded {
id
body
product {
name
shortDescription
price
}
user {
firstName
lastName
}
}
}

Now trigger the by clicking the Review Subscription button in the upper middle of the page.

Studio view of Explorer with send button highlighted

Connection Settings view with router URL highlighted

On the right-hand side, you should see live reviews coming into your explorer. You may have to expand the window by clicking and dragging the bar up towards the top.

Studio view of results streaming

Subscriptions checklist

Conclusion

🎉 Congratulations on completing this lab! 🎉 This is a big step. You have used GraphQL subscriptions and taken it a step further by leveraging Federated Subscriptions! Now you can add Federated Subscriptions to your supergraphs in the future.

Conclusion

Congratulations on successfully completing this workshop! 🎉🎉🎉

Throughout this journey, we have now added:

  • ,
  • authentication and authorization,
  • a coprocessor,
  • limits,
  • ,
  • and safelisting!

We did all of this with either schema changes or yaml configuration. With those simple changes, we were able to create a that serves more use cases and does so in a more secure way.

Beyond that, we have also begun the process of detailing how we can manage schema changes for a across multiple stakeholders to ensure our graph remains highly available.

KBT Threads can now confidently deploy their to support their new omni-channel presence!

Previous