Skip to content

Commit e8dc5a9

Browse files
committed
docs: Add and organize document
1 parent c9c2912 commit e8dc5a9

File tree

1 file changed

+118
-33
lines changed

1 file changed

+118
-33
lines changed

README.md

Lines changed: 118 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,145 @@
1-
# graphql-codegen-plugin-typescript-swr
1+
# graphql-codegen-plugin-typescript-swr <!-- omit in toc -->
22

33
A [GraphQL code generator](https://graphql-code-generator.com/) plug-in that automatically generates utility functions for [SWR](https://swr.vercel.app/).
44

5-
## Example
5+
## Table of Contents <!-- omit in toc -->
6+
7+
- [API Reference](#api-reference)
8+
- [`excludeQueries`](#excludequeries)
9+
- [`useSWRInfinite`](#useswrinfinite)
10+
- [`autogenSWRKey`](#autogenswrkey)
11+
- [Config Example](#config-example)
12+
- [Usage Examples](#usage-examples)
13+
- [Pagination](#pagination)
14+
- [Authorization](#authorization)
15+
- [Next.js](#nextjs)
16+
17+
## API Reference
18+
19+
### `excludeQueries`
20+
21+
type: `string | string[]` default: `""`
22+
23+
Exclude queries that are matched by micromatch (case-sensitive).
24+
25+
### `useSWRInfinite`
26+
27+
type: `string | string[]` default: `""`
28+
29+
Add `useSWRInfinite()` wrapper for the queries that are matched by micromatch (case-sensitive).
30+
31+
### `autogenSWRKey`
32+
33+
type: `boolean` default: `false`
34+
35+
Generate key to use `useSWR()` automatically.
36+
But, ​the cache may not work unless you separate the variables object into an external file and use it, or use a primitive type for the value of each field.
37+
38+
## Config Example
639

740
```yaml
8-
# codegen.yml
9-
# Add `plugin-typescript-swr` below `typescript-graphql-request`
1041
generates:
1142
path/to/graphql.ts:
1243
schema: 'schemas/github.graphql'
1344
documents: 'src/services/github/**/*.graphql'
1445
plugins:
1546
- typescript
1647
- typescript-operations
48+
# Put `plugin-typescript-swr` below `typescript-graphql-request`
1749
- typescript-graphql-request
1850
- plugin-typescript-swr
1951
config:
2052
rawRequest: false
21-
# exclude queries that are matched by micromatch (case-sensitive)
2253
excludeQueries:
2354
- foo*
2455
- bar
25-
# add `useSWRInfinite` wrapper for the queries that are matched by micromatch (case-sensitive)
2656
useSWRInfinite:
2757
- hoge
2858
- bar{1,3}
29-
# generate keys automatically.
30-
# but, ​the cache may not work unless you separate the variables object into an external file and use it,
31-
# or use a primitive type for the value of each field.
32-
autogenSWRKey: true #(default: false)
59+
autogenSWRKey: true
60+
```
61+
62+
## Usage Examples
63+
64+
For the given input:
65+
66+
```graphql
67+
query continents {
68+
continents {
69+
name
70+
countries {
71+
...CountryFields
72+
}
73+
}
74+
}
75+
76+
fragment CountryFields on Country {
77+
name
78+
currency
79+
}
80+
```
81+
82+
It generates SDK you can import and wrap your GraphQLClient instance, and get fully-typed SDK based on your operations:
83+
84+
```tsx
85+
import { GraphQLClient } from 'graphql-request'
86+
import { getSdkWithHooks } from './sdk'
87+
88+
function Continents() {
89+
const client = new GraphQLClient('https://countries.trevorblades.com/')
90+
const sdk = getSdkWithHooks(client)
91+
92+
const { data, error } = sdk.useContinents('Continents')
93+
94+
if (error) return <div>failed to load</div>
95+
if (!data) return <div>loading...</div>
96+
97+
return (
98+
<ul>
99+
{data.continents.map((continent) => (
100+
<li>{continent.name}</li>
101+
))}
102+
</ul>
103+
)
104+
}
105+
```
106+
107+
### Pagination
108+
109+
#### codegen.yaml <!-- omit in toc -->
110+
111+
```yaml
112+
config:
113+
useSWRInfinite:
114+
- MyQuery
115+
```
116+
117+
#### Functional Component <!-- omit in toc -->
118+
119+
```tsx
120+
const { data, size, setSize } = sdk.useMyQueryInfinite(
121+
'id_for_caching',
122+
(pageIndex, previousPageData) => {
123+
if (previousPageData && !previousPageData.search.pageInfo.hasNextPage)
124+
return null
125+
return [
126+
'after',
127+
previousPageData ? previousPageData.search.pageInfo.endCursor : null,
128+
]
129+
},
130+
variables, // GraphQL Query Variables
131+
config // Configuration of useSWRInfinite
132+
)
33133
```
34134

135+
### Authorization
136+
35137
```typescript
36-
// sdk.ts
37138
import { GraphQLClient } from 'graphql-request'
38-
import { getSdkWithHooks } from './graphql'
139+
import { getSdkWithHooks } from './sdk'
39140
import { getJwt } from './jwt'
40141

41-
const sdk = () => {
142+
const getAuthorizedSdk = () => {
42143
const headers: Record<string, string> = { 'Content-Type': 'application/json' }
43144
const jwt = getJwt()
44145
if (jwt) {
@@ -51,9 +152,11 @@ const sdk = () => {
51152
)
52153
}
53154

54-
export default sdk
155+
export default getAuthorizedSdk
55156
```
56157

158+
### Next.js
159+
57160
```tsx
58161
// pages/posts/[slug].tsx
59162
import { GetStaticProps, NextPage } from 'next'
@@ -109,7 +212,7 @@ export const getStaticProps: GetStaticProps<StaticProps, StaticParams> = async (
109212
export const ArticlePage: NextPage<ArticlePageProps> = ({ slug, initialData, preview }) => {
110213
const router = useRouter()
111214
const { data: { article }, mutate: mutateArticle } = sdk().useGetArticle(
112-
`UniqueKeyForTheRequest/${slug}`, { slug }, { initialData }
215+
`GetArticle/${slug}`, { slug }, { initialData }
113216
)
114217

115218
if (!router.isFallback && !article) {
@@ -133,21 +236,3 @@ export const ArticlePage: NextPage<ArticlePageProps> = ({ slug, initialData, pre
133236
)
134237
}
135238
```
136-
137-
### Example for useSWRInfinite
138-
139-
```tsx
140-
const { data, size, setSize } = sdk.useMyQueryInfinite(
141-
'id_for_caching',
142-
(pageIndex, previousPageData) => {
143-
if (previousPageData && !previousPageData.search.pageInfo.hasNextPage)
144-
return null
145-
return [
146-
'after',
147-
previousPageData ? previousPageData.search.pageInfo.endCursor : null,
148-
]
149-
},
150-
variables, // GraphQL Query Variables
151-
config // Configuration of useSWRInfinite
152-
)
153-
```

0 commit comments

Comments
 (0)