Skip to content

Commit d279295

Browse files
committed
Update docs for cra users
1 parent a992d8f commit d279295

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

README.md

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ The difference is that is uses generic types, so that you have type safety with
2020

2121
### Install
2222

23+
```sh
24+
$ yarn add @graphql-codegen/add @graphql-codegen/typescript @graphql-codegen/typescript-operations
25+
$ yarn add apollo-typed-documents
26+
```
27+
2328
`codegen.yml`:
2429

2530
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/codegenTypedDocuments.yml) -->
@@ -169,24 +174,26 @@ export default AuthorList;
169174

170175
### Notes for `create-react-app` users
171176

172-
`create-react-app` uses `graphql.macro` for loading of `.graphql` files (https://create-react-app.dev/docs/loading-graphql-files/).
177+
`create-react-app` supports `graphql.macro` for loading `.graphql` files (https://create-react-app.dev/docs/loading-graphql-files/).
173178

174-
Because the `codegenTypedDocuments` plugin generates ambient module declarations for those `.graphql` files, they must be imported as regular modules (`commonjs`), otherwise TypeScript can't know its type.
179+
The `codegenTypedDocuments` plugin generates ambient module declarations for `.graphql` files, which means that `.graphql` files must be imported as regular modules (`import` syntax) so that TypeScript knows about the types.
175180

176-
You can use the babel plugin `babel-plugin-import-graphql`, but then you need to either `eject` or use `react-app-rewired`/`customize-cra`.
181+
You can use the babel plugin `babel-plugin-import-graphql` (https://github.com/detrohutt/babel-plugin-import-graphql), but then you need to use `react-app-rewired` (https://github.com/timarney/react-app-rewired/) and `customize-cra` (https://github.com/arackaf/customize-cra) so that you can define custom babel plugins.
177182

178-
Follow install instructions for `react-app-rewired`/`customize-cra`:
183+
```sh
184+
$ yarn add react-app-rewired customize-cra
185+
$ yarn add babel-plugin-import-graphql
186+
```
179187

180-
- https://github.com/timarney/react-app-rewired/
181-
- https://github.com/arackaf/customize-cra
188+
`config-overrides.js`
182189

183-
Install `babel-plugin-import-graphql`
190+
```js
191+
const { override, useBabelRc } = require("customize-cra");
184192

185-
```
186-
$ yarn add babel-plugin-import-graphql
193+
module.exports = override(useBabelRc());
187194
```
188195

189-
Add to `.babelrc`:
196+
`.babelrc`
190197

191198
```json
192199
{
@@ -195,58 +202,57 @@ Add to `.babelrc`:
195202
}
196203
```
197204

198-
Also, if you have a `create-react-app` project without TypeScript, the `tsconfig.json` must not be placed in your app root folder, because `create-react-app` uses this file to detect a TypeScript project: [reference](https://github.com/facebook/create-react-app/blob/v3.4.1/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L49).
205+
`package.json`
206+
207+
```js
208+
"scripts": {
209+
"start": "react-app-rewired start",
210+
"build": "react-app-rewired build",
211+
"test": "react-app-rewired test",
212+
...
213+
}
214+
```
199215

200-
All `.ts`/`.d.ts` files must be outside of your `src` folder: [reference](https://github.com/facebook/create-react-app/blob/v3.4.1/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L50)
216+
If you have a TypeScript app, you need to override the `@apollo/react-hooks` types in `tsconfig.json`:
201217

202-
A solution is to put the `tsconfig.json` in the parent folder:
218+
`tsconfig.json`
203219

204-
```
205-
tsconfig.json
206-
backend/
207-
frontend/
208-
codegen.yml
209-
codegenTypedDocuments.d.ts
210-
codegenTypes.ts
211-
src/
212-
apolloMock.js
220+
```js
221+
{
222+
"compilerOptions": {
213223
...
224+
},
225+
"include": ["src", "node_modules/apollo-typed-documents/lib/reactHooks.d.ts"]
226+
}
214227
```
215228

216-
An example `tsconfig.json`:
229+
If you don't have a TypeScript app (you just want TypeScript support within your IDE) you can't create a `tsconfig.json` in your app folder, because `create-react-app` uses that file to detect if this is a TypeScript project.
230+
231+
Instead, you have to create the `tsconfig.json` in your `src` folder:
217232

233+
`src/tsconfig.json`
234+
235+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/cra/src/tsconfig.json) -->
236+
<!-- The below code snippet is automatically added from ./examples/cra/src/tsconfig.json -->
218237
```json
219238
{
220-
"comment": "File is in root folder so that create-react-app doesn't detect a typescript app",
221239
"compilerOptions": {
222-
"rootDir": "frontend",
223-
"typeRoots": ["frontend/node_modules/@types"],
224-
"baseUrl": "frontend",
225-
"paths": {
226-
"@codegen-types": ["codegenTypes.ts"],
227-
"@apollo/react-hooks": [
228-
"node_modules/apollo-typed-documents/lib/reactHooks.d.ts"
229-
]
230-
},
240+
"noEmit": true,
231241
"allowJs": true,
232242
"checkJs": true,
233-
"noEmit": true,
243+
"strict": true,
234244
"jsx": "react",
235-
"esModuleInterop": true,
236-
"moduleResolution": "node",
237-
"alwaysStrict": true,
238-
"strictNullChecks": true,
239-
"target": "ES2018"
245+
"esModuleInterop": true
240246
},
241-
"include": [
242-
"frontend/src/**/*",
243-
"frontend/*.ts",
244-
"frontend/*.d.ts",
245-
"frontend/node_modules/react-scripts/lib/react-app.d.ts",
246-
"frontend/node_modules/babel-plugin-react-intl-auto/types.d.ts"
247-
]
247+
"include": [".", "../node_modules/apollo-typed-documents/lib/reactHooks.d.ts"]
248248
}
249249
```
250+
<!-- AUTO-GENERATED-CONTENT:END -->
251+
252+
Please see the following example projects for more details:
253+
254+
- [`create-react-app` with TypeScript](examples/cra-ts)
255+
- [`create-react-app` without TypeScript](examples/cra)
250256

251257
## codegenApolloMock
252258

@@ -270,6 +276,10 @@ When used together with `codegenTypedDocuments` the data and variables are type
270276

271277
### Install
272278

279+
```sh
280+
$ yarn add apollo-typed-documents
281+
```
282+
273283
`codegen.yml`
274284

275285
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/codegenApolloMock.yml) -->

0 commit comments

Comments
 (0)