Skip to content

Commit

Permalink
feat: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenho0811 committed Mar 4, 2024
1 parent ce03ea4 commit 474e806
Show file tree
Hide file tree
Showing 13 changed files with 17,241 additions and 149 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_GRAPHQL_URI="https://very.icook.tw/graphql"
37 changes: 37 additions & 0 deletions apollo/ApolloWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use client'

import { ApolloLink } from '@apollo/client'
import { BatchHttpLink } from '@apollo/client/link/batch-http'
import {
ApolloNextAppProvider,
NextSSRApolloClient,
NextSSRInMemoryCache,
SSRMultipartLink,
} from '@apollo/experimental-nextjs-app-support/ssr'

function makeClient() {
const httpLink = new BatchHttpLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_URI,
})

return new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(),
link:
typeof window === 'undefined'
? ApolloLink.from([
new SSRMultipartLink({
stripDefer: true,
}),
httpLink,
])
: httpLink,
})
}

export function ApolloWrapper({ children }: React.PropsWithChildren) {
return (
<ApolloNextAppProvider makeClient={makeClient}>
{children}
</ApolloNextAppProvider>
)
}
15 changes: 15 additions & 0 deletions apollo/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { HttpLink } from '@apollo/client/link/http'
import { registerApolloClient } from '@apollo/experimental-nextjs-app-support/rsc'
import {
NextSSRApolloClient,
NextSSRInMemoryCache,
} from '@apollo/experimental-nextjs-app-support/ssr'

export const { getClient } = registerApolloClient(() => {
return new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(),
link: new HttpLink({
uri: process.env.NEXT_PUBLIC_GRAPHQL_URI,
}),
})
})
12 changes: 12 additions & 0 deletions app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { revalidatePath } from 'next/cache'
import { type NextRequest } from 'next/server'

export async function GET(req: NextRequest) {
const { searchParams } = new URL(req.url || '')
const postId = searchParams.get('postId')

revalidatePath('/', 'layout')
revalidatePath(`/posts/${postId}`, 'page')

return new Response(null, { status: 204 })
}
30 changes: 0 additions & 30 deletions app/globals.css
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;
}

@media (prefers-color-scheme: dark) {
:root {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
}
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(
to bottom,
transparent,
rgb(var(--background-end-rgb))
)
rgb(var(--background-start-rgb));
}

@layer utilities {
.text-balance {
text-wrap: balance;
}
}
18 changes: 7 additions & 11 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { ApolloWrapper } from '@/apollo/ApolloWrapper';
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<html>
<body>
<ApolloWrapper>
{children}
</ApolloWrapper>
</body>
</html>
);
}
135 changes: 29 additions & 106 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,113 +1,36 @@
import Image from "next/image";
import { getClient } from '@/apollo/client'
import { PostsDocument, type PostsQuery } from '@/graphql/types-and-hooks'
import Link from 'next/link'

export default function Home() {
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
Get started by editing&nbsp;
<code className="font-mono font-bold">app/page.tsx</code>
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
By{" "}
<Image
src="/vercel.svg"
alt="Vercel Logo"
className="dark:invert"
width={100}
height={24}
priority
/>
</a>
</div>
</div>
const queryPosts = async () => {

<div className="relative flex place-items-center before:absolute before:h-[300px] before:w-full sm:before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-full sm:after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 before:lg:h-[360px] z-[-1]">
<Image
className="relative dark:drop-shadow-[0_0_0.3rem_#ffffff70] dark:invert"
src="/next.svg"
alt="Next.js Logo"
width={180}
height={37}
priority
/>
</div>
const first = 10
const client = getClient()

<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<a
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Docs{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Find in-depth information about Next.js features and API.
</p>
</a>
const { data } = await client.query<PostsQuery>({
query: PostsDocument,
variables: {
first,
},
})

<a
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Learn{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Learn about Next.js in an interactive course with&nbsp;quizzes!
</p>
</a>
return data
}

<a
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Templates{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50`}>
Explore starter templates for Next.js.
</p>
</a>
const PopularPostSection = async () => {
const { posts } = await queryPosts()

<a
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer"
>
<h2 className={`mb-3 text-2xl font-semibold`}>
Deploy{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={`m-0 max-w-[30ch] text-sm opacity-50 text-balance`}>
Instantly deploy your Next.js site to a shareable URL with Vercel.
</p>
</a>
</div>
</main>
);
return (
<section>
<ul className='space-y-6'>
{posts?.nodes.map((post) => (
<li key={post.databaseId}>
<Link href={`/posts/${post.databaseId}`}>{post.title}</Link>
</li>
))}
</ul>
</section>
)
}

export default PopularPostSection
42 changes: 42 additions & 0 deletions app/posts/[postId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getClient } from '@/apollo/client'
import { PostDocument, type PostQuery } from '@/graphql/types-and-hooks'
import { notFound } from 'next/navigation'
import { cache, type FC } from 'react'

const queryPost = cache(async (postId: number) => {
const client = getClient()

const { data } = await client.query<PostQuery>({
query: PostDocument,
variables: {
postId,
},
})

return data
})



export interface PostIdPageProps {
params: {
postId: string
}
searchParams: {
preview?: string
}
}

const PostIdPage: FC<PostIdPageProps> = async ({ params: { postId } }) => {
if (!postId || isNaN(+postId)) {
notFound()
}

const { post } = await queryPost(+postId)


return <div dangerouslySetInnerHTML={{__html: (post?.content || "")}}></div>
}


export default PostIdPage
12 changes: 12 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
schema:
- https://very.icook.tw/graphql:
headers:
user-agent: "JS GraphQL"
documents:
- "./**/*.graphql"
generates:
./graphql/types-and-hooks.tsx:
plugins:
- typescript
- typescript-operations
- typescript-react-apollo
Loading

0 comments on commit 474e806

Please sign in to comment.