Building Subgraph UIs (Apollo Client)

This guide will teach you how to create a minimal, functional UI that queries blockchain data from a Somnia subgraph using Next.js, Apollo Client, and GraphQL.

Prerequisites

  • Basic knowledge of React and Next.js

  • Node.js installed (v16 or higher)

  • A deployed subgraph on Somnia (we'll use SomFlip as an example)

What You'll Build

A clean, minimal interface that:

  • Displays all coin flip results with pagination

  • Shows a live feed that auto-refreshes every 5 seconds

Create a Next.js Project

Start by creating a new Next.js application with TypeScript and TailwindCSS:

npx create-next-app@latest somnia-subgraph-ui --typescript --tailwind --app
cd somnia-subgraph-ui

Install the required GraphQL dependencies:

npm install @apollo/client graphql

Understand the Architecture

Before we code, let's understand how the pieces fit together:

Set Up Apollo Client

Apollo Client is a comprehensive GraphQL client that manages data fetching, caching, and state management. Create a lib directory and create a file apollo-client.ts

The URI is the endpoint where your subgraph is hosted, and InMemoryCache will store the query results in memory for fast access

Create the Apollo Provider Wrapper

React components need access to the Apollo Client. We'll create a wrapper component that provides this access to the entire app. Create a components directory and create a file ApolloWrapper.tsx.

ApolloProvider: Makes the Apollo Client available to all child components

Update app/layout.tsx

Create GraphQL Queries

GraphQL queries define exactly what data you want from the subgraph. Let's create queries for our two main features. In the lib directory create a queries.ts file.

queries.ts

Note the following:

  • gql is the template literal tag that parses GraphQL queries

  • Variables start with $ and have types (Int!, String!, etc.)

  • ! means the field is required (non-nullable)

Build the All Flips Component

Let's build the All Flips component step by step, understanding each part in detail.

Set Up the Component File

In the components directory create AllFlips.tsx file and add the following imports.

Create Utility Functions

Add these helper functions at the top of your component:

Component Function and State Management

the number ofpage tracks the current page number (starting at 0), which is used to calculate the number of results to skip. It updates when the user clicks the Previous/Next button.

Execute the GraphQL Query

The useQuery function is set to loading: true while fetching data and the data contains the query results when successful.

Handle Query States

This prevents rendering errors and andles edge cases gracefully

Render the Table View

AllFlips.tsx - Table View

Build the Live Feed Component

Now let's build the Live Feed component that automatically refreshes to show new flips.

Set Up the Component

In the components directory create a LiveFeed.tsx file and update the imports.

Create Utility Functions

Component Function with Auto-Refresh

The pollInterval automatically re-executes the query every 5 seconds. New flips appear without user interaction with Apollo Client handling the refresh logic. You can set to 0 or remove to disable auto-refresh

Handle Query States

Complete Live Feed Component

LiveFeed.tsx

The key differences from the AllFlips page are that there is no pagination (shows most recent only), and it auto-refreshes with pollInterval, with a visual emphasis on win/loss status.

Update the Main Page.tsx

page.tsx

Run Your Application

Visit http://localhost:3000 to see your UI in action.

Last updated