With Apollo
Using pg_grapqhl with Apollo.
This guide will show you how to use pg_graphql with Apollo and GraphQL Code Generator for type-safe GraphQL queries in your React application.
- Follow the Apollo Getting Started Guide.
- Follow the GraphQL Code Generator Installation Guide.
Modify your codegen.ts
file to reflect the following:
import type { CodegenConfig } from '@graphql-codegen/cli'
import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset'
const config: CodegenConfig = {
schema: 'http://localhost:54321/graphql/v1', // Using the local endpoint, update if needed
documents: 'src/**/*.tsx',
documentTransforms: [addTypenameSelectionDocumentTransform],
afterAllFileWrite: ['npm run prettier'], // optional
This example uses Supabase for the GraphQL server, but pg_graphql can be used independently.
import { setContext } from '@apollo/client/link/context'
import { relayStylePagination } from '@apollo/client/utilities'
import supabase from './supabase'
const cache = new InMemoryCache({
dataIdFromObject(responseObject) {
if ('nodeId' in responseObject) {
return `${responseObject.nodeId}`
return defaultDataIdFromObject(responseObject)
possibleTypes: { Node: ['Todos'] }, // optional, but useful to specify supertype-subtype relationships
todosCollection: relayStylePagination(), // example of paginating a collection
read(_, { args, toReference }) {
const ref = toReference({
const httpLink = createHttpLink({
uri: 'http://localhost:54321/graphql/v1',
const authLink = setContext(async (_, { headers }) => {
const token = (await supabase.auth.getSession()).data.session?.access_token
Authorization: token ? `Bearer ${token}` : '',
const apolloClient = new ApolloClient({
link: authLink.concat(httpLink),
export default apolloClient
typePolicies.Query.fields.node
is also optional, but useful for reducing cache misses. Learn more about Redirecting to cached data.
import { useQuery } from '@apollo/client'
import { graphql } from './gql'
const allTodosQueryDocument = graphql(/* GraphQL */ `
query AllTodos($cursor: Cursor) {
todosCollection(first: 10, after: $cursor) {
const { data, fetchMore } = useQuery(allTodosQueryDocument)
{data?.thingsCollection?.edges.map(({ node }) => (
<Todo key={node.nodeId} title={node.title} />
{data?.thingsCollection?.pageInfo.hasNextPage && (
cursor: data?.thingsCollection?.pageInfo.endCursor,