File tree 1 file changed +70
-0
lines changed
1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change 1
1
# graphql-codegen-plugin-typescript-swr
2
2
3
3
A [ GraphQL code generator] ( https://graphql-code-generator.com/ ) plug-in that automatically generates utility functions for [ SWR] ( https://swr.vercel.app/ ) .
4
+
5
+ # Example
6
+
7
+ ``` yaml
8
+ # codegen.yml
9
+ # Add `plugin-typescript-swr` below `typescript-graphql-request`
10
+ generates :
11
+ path/to/graphql.ts :
12
+ plugins :
13
+ - typescript
14
+ - typescript-operations
15
+ - typescript-graphql-request
16
+ - plugin-typescript-swr
17
+ config :
18
+ rawRequest : false
19
+ ` ` `
20
+
21
+ ` ` ` typescript
22
+ // sdk.ts
23
+ import { GraphQLClient } from 'graphql-request'
24
+ import { getSdkWithHooks } from './graphql'
25
+
26
+ const sdk = getSdkWithHooks(
27
+ // Disable the cache because it is managed by `swr`.
28
+ // In the case of SSG, the server side always fetches the latest data, so it is unnecessary.
29
+ new GraphQLClient(`${process.env.API_URL}/graphql`, { cache : ' no-cache' })
30
+ )
31
+
32
+ export default sdk
33
+ ```
34
+
35
+ ``` tsx
36
+ // Article.tsx
37
+ import sdk from ' ../sdk'
38
+
39
+ type StaticPropsParams = { slug: string }
40
+ export const getStaticProps: GetStaticProps <StaticProps , StaticPropsParams > = async ({
41
+ params ,
42
+ preview = false ,
43
+ }) => {
44
+ if (! params ) {
45
+ throw new Error (' Parameter is invalid!' )
46
+ }
47
+
48
+ const { articleBySlug : article } = await sdk .GetArticle ({
49
+ slug: params .slug ,
50
+ })
51
+
52
+ if (! article ) {
53
+ throw new Error (' Article is not found!' )
54
+ }
55
+
56
+ return {
57
+ props: {
58
+ slug: params .slug ,
59
+ preview ,
60
+ initialData: {
61
+ articleBySlug: article
62
+ }
63
+ }
64
+ }
65
+ })
66
+
67
+ export const Article = ({ slug , initialData , preview }: ArticleProps ): JSX .Element => {
68
+ const { data : { article } } = sdk .useGetArticle (
69
+ ' UniqueKeyForTheRequest' , { slug }, { initialData }
70
+ )
71
+ // ...
72
+ }
73
+ ```
You can’t perform that action at this time.
0 commit comments