Skip to content

Commit

Permalink
Using apollo with RSC support to fetch mods in a table
Browse files Browse the repository at this point in the history
  • Loading branch information
rithik-b committed Dec 7, 2023
1 parent 9fa8bc2 commit ab876f4
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 34 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@apollo/client": "^3.8.8",
"@apollo/experimental-nextjs-app-support": "^0.5.2",
"@tanstack/react-table": "^8.10.7",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/apollo/ApolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApolloClient, InMemoryCache } from "@apollo/client"

const apolloClient = new ApolloClient({
uri: `${process.env.NEXT_PUBLIC_BEATFORGE_API_URL}/graphql`,
cache: new InMemoryCache(),
})

export default apolloClient
13 changes: 13 additions & 0 deletions apps/web/src/apollo/ApolloProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use client"

import { ApolloProvider as _ApolloProvider } from "@apollo/client"
import { PropsWithChildren } from "react"
import apolloClient from "@beatforge/web/apollo/ApolloClient"

const ApolloProvider = (props: PropsWithChildren) => {
return (
<_ApolloProvider client={apolloClient}>{props.children}</_ApolloProvider>
)
}

export default ApolloProvider
8 changes: 8 additions & 0 deletions apps/web/src/apollo/getApolloRSCClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { registerApolloClient } from "@apollo/experimental-nextjs-app-support/rsc"
import apolloClient from "@beatforge/web/apollo/ApolloClient"

const { getClient: getApolloRSCClient } = registerApolloClient(
() => apolloClient,
)

export default getApolloRSCClient
2 changes: 1 addition & 1 deletion apps/web/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Metadata } from "next"
import "./globals.css"
import { GeistSans } from "geist/font/sans"
import ApolloProvider from "@beatforge/web/app/mods/ApolloProvider"
import ApolloProvider from "@beatforge/web/apollo/ApolloProvider"

export const metadata: Metadata = {
title: "BeatForge",
Expand Down
21 changes: 0 additions & 21 deletions apps/web/src/app/mods/ApolloProvider.tsx

This file was deleted.

42 changes: 42 additions & 0 deletions apps/web/src/app/mods/ModsTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use client"

import { GetModsQuery } from "@beatforge/web/__generated__/graphql"
import { ColumnDef } from "@tanstack/react-table"
import DataTable from "@beatforge/web/components/DataTable"

interface Props {
mods: GetModsQuery["mods"]
}

interface Mod {
name: string
version: string
author: string
}

const columns: ColumnDef<Mod>[] = [
{
accessorKey: "name",
header: "Name",
},
{
accessorKey: "version",
header: "Version",
},
{
accessorKey: "author",
header: "Author",
},
]

const ModsTable = ({ mods }: Props) => {
const mappedMods = mods.map((mod) => ({
name: mod.name,
version: mod.versions[0].version,
author: mod.author.displayName ?? mod.author.username,
}))

return <DataTable columns={columns} data={mappedMods} />
}

export default ModsTable
27 changes: 15 additions & 12 deletions apps/web/src/app/mods/page.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
"use client"

import { gql } from "@beatforge/web/__generated__"
import { useQuery } from "@apollo/client"
import ModsTable from "@beatforge/web/app/mods/ModsTable"
import getApolloRSCClient from "@beatforge/web/apollo/getApolloRSCClient"

const getModsQuery = gql(/* GraphQL */ `
query GetMods {
mods {
name
id
versions {
version
}
author {
username
displayName
}
}
}
`)

const Mods = () => {
const { data, error } = useQuery(getModsQuery)
return (
<main className="">
<h1>Mods</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</main>
)
const Mods = async () => {
const mods = await getApolloRSCClient().query({
query: getModsQuery,
})

return <ModsTable mods={mods.data.mods} />
}

export default Mods
79 changes: 79 additions & 0 deletions apps/web/src/components/DataTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"use client"

import {
ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table"

import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@beatforge/web/components/ui/table"

interface Props<TData, TValue> {
columns: ColumnDef<TData, TValue>[]
data: TData[]
}

function DataTable<TData, TValue>({ columns, data }: Props<TData, TValue>) {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})

return (
<div className="rounded-md border">
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && "selected"}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell colSpan={columns.length} className="h-24 text-center">
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
)
}

export default DataTable
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab876f4

Please sign in to comment.