Skip to content

Commit eba1a8b

Browse files
authored
Merge pull request rubengrill#2 from rubengrill/cra-examples
CRA examples
2 parents 23bb75d + d279295 commit eba1a8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+23672
-1097
lines changed

.eslintignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
examples/docs/apolloMock.js
2-
examples/docs/codegenTypedDocuments.d.ts
3-
examples/docs/codegenTypes.ts
1+
examples/**/apolloMock.js
2+
examples/**/codegenTypedDocuments.d.ts
3+
examples/**/codegenTypes.d.ts

.prettierignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
examples/docs/apolloMock.js
2-
examples/docs/codegenTypedDocuments.d.ts
3-
examples/docs/codegenTypes.ts
1+
examples/**/apolloMock.js
2+
examples/**/codegenTypedDocuments.d.ts
3+
examples/**/codegenTypes.d.ts
4+
README.md

README.md

Lines changed: 108 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,62 @@ 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) -->
2631
<!-- The below code snippet is automatically added from ./examples/docs/codegenTypedDocuments.yml -->
2732
```yml
28-
schema: ./schema.graphql
29-
documents: ./documents/*.graphql
33+
schema: schema.graphql
34+
documents: src/**/*.graphql
3035
generates:
31-
./codegenTypedDocuments.d.ts:
36+
./src/codegenTypedDocuments.d.ts:
3237
plugins:
3338
- apollo-typed-documents/lib/codegenTypedDocuments
3439
config:
3540
typesModule: "@codegen-types"
36-
./codegenTypes.ts:
41+
./src/codegenTypes.d.ts:
3742
plugins:
43+
- add: 'declare module "@codegen-types" {'
44+
- add:
45+
placement: append
46+
content: "}"
3847
- typescript
3948
- typescript-operations
4049
```
4150
<!-- AUTO-GENERATED-CONTENT:END -->
4251
4352
`tsconfig.json`:
4453

54+
Add `node_modules/apollo-typed-documents/lib/reactHooks.d.ts` in `include` to override the typings for `@apollo/react-hooks`, so that types can be inferred from typed documents.
55+
56+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/tsconfig.json) -->
57+
<!-- The below code snippet is automatically added from ./examples/docs/tsconfig.json -->
4558
```json
4659
{
4760
"compilerOptions": {
48-
"baseUrl": ".",
49-
"paths": {
50-
"@codegen-types": ["codegenTypes.ts"],
51-
"@apollo/react-hooks": [
52-
"./node_modules/apollo-typed-documents/lib/reactHooks.d.ts"
53-
]
54-
}
55-
}
61+
"noEmit": true,
62+
"allowJs": true,
63+
"checkJs": true,
64+
"strict": true,
65+
"jsx": "react",
66+
"esModuleInterop": true
67+
},
68+
"include": ["src", "node_modules/apollo-typed-documents/lib/reactHooks.d.ts"]
5669
}
5770
```
58-
59-
`@codegen-types` points to the output of `typescript-operations` graphql-codegen plugin.
60-
61-
This alias is required, because in ambient module declarations (`.d.ts`) only non relative imports are allowed.
62-
63-
`@apollo/react-hooks` overrides the types to have generic hooks in your code: [reference](src/reactHooks.ts)
64-
65-
If you don't have a TypeScript project but instead just want to get type safety and code completion,
66-
the configuration needs to have more options set: [reference](examples/docs/tsconfig.json)
71+
<!-- AUTO-GENERATED-CONTENT:END -->
6772

6873
### Example
6974

70-
`./documents/authors.graphql`:
75+
`src/authors.graphql`:
7176

72-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/documents/authors.graphql) -->
73-
<!-- The below code snippet is automatically added from ./examples/docs/documents/authors.graphql -->
77+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/authors.graphql) -->
78+
<!-- The below code snippet is automatically added from ./examples/docs/src/authors.graphql -->
7479
```graphql
7580
query authors {
7681
authors {
@@ -86,10 +91,10 @@ query authors {
8691
```
8792
<!-- AUTO-GENERATED-CONTENT:END -->
8893

89-
`./documents/createAuthor.graphql`:
94+
`src/createAuthor.graphql`:
9095

91-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/documents/createAuthor.graphql) -->
92-
<!-- The below code snippet is automatically added from ./examples/docs/documents/createAuthor.graphql -->
96+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/createAuthor.graphql) -->
97+
<!-- The below code snippet is automatically added from ./examples/docs/src/createAuthor.graphql -->
9398
```graphql
9499
mutation createAuthor($input: AuthorInput!) {
95100
createAuthor(input: $input) {
@@ -105,10 +110,10 @@ mutation createAuthor($input: AuthorInput!) {
105110
```
106111
<!-- AUTO-GENERATED-CONTENT:END -->
107112

108-
`./codegenTypedDocuments.d.ts` (generated):
113+
`src/codegenTypedDocuments.d.ts` (generated):
109114

