Skip to content

Commit cccb703

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [v23] 15 pages modified
1 parent 0df64bd commit cccb703

File tree

13 files changed

+294
-176
lines changed

13 files changed

+294
-176
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: frictionless state management
66

77
> Web application development is about **defining**, **changing** and **consuming state** to produce a user experience. Overmind aims for a developer experience where that is all you focus on, reducing the orchestration of state management to a minimum. Making you a **happier** and more **productive** developer!
88
9-
{% embed url="https://overmindjs.changefeed.app/general/v22.0.0" %}
9+
{% embed url="https://overmindjs.changefeed.app/general/v23" %}
1010

1111
## APPLICATION INSIGHT
1212

SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* [Effects](core/running-side-effects.md)
1717
* [Operators](core/going-functional.md)
1818
* [Statecharts](core/statecharts.md)
19-
* [Server Side Rendering](guides-1/server-side-rendering.md)
19+
* [Server Side Rendering](core/server-side-rendering.md)
2020
* [Typescript](core/typescript.md)
2121

2222
## views
@@ -36,7 +36,7 @@
3636
* [Managing lists](guides-1/managing-lists.md)
3737
* [State first routing](guides-1/state-first-routing.md)
3838
* [Move to Typescript](guides-1/move-to-typescript.md)
39-
* [Testing](features/testing.md)
39+
* [Testing](guides-1/testing.md)
4040

4141
## API <a id="api-1"></a>
4242

addons/graphql.md

Lines changed: 117 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ Using Graphql with Overmind gives you the following benefits:
66
* **Cache:** You integrate the data from Graphql with your existing state, allowing you to control when new data is needed
77
* **Optimistic updates:** With the data integrated with your Overmind state you can also optimistically update that state before running a mutation query
88

9-
{% hint style="info" %}
10-
The Graphql package does not support **subscriptions** currently
11-
{% endhint %}
12-
139
## Get up and running
1410

1511
Install the separate package:
@@ -20,7 +16,7 @@ npm install overmind-graphql
2016

2117
### Initial state
2218

23-
The Graphql package is a _configuration factory_. That means you need some existing configuration before going:
19+
The Graphql package is an _effect_. Though since we are operating on state, let us prepare some:
2420

