Skip to content

Commit 6819f1b

Browse files
committed
Update the example
1 parent d29bc5c commit 6819f1b

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

README.md

+67-20
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,99 @@ config:
3030
// sdk.ts
3131
import { GraphQLClient } from 'graphql-request'
3232
import { getSdkWithHooks } from './graphql'
33+
import { getJwt } from './jwt'
3334

34-
const sdk = getSdkWithHooks(
35-
new GraphQLClient(`${process.env.API_URL}/graphql`, {
36-
cache: typeof window === 'object' ? 'default' : 'no-cache',
37-
})
38-
)
35+
const sdk = () => {
36+
const headers: Record<string, string> = { 'Content-Type': 'application/json' }
37+
const jwt = getJwt()
38+
if (jwt) {
39+
headers.Authorization = `Bearer ${jwt}`
40+
}
41+
return getSdkWithHooks(
42+
new GraphQLClient(`${process.env.NEXT_PUBLIC_API_URL}`, {
43+
headers,
44+
})
45+
)
46+
}
3947

4048
export default sdk
4149
```
4250

4351
```tsx
44-
// Article.tsx
52+
// pages/posts/[slug].tsx
53+
import { GetStaticProps, GetStaticPropsResult } from 'next'
54+
import ErrorPage from 'next/error'
55+
import { useRouter } from 'next/router'
56+
import Article from '../components/Article'
4557
import sdk from '../sdk'
4658

47-
type StaticPropsParams = { slug: string }
48-
export const getStaticProps: GetStaticProps<StaticProps, StaticPropsParams> = async ({
59+
type StaticParams = { slug: string }
60+
type StaticProps = StaticParams & {
61+
initialData: {
62+
articleBySlug: NonNullable<GetArticleQuery['articleBySlug']>
63+
}
64+
}
65+
type ArticlePageProps = StaticProps & { preview?: boolean }
66+
67+
export const getStaticProps: GetStaticProps<StaticProps, StaticParams> = async ({
4968
params,
5069
preview = false,
70+
previewData
5171
}) => {
5272
if (!params) {
5373
throw new Error('Parameter is invalid!')
5474
}
5575

56-
const { articleBySlug: article } = await sdk.GetArticle({
76+
const { articleBySlug: article } = await sdk().GetArticle({
5777
slug: params.slug,
5878
})
5979

6080
if (!article) {
6181
throw new Error('Article is not found!')
6282
}
6383

64-
return {
65-
props: {
66-
slug: params.slug,
67-
preview,
68-
initialData: {
69-
articleBySlug: article
70-
}
84+
const props: ArticlePageProps = {
85+
slug: params.slug,
86+
preview,
87+
initialData: {
88+
articleBySlug: article
7189
}
7290
}
91+
92+
return {
93+
props: preview
94+
? {
95+
...props,
96+
...previewData,
97+
}
98+
: props,
99+
}
73100
})
74101

75-
export const Article = ({ slug, initialData, preview }: ArticleProps): JSX.Element => {
76-
const { data: { article } } = sdk.useGetArticle(
77-
'UniqueKeyForTheRequest', { slug }, { initialData }
102+
export const ArticlePage = ({ slug, initialData, preview }: ArticlePageProps): JSX.Element => {
103+
const router = useRouter()
104+
const { data: { article }, mutate: mutateArticle } = sdk().useGetArticle(
105+
`UniqueKeyForTheRequest/${slug}`, { slug }, { initialData }
106+
)
107+
108+
if (!router.isFallback && !article) {
109+
return <ErrorPage statusCode={404} />
110+
}
111+
112+
// because of typescript problem
113+
if (!article) {
114+
throw new Error('Article is not found!')
115+
}
116+
117+
return (
118+
<Layout preview={preview}>
119+
<>
120+
<Head>
121+
<title>{article.title}</title>
122+
</Head>
123+
<Article article={article} />
124+
</>
125+
</Layout>
78126
)
79-
// ...
80127
}
81128
```

0 commit comments

Comments
 (0)