110-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/codegenTypedDocuments.d.ts) -->
111-
<!-- The below code snippet is automatically added from ./examples/docs/codegenTypedDocuments.d.ts -->
115+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/codegenTypedDocuments.d.ts) -->
116+
<!-- The below code snippet is automatically added from ./examples/docs/src/codegenTypedDocuments.d.ts -->
112117
```ts
113118
declare module "*/authors.graphql" {
114119
import { TypedDocumentNode } from "apollo-typed-documents";
@@ -126,16 +131,16 @@ declare module "*/createAuthor.graphql" {
126131
```
127132
<!-- AUTO-GENERATED-CONTENT:END -->
128133

129-
`./AuthorList.js`:
134+
`src/AuthorList.js`:
130135

131-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/AuthorList.js) -->
132-
<!-- The below code snippet is automatically added from ./examples/docs/AuthorList.js -->
136+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/AuthorList.js) -->
137+
<!-- The below code snippet is automatically added from ./examples/docs/src/AuthorList.js -->
133138
```js
134139
import { useMutation, useQuery } from "@apollo/react-hooks";
135140
import React from "react";
136141
137-
import authorsQuery from "./documents/authors.graphql";
138-
import createAuthorMutation from "./documents/createAuthor.graphql";
142+
import authorsQuery from "./authors.graphql";
143+
import createAuthorMutation from "./createAuthor.graphql";
139144
140145
const AuthorList = () => {
141146
// Type of `data` is inferred (AuthorsQuery)
@@ -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.
217230

231+
Instead, you have to create the `tsconfig.json` in your `src` folder:
232+
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,23 +276,27 @@ 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) -->
276286
<!-- The below code snippet is automatically added from ./examples/docs/codegenApolloMock.yml -->
277287
```yml
278-
schema: ./schema.graphql
279-
documents: ./documents/*.graphql
288+
schema: schema.graphql
289+
documents: src/**/*.graphql
280290
generates:
281-
./apolloMock.js:
291+
./src/apolloMock.js:
282292
plugins:
283293
- apollo-typed-documents/lib/codegenApolloMock
284294
```
285295
<!-- AUTO-GENERATED-CONTENT:END -->
286296
287297
### Example
288298
289-
`./schema.graphql`:
299+
`schema.graphql`:
290300

291301
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/schema.graphql) -->
292302
<!-- The below code snippet is automatically added from ./examples/docs/schema.graphql -->
@@ -328,10 +338,10 @@ schema {
328338
```
329339
<!-- AUTO-GENERATED-CONTENT:END -->
330340

331-
`./documents/authors.graphql`:
341+
`src/authors.graphql`:
332342

333-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/documents/authors.graphql) -->
334-
<!-- The below code snippet is automatically added from ./examples/docs/documents/authors.graphql -->
343+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/authors.graphql) -->
344+
<!-- The below code snippet is automatically added from ./examples/docs/src/authors.graphql -->
335345
```graphql
336346
query authors {
337347
authors {
@@ -347,10 +357,10 @@ query authors {
347357
```
348358
<!-- AUTO-GENERATED-CONTENT:END -->
349359

350-
`./documents/createAuthor.graphql`:
360+
`src/createAuthor.graphql`:
351361

352-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/documents/createAuthor.graphql) -->
353-
<!-- The below code snippet is automatically added from ./examples/docs/documents/createAuthor.graphql -->
362+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/createAuthor.graphql) -->
363+
<!-- The below code snippet is automatically added from ./examples/docs/src/createAuthor.graphql -->
354364
```graphql
355365
mutation createAuthor($input: AuthorInput!) {
356366
createAuthor(input: $input) {
@@ -366,18 +376,18 @@ mutation createAuthor($input: AuthorInput!) {
366376
```
367377
<!-- AUTO-GENERATED-CONTENT:END -->
368378

369-
`./apolloMock.js` (generated):
379+
`src/apolloMock.js` (generated):
370380

371-
See: [reference](examples/docs/apolloMock.js)
381+
See: [reference](examples/docs/src/apolloMock.js)
372382

373-
`./apolloMock.test.js`:
383+
`src/apolloMock.test.js`:
374384

375-
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/apolloMock.test.js) -->
376-
<!-- The below code snippet is automatically added from ./examples/docs/apolloMock.test.js -->
385+
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/apolloMock.test.js) -->
386+
<!-- The below code snippet is automatically added from ./examples/docs/src/apolloMock.test.js -->
377387
```js
378388
import apolloMock from "./apolloMock";
379-
import authors from "./documents/authors.graphql";
380-
import createAuthor from "./documents/createAuthor.graphql";
389+
import authors from "./authors.graphql";
390+
import createAuthor from "./createAuthor.graphql";
381391
382392
describe("apolloMock", () => {
383393
it("produces the minimal output that is valid according to graphql schema", () => {

examples/cra-ts/.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["react-app"],
3+
"plugins": ["babel-plugin-import-graphql"]
4+
}

0 commit comments

Comments
 (0)