Skip to content

Commit 95dbaa0

Browse files
committed
added readme/codegen explanation
1 parent db6eac5 commit 95dbaa0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Diff for: README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# graphql-typescript-react-demo
2+
3+
## Usage and Codegen
4+
5+
Run `yarn install` in the root as well as the `app` and `api` directories. Run `yarn api` to start the API (to actually run queries for data, you'll need a Movies Database API key). For codegen, you will not need to worry about it.
6+
7+
### Codegen
8+
9+
Run yarn to install all dependencies.
10+
11+
The `codegen.yml` file will be what handles generating TS types for queries and mutations.
12+
13+
The important part of this file is
14+
15+
```
16+
app/src/generated/graphql.tsx:
17+
documents: app/src/**/*.tsx
18+
plugins:
19+
- 'typescript'
20+
- 'typescript-operations'
21+
- 'typescript-react-apollo'
22+
```
23+
24+
This says to generate a file at `app/src/generated/graphql.tsx` based on the documents (`gql` tagged queries and mutations) that exist at `app/src/**/*.tsx` (file glob) and apply the listed plugins for the generation of code and types.
25+
26+
For example, check out `app/src/pages/graphql.ts`. There are two queries and a fragment for grabbing some movies.
27+
28+
Run the `codegen` command from the root. This will use the queries and mutations provided to created types and hooks and put it in the generated folder (`app/src/generated/graphql.tsx`). You can look in there for the output.
29+
30+
This is used in the `NowPlaying.tsx` component. Types for fragments are also generated. We map over the movies retured in the NowPlaying component and output MoviePoster components. Checkout `MoviePoster.tsx` for an example of using a generated fragment type.

Diff for: app/src/pages/graphql.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { gql } from '@apollo/client';
22

3-
// TODO: create fragment for movie object
43
export const MOVIE_FRAGMENT = gql`
54
fragment MovieFragment on Movie {
65
id

0 commit comments

Comments
 (0)