Fetch public todos - subscription

Now let's define the subscription query to get notified about new public todos

Open src/components/Todo/TodoPublicListSubscription.svelte and add the following code.

import { gql } from "@apollo/client";
import { subscribe } from "svelte-apollo";
subscription notifyNewPublicTodos {
where: { is_public: { _eq: true } }
limit: 1
order_by: { created_at: desc }
) {
const todos = subscribe(NOTIFY_NEW_PUBLIC_TODOS);

What does the Subscription do?

The query fetches todos with a simple condition; is_public must be true. We also limit the number of todos to 1, since we would just like to get notified whenever a new todo comes in. We sort the todos by its latest created_at time according to the schema. We specify which fields we need for the todos node.

We already have the TodoPublicList component which renders the list of public todos. So let's pass latestTodo as a prop to that component.

{#if $todos.loading}
<div>Loading ...</div>
{:else if $todos.error}
{:else if $todos.data}
latestTodo={$todos.data.todos.length ? $todos.data.todos[0] : null}

Get Started with GraphQL Now

Hasura Cloud gives you a fully managed, production ready GraphQL API as a service to help you build modern apps faster.
Ready to get started?
Start for free on Hasura Cloud or you could contact our sales team for a detailed walk-through on how Hasura may benefit your business.
Stay in the know
Sign up for full access to our community highlights, new features, and occasional baby animal gifs! Oh, and we have a strict no-spam rule. โœŒ๏ธ