Skip to content

Commit e937643

Browse files
committed
add arguments to all actions & add complete readme
1 parent 06490d0 commit e937643

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
# React Apollo Redux
2+
3+
This library wraps the `graphql` function from [react-apollo](https://github.com/apollographql/react-apollo) function by also dispatching redux actions when a mutation is called.
4+
5+
If you were using
6+
```
7+
import { graphql } from 'react-apollo';
8+
```
9+
you may now do
10+
```
11+
import { graphql } from 'react-apollo-redux';
12+
```
13+
14+
### Usage
15+
16+
When a mutation is called, an action bearing the same name (snake and upper cased) and prefixed with `@@MUTATION` is dispatched. For example, a mutation like this
17+
```
18+
const FollowItems = gql`
19+
mutation followItems($itemIds: [ID]!) {
20+
followItems(item_ids: $itemIds) {
21+
id,
22+
},
23+
}
24+
`;
25+
26+
27+
graphql(FollowItems, {
28+
name: 'followItems',
29+
}),
30+
```
31+
Will dispatch the following actions with respective types:
32+
33+
#### type @@MUTATION/FOLLOW_ITEMS
34+
`@@MUTATION/[name]` contains the arguments to the mutation in its payload. For example, if the mutation was called this way
35+
```
36+
followItems({
37+
variables: {
38+
itemIds: itemIds,
39+
},
40+
extra: {
41+
user,
42+
references,
43+
},
44+
});
45+
```
46+
The payload will contain that object.
47+
48+
#### type @@MUTATION/FOLLOW_ITEMS_SUCCESS
49+
`@@MUTATION/[name]_SUCCESS` is dispatched once the mutation result is successfully returned from the server. The action payload contains two things:
50+
- The `result` from the server (as specified in the mutation)
51+
- The arguments (`args`) passed to the mutation, like above
52+
53+
54+
#### type @@MUTATION/FOLLOW_ITEMS_FAIL
55+
`@@MUTATION/[name]_FAIL` is dispatched if the server returns an error. This action contains the same payload as the initial action `@@MUTATION/FOLLOW_ITEMS`, and has an additional `meta` property with the error returned from the server.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function reduxGraphql(options) {
4444
catch (error) {
4545
dispatch({
4646
type: prefix + options.name + '_fail' |> snakeCase |> toUpper,
47+
payload: args[0],
4748
meta: { error },
4849
});
4950
throw error;

0 commit comments

Comments
 (0)