2521
{% tabs %}
2622
{% tab title="overmind/index.js" %}
@@ -43,29 +39,57 @@ export const state = {
4339
{% endtab %}
4440
{% endtabs %}
4541

46-
### The factory
42+
### The effect
4743

48-
Now let us introduce the factory:
44+
Now let us introduce the effect:
4945

5046
{% tabs %}
5147
{% tab title="overmind/index.js" %}
5248
```typescript
49+
import { state } from './state'
50+
import { onInitialize } from './onInitialize'
51+
import { gql } from './effects/gql'
52+
53+
export const config = {
54+
onInitialize,
55+
state,
56+
effects: {
57+
gql
58+
}
59+
}
60+
```
61+
{% endtab %}
62+
63+
{% tab title="overmind/onInitialize.js" %}
64+
```javascript
65+
export const onInitialize = ({ effects }) => {
66+
effects.gql.initialize({
67+
// query and mutation options
68+
endpoint: 'http://some-endpoint.dev',
69+
}, {
70+
// subscription options
71+
endpoint: 'ws://some-endpoint.dev',
72+
})
73+
}
74+
```
75+
{% endtab %}
76+
77+
{% tab title="overmind/effects/gql/index.js" %}
78+
```javascript
5379
import { graphql } from 'overmind-graphql'
5480
import * as queries from './queries'
5581
import * as mutations from './mutations'
56-
import { state } from './state'
82+
import * as subscriptions from './subscriptions'
5783

58-
export const config = graphql({
59-
state
60-
}, {
61-
endpoint: 'http://some-endpoint.dev',
84+
export const gql = graphql({
6285
queries,
63-
mutations
86+
mutations,
87+
subscriptions
6488
})
6589
```
6690
{% endtab %}
6791

68-
{% tab title="overmind/queries.js" %}
92+
{% tab title="overmind/effects/gql/queries.js" %}
6993
```typescript
7094
import { gql } from 'overmind-graphql'
7195

@@ -80,7 +104,7 @@ export const posts = gql`
80104
```
81105
{% endtab %}
82106

83-
{% tab title="overmind/mutations.js" %}
107+
{% tab title="overmind/effects/gql/mutations.js" %}
84108
```typescript
85109
import { gql } from 'overmind-graphql'
86110

@@ -93,60 +117,82 @@ export const createPost = gql`
93117
`
94118
```
95119
{% endtab %}
120+
121+
{% tab title="overmind/effects/gql/subscriptions.js" %}
122+
```javascript
123+
import { gql } from 'overmind-graphql'
124+
125+
export const onPostAdded = gql`
126+
subscription PostAdded() {
127+
postAdded() {
128+
id
129+
title
130+
}
131+
}
132+
`
133+
```
134+
{% endtab %}
96135
{% endtabs %}
97136

98-
You define **queries** and **mutations** as part of the second argument to the factory, with what **endpoint** you want to connect to. These queries and mutations are converted into Overmind effects that you can call from your actions.
137+
You define **queries,** **mutations** and **subscriptions** with the effect. That means you can have multiple effects holding different queries and even endpoints. The endpoints are defined when you initialize the effect. This allows you to dynamically create the endpoints based on state, and also pass state related to requests to the endpoints. The queries, mutations and subscriptions are converted into Overmind effects that you can call from your actions.
99138

100139
## Query
101140

102141
To call a query you will typically use an action. Let us create an action that uses our **posts** query.
103142

104143
{% tabs %}
105-
{% tab title="overmind/index.js" %}
144+
{% tab title="overmind/actions.js" %}
106145
```typescript
107-
import { graphql } from 'overmind-graphql'
108-
import * as actions from './actions'
109-
import * as queries from './queries'
110-
import * as mutations from './mutations'
111-
import { state } from './state'
146+
export const getPosts = async ({ state, effects }) => {
147+
const { posts } = await effects.gql.queries.posts()
112148

113-
export const config = graphql({
114-
state,
115-
actions
116-
}, {
117-
endpoint: 'http://some-endpoint.dev',
118-
queries,
119-
mutations
120-
})
149+
state.posts = posts
150+
}
121151
```
122152
{% endtab %}
153+
{% endtabs %}
154+
155+
## Mutate
156+
157+
Mutation queries are basically the same as normal queries. You would typically also call these from an action.
123158

159+
{% tabs %}
124160
{% tab title="overmind/actions.js" %}
125161
```typescript
126162
export const getPosts = async ({ state, effects }) => {
127-
const { posts } = await effects.queries.posts()
163+
const { posts } = await effects.gql.queries.posts()
128164

129165
state.posts = posts
130166
}
167+
168+
export const addPost = async ({ effects }, title) => {
169+
await effects.gql.mutations.createPost({ title })
170+
}
131171
```
132172
{% endtab %}
133173
{% endtabs %}
134174

135-
## Mutate
175+
## Subscription
136176

137-
Mutation queries are basically the same as normal queries. You would typically also call these from an action.
177+
Subscriptions are also available via actions. You typically give them an action which triggers whenever the subscription triggers.
138178

139179
{% tabs %}
140180
{% tab title="overmind/actions.js" %}
141181
```typescript
142-
export const getPosts = async ({ state, effects }) => {
143-
const { posts } = await effects.queries.posts()
182+
export const getPosts = async ({ state, effects, actions }) => {
183+
const { posts } = await effects.gql.queries.posts()
144184

145185
state.posts = posts
186+
187+
effects.gql.subscriptions.onPostAdded(actions.onPostAdded)
146188
}
147189

148190
export const addPost = async ({ effects }, title) => {
149-
await effects.mutations.createPost({ title })
191+
await effects.gql.mutations.createPost({ title })
192+
}
193+
194+
export const onPostAdded = ({ state }, post) => {
195+
state.posts.push(post)
150196
}
151197
```
152198
{% endtab %}
@@ -193,58 +239,55 @@ There are two points of options in the Graphql factory. The **headers** and the
193239
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.
194240

195241
{% tabs %}
196-
{% tab title="overmind/index.js" %}
242+
{% tab title="overmind/onInitialize.js" %}
197243
```typescript
198-
import { graphql } from 'overmind-graphql'
199-
import * as queries from './queries'
200-
import * as mutations from './mutations'
201-
import { state } from './state'
202-
203-
export const config = graphql({
204-
state
205-
}, {
206-
endpoint: 'http://some-endpoint.dev',
207-
headers: (state) => ({
208-
authorization: `Bearer ${state.auth.token}`
209-
}),
210-
queries,
211-
mutations
212-
})
244+
export const onInitialize = ({ state, effects }) => {
245+
effects.gql.initialize({
246+
endpoint: 'http://some-endpoint.dev',
247+
// This runs on every request
248+
headers: () => ({
249+
authorization: `Bearer ${state.auth.token}`
250+
}),
251+
// The options are the options passed to GRAPHQL-REQUEST
252+
options: {
253+
credentials: 'include',
254+
mode: 'cors',
255+
},
256+
}, {
257+
endpoint: 'ws://some-endpoint.dev',
258+
// This runs on every connect
259+
params: () => ({
260+
token: state.auth.token
261+
})
262+
})
263+
}
213264
```
214265
{% endtab %}
215266
{% endtabs %}
216267

217-
The options are the options passed to [GRAPHQL-REQUEST](https://github.com/prisma-labs/graphql-request).
268+
## Disposing subscriptions
269+
270+
You can dispose any subscriptions in any action. There are two ways to dispose:
218271

219272
{% tabs %}
220-
{% tab title="overmind/index.js" %}
273+
{% tab title="overmind/actions.js" %}
221274
```typescript
222-
import { graphql } from 'overmind-graphql'
223-
import * as queries from './queries'
224-
import * as mutations from './mutations'
225-
import { state } from './state'
226-
227-
export const config = graphql({
228-
state
229-
}, {
230-
endpoint: 'http://some-endpoint.dev',
231-
headers: (state) => ({
232-
authorization: `Bearer ${state.auth.token}`
233-
}),
234-
options: {
235-
credentials: 'include',
236-
mode: 'cors',
237-
},
238-
queries,
239-
mutations
240-
})
275+
export const disposeSubscriptions = async ({ state, effects }) => {
276+
// Disposes all subscriptions on "onPostAdded"
277+
effects.gql.subscriptions.onPostAdded.dispose()
278+
// If the subscription takes a payload, you can dispose specific
279+
// subscriptions
280+
effects.gql.subscriptions.onPostChange.disposeWhere(
281+
data => data.id === state.currentPostId
282+
)
283+
}
241284
```
242285
{% endtab %}
243286
{% endtabs %}
244287

245288
## Typescript
246289

247-
There is only a single type exposed by the library, **Query**. It is used for both queries and mutations.
290+
There is only a single type exposed by the library, **Query**. It is used for queries, mutations and subscriptions.
248291

249292
{% tabs %}
250293
{% tab title="overmind/queries.ts" %}

api-1/addmutationlistener.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# addMutationListener
22

3-
It is possible to listen to all mutations performed in Overmind. This allows you to create special effects based on mutations within a certain domain of your app, or whatever else you come up with. Note that this method triggers before the mutation occurs, you might rather want to use **addFlushListener** to be notified about batched changes, like the components does.
3+
It is possible to listen to all mutations performed in Overmind. This allows you to create special effects based on mutations within a certain domain of your app, or whatever else you come up with. Note that this method triggers right after any mutation occurs, you might rather want to use **addFlushListener** to be notified about batched changes, like the components does.
44

55
{% tabs %}
66
{% tab title="overmind/onInitialize.ts" %}

api-1/createovermind.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ const overmind = createOvermind(config, {
7070
})
7171
```
7272

73+
## options.delimiter
74+
75+
By default Overmind will create state paths using `.` as delimiter. This is used to give each state value an address and is used with the devtools. If any state keys uses `.` you will get weird behaviour in the devtools. You can now change this delimiter to a safe value, typically `' '` or `'|'` :
76+
77+
```typescript
78+
const overmind = createOvermind(config, {
79+
delimiter: '.'
80+
})
81+
```
82+
7383
## events
7484

7585
Overmind emits events during execution of actions and similar. It can be beneficial to listen to these events for analytics or maybe you want to create a custom debugging experience. The following events can be listened to by adding a listener to the eventHub:

0 commit comments

Comments
 (0)