We’re going to use a GraphQL Client on the front end. There are many out there, but we’re going to go with Urql. I chose this one because of its simplicity and react features. Urql handles many things like making the API calls to even caching the results.
First we need to create a client as a provider for the app.
// app/gqlProvider.tsx
'use client'
import { PropsWithChildren, useMemo } from 'react'
import {
UrqlProvider,
ssrExchange,
fetchExchange,
createClient,
gql,
} from '@urql/next'
import { cacheExchange } from '@urql/exchange-graphcache'
import { url } from '@/utils/url'
import { getToken } from '@/utils/token'
export default function GQLProvider({ children }: PropsWithChildren) {
const [client, ssr] = useMemo(() => {
const ssr = ssrExchange({
isClient: typeof window !== 'undefined',
})
const client = createClient({
url,
exchanges: [cacheExchange({}), ssr, fetchExchange],
fetchOptions: () => {
const token = getToken()
return token
? {
headers: { authorization: `Bearer ${token}` },
}
: {}
},
})
return [client, ssr]
}, [])
return (
<UrqlProvider client={client} ssr={ssr}>
{children}
</UrqlProvider>
)
}
// app/layout.tsx
import { Inter } from 'next/font/google'
import './globals.css'
import { Providers } from './providers'
import GQLProvider from './gqlProvider'
const inter = Inter({ subsets: ['latin'] })
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html lang="en">
<body className={inter.className}>
<Providers>
<GQLProvider>{children}</GQLProvider>
</Providers>
</body>
</html>
)
}