Skip to content

Commit 3afe05f

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] 18 pages modified
1 parent e538da9 commit 3afe05f

File tree

13 files changed

+484
-608
lines changed

13 files changed

+484
-608
lines changed

README.md

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ Building your application with a single state tree is the most straight forward
3333
Separate 3rd party APIs and logic not specific to your application by using **effects**. This will keep your application logic pure and without low level APIs cluttering your code.
3434

3535
{% tabs %}
36-
{% tab title="api.ts" %}
37-
```typescript
38-
export const fetchItems = async (): Promise<Item[]> {
36+
{% tab title="api.js" %}
37+
```javascript
38+
export const fetchItems = async () {
3939
const response = await fetch('/api/items')
4040

4141
return response.json()
@@ -44,9 +44,9 @@ export const fetchItems = async (): Promise<Item[]> {
4444
```
4545
{% endtab %}
4646

47-
{% tab title="actions.ts" %}
47+
{% tab title="actions.js" %}
4848
```typescript
49-
export const loadApp: AsyncAction = ({ state, effects }) => {
49+
export const loadApp = ({ state, effects }) => {
5050
state.items = await effects.api.fetchItems()
5151
}
5252
```
@@ -57,8 +57,8 @@ export const loadApp: AsyncAction = ({ state, effects }) => {
5757

5858
When you build applications that perform many state changes things can get out of hand. In Overmind you can only perform state changes from **actions** and all changes are tracked by the development tool.
5959

60-
```typescript
61-
export const getItems: AsyncAction = async ({ state, effects }) => {
60+
```javascript
61+
export const getItems = async ({ state, effects }) => {
6262
state.isLoadingItems = true
6363
state.items = await effects.api.fetchItems()
6464
state.isLoadingItems = false
@@ -71,8 +71,8 @@ Even though Overmind can create applications with only plain **state** and **act
7171

7272
{% tabs %}
7373
{% tab title="Operators" %}
74-
```typescript
75-
export const search: Operator<string> = pipe(
74+
```javascript
75+
export const search = pipe(
7676
mutate(({ state }, query) => {
7777
state.query = query
7878
}),
@@ -88,16 +88,8 @@ export const search: Operator<string> = pipe(
8888
{% endtab %}
8989

9090
{% tab title="Statechart" %}
91-
```typescript
92-
const loginChart: Statechart<
93-
typeof config,
94-
{
95-
LOGIN: void
96-
AUTHENTICATING: void
97-
AUTHENTICATED: void
98-
ERROR: void
99-
}
100-
> = {
91+
```javascript
92+
const loginChart = {
10193
initial: 'LOGIN',
10294
states: {
10395
LOGIN: {
@@ -129,15 +121,15 @@ const loginChart: Statechart<
129121
{% endtab %}
130122

131123
{% tab title="Class state" %}
132-
```typescript
124+
```javascript
133125
class LoginForm() {
134-
private username: string = ''
135-
private password: string = ''
136-
private validationError: string = ''
137-
changeUsername(username: string) {
126+
private username = ''
127+
private password = ''
128+
private validationError = ''
129+
changeUsername(username) {
138130
this.username = username
139131
}
140-
changePassword(password: string) {
132+
changePassword(password) {
141133
if (!password.match([0-9]) {
142134
this.validationError = 'You need some numbers in your password'
143135
}
@@ -148,11 +140,7 @@ class LoginForm() {
148140
}
149141
}
150142

151-
type State = {
152-
loginForm: LoginForm
153-
}
154-
155-
export const state: State = {
143+
export const state = {
156144
loginForm: new LoginForm()
157145
}
158146
```
@@ -163,7 +151,7 @@ export const state: State = {
163151
164152
Bring in your application configuration of state, effects and actions. Create mocks for any effects. Take a snapshot of mutations performed in an action to ensure all intermediate states are met.
165153
166-
```typescript
154+
```javascript
167155
import { createOvermindMock } from 'overmind'
168156
import { config } from './'
169157

SUMMARY.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33
* [Overmind](README.md)
44
* [Introduction](introduction.md)
55
* [Quickstart](quickstart.md)
6+
* [How to learn](how-to-learn.md)
67
* [Videos](videos-1.md)
78
* [FAQ](faq.md)
89

910
## Core
1011

12+
* [Configuration](core/structuring-the-app.md)
1113
* [State](core/defining-state.md)
1214
* [Actions](core/writing-application-logic.md)
1315
* [Effects](core/running-side-effects.md)
1416
* [Operators](core/going-functional.md)
15-
* [Statecharts](features/statecharts.md)
17+
* [Statecharts](core/statecharts.md)
1618
* [Typescript](core/typescript.md)
1719

1820
## Addons
1921

20-
* [React](view-layers/react.md)
21-
* [Angular](view-layers/angular.md)
22-
* [Vue](view-layers/vue.md)
23-
* [GraphQL](features/graphql.md)
22+
* [React](addons/react.md)
23+
* [Angular](addons/angular.md)
24+
* [Vue](addons/vue.md)
25+
* [GraphQL](addons/graphql.md)
2426

2527
## Guides <a id="guides-1"></a>
2628

27-
* [Testing](guides-1/testing.md)
28-
* [Structuring the app](guides-1/structuring-the-app.md)
2929
* [Connecting components](guides-1/connecting-components.md)
3030
* [Managing lists](guides-1/managing-lists.md)
3131
* [State first routing](guides-1/state-first-routing.md)
32-
* [Server Side Rendering](features/server-side-rendering.md)
32+
* [Server Side Rendering](guides-1/server-side-rendering.md)
3333
* [Move to Typescript](guides-1/move-to-typescript.md)
34+
* [Testing](guides-1/testing.md)
3435

3536
## API <a id="api-1"></a>
3637

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# GraphQL
22

3-
4-
53
Using Graphql with Overmind gives you the following benefits:
64

75
* **Query:** The query for data is run with the rest of your application logic, unrelated to mounting components
@@ -25,7 +23,7 @@ npm install overmind-graphql
2523
The Graphql package is a _configuration factory_. That means you need some existing configuration before going:
2624

2725
{% tabs %}
28-
{% tab title="overmind/index.ts" %}
26+
{% tab title="overmind/index.js" %}
2927
```typescript
3028
import { state } from './state'
3129

@@ -35,16 +33,10 @@ export const config = {
3533
```
3634
{% endtab %}
3735

38-
{% tab title="overmind/state.ts" %}
36+
{% tab title="overmind/state.js" %}
3937
```typescript
40-
// We will talk about this one soon :)
41-
import { Post } from './graphql-types'
42-
43-
type State = {
44-
posts: Post[]
45-
}
4638

47-
export const state: State = {
39+
export const state = {
4840
posts: []
4941
}
5042
```
@@ -56,7 +48,7 @@ export const state: State = {
5648
Now let us introduce the factory:
5749

5850
{% tabs %}
59-
{% tab title="overmind/index.ts" %}
51+
{% tab title="overmind/index.js" %}
6052
```typescript
6153
import { graphql } from 'overmind-graphql'
6254
import * as queries from './queries'
@@ -73,12 +65,11 @@ export const config = graphql({
7365
```
7466
{% endtab %}
7567

76-
{% tab title="overmind/queries.ts" %}
68+
{% tab title="overmind/queries.js" %}
7769
```typescript
78-
import { Query, gql } from 'overmind-graphql'
79-
import { Posts } from './graphql-types'
70+
import { gql } from 'overmind-graphql'
8071

81-
export const posts: Query<Posts> = gql`
72+
export const posts = gql`
8273
query Posts {
8374
posts {
8475
id
@@ -89,12 +80,11 @@ export const posts: Query<Posts> = gql`
8980
```
9081
{% endtab %}
9182

92-
{% tab title="overmind/mutations.ts" %}
83+
{% tab title="overmind/mutations.js" %}
9384
```typescript
94-
import { Query, gql } from 'overmind-graphql'
95-
import { CreatePost, CreatePostVariables } from './graphql-types'
85+
import { gql } from 'overmind-graphql'
9686

97-
export const createPost: Query<CreatePost, CreatePostVariables> = gql`
87+
export const createPost = gql`
9888
mutation CreatePost($title: String!) {
9989
createPost(title: $title) {
10090
id
@@ -112,7 +102,7 @@ You define **queries** and **mutations** as part of the second argument to the f
112102
To call a query you will typically use an action. Let us create an action that uses our **posts** query.
113103

114104
{% tabs %}
115-
{% tab title="overmind/index.ts" %}
105+
{% tab title="overmind/index.js" %}
116106
```typescript
117107
import { graphql } from 'overmind-graphql'
118108
import * as actions from './actions'
@@ -131,11 +121,9 @@ export const config = graphql({
131121
```
132122
{% endtab %}
133123

134-
{% tab title="overmind/actions.ts" %}
124+
{% tab title="overmind/actions.js" %}
135125
```typescript
136-
import { AsyncAction } from 'overmind'
137-
138-
export const getPosts: AsyncAction = async ({ state, effects }) => {
126+
export const getPosts = async ({ state, effects }) => {
139127
const { posts } = await effects.queries.posts()
140128

141129
state.posts = posts
@@ -149,17 +137,15 @@ export const getPosts: AsyncAction = async ({ state, effects }) => {
149137
Mutation queries are basically the same as normal queries. You would typically also call these from an action.
150138

151139
{% tabs %}
152-
{% tab title="overmind/actions.ts" %}
140+
{% tab title="overmind/actions.js" %}
153141
```typescript
154-
import { AsyncAction } from 'overmind'
155-
156-
export const getPosts: AsyncAction = async ({ state, effects }) => {
142+
export const getPosts = async ({ state, effects }) => {
157143
const { posts } = await effects.queries.posts()
158144

159145
state.posts = posts
160146
}
161147

162-
export const addPost: AsyncAction<string> = async ({ effects }, title) => {
148+
export const addPost = async ({ effects }, title) => {
163149
await effects.mutations.createPost({ title })
164150
}
165151
```
@@ -175,17 +161,15 @@ Now that we have the data from our query in the state, we can decide ourselves w
175161
Again, since our data is just part of our state we are in complete control of optimistically adding new data. Let us create an optimistic post.
176162

177163
{% tabs %}
178-
{% tab title="overmind/actions.ts" %}
164+
{% tab title="overmind/actions.js" %}
179165
```typescript
180-
import { AsyncAction } from 'overmind'
181-
182-
export const getPosts: AsyncAction = async ({ state, effects }) => {
166+
export const getPosts = async ({ state, effects }) => {
183167
const { posts } = await effects.queries.posts()
184168

185169
state.posts = posts
186170
}
187171

188-
export const addPost: AsyncAction<string> = async ({ state, effects }, title) => {
172+
export const addPost = async ({ state, effects }, title) => {
189173
const optimisticId = String(Date.now())
190174

191175
state.posts.push({
@@ -209,7 +193,7 @@ There are two points of options in the Graphql factory. The **headers** and the
209193
The headers option is a function which receives the state of the application. That means you can produce request headers dynamically. This can be useful related to authentciation.
210194

211195
{% tabs %}
212-
{% tab title="overmind/index.ts" %}
196+
{% tab title="overmind/index.js" %}
213197
```typescript
214198
import { graphql } from 'overmind-graphql'
215199
import * as queries from './queries'
@@ -233,7 +217,7 @@ export const config = graphql({
233217
The options are the options passed to [GRAPHQL-REQUEST](https://github.com/prisma-labs/graphql-request).
234218

235219
{% tabs %}
236-
{% tab title="overmind/index.ts" %}
220+
{% tab title="overmind/index.js" %}
237221
```typescript
238222
import { graphql } from 'overmind-graphql'
239223
import * as queries from './queries'
@@ -258,7 +242,50 @@ export const config = graphql({
258242
{% endtab %}
259243
{% endtabs %}
260244

261-
## Generate typings
245+
## Typescript
246+
247+
There is only a single type exposed by the library, **Query**. It is used for both queries and mutations.
248+
249+
{% tabs %}
250+
{% tab title="overmind/queries.ts" %}
251+
```typescript
252+
import { Query, gql } from 'overmind-graphql'
253+
// You will understand this very soon
254+
import { Posts } from './graphql-types'
255+
256+
export const posts: Query<Posts> = gql`
257+
query Posts {
258+
posts {
259+
id
260+
title
261+
}
262+
}
263+
`;
264+
```
265+
{% endtab %}
266+
{% endtabs %}
267+
268+
The first **Query** argument is the result of the query. There is also a second query argument which is the payload to the query, as seen here.
269+
270+
{% tabs %}
271+
{% tab title="overmind/mutations.ts" %}
272+
```typescript
273+
import { Query, gql } from 'overmind-graphql'
274+
// You will understand this very soon
275+
import { CreatePost, CreatePostVariables } from './graphql-types'
276+
277+
export const createPost: Query<CreatePost, CreatePostVariables> = gql`
278+
mutation CreatePost($title: String!) {
279+
createPost(title: $title) {
280+
id
281+
}
282+
}
283+
`
284+
```
285+
{% endtab %}
286+
{% endtabs %}
287+
288+
### Generate typings
262289

263290
It is possible to generate all the typings for the queries and mutations. This is done by using the [APOLLO](https://www.apollographql.com/) project CLI. Install it with:
264291

@@ -284,6 +311,10 @@ npm run schema
284311

285312
Apollo will look for queries defined with the **gql** template tag and automatically produce the typings. That means whenever you add, remove or update a query in your code you should run this script to update the typings. It also produces what is called **graphql-global-types**. These are types related to fields on your queries, which can be used in your state definition and/or actions.
286313

314+
{% hint style="info" %}
315+
Note that initially you have to define your queries without types and after running the script you can start typing them to get typing in your app and ensure that your app does not break when you change the queries either in the client or on the server
316+
{% endhint %}
317+
287318
## Optimize query
288319

289320
It is possible to transpile the queries from strings into code. This reduces the size of your bundle, though only noticeably if you have a lot of queries. This can be done with the [BABEL-PLUGIN-GRAPHQL-TAG](https://github.com/gajus/babel-plugin-graphql-tag).

0 commit comments

Comments
 (0)