Skip to content

Commit 316679d

Browse files
committed
Add convenience methods to docs
1 parent f83d845 commit 316679d

File tree

9 files changed

+83
-32
lines changed

9 files changed

+83
-32
lines changed

docs/guide/README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ The following table lists all actions and what they do:
3131

3232
CRUD | Vuex Only | Persist to GraphQL API
3333
--| -- | --
34-
**R**EAD | getters['find'] & getters['findAll'] | [dispatch('fetch')](/guide/fetch)
35-
**C**REATE | dispatch('create') | [dispatch('persist')](/guide/persist)
36-
**U**PDATE | dispatch('save') | [dispatch('push')](/guide/push)
37-
**D**ELETE | dispatch('delete') | [dispatch('destroy')](/guide/destroy)
34+
**R**EAD | [`find()`](https://vuex-orm.github.io/vuex-orm/store/retrieving-data.html#get-single-data), [`all()`](https://vuex-orm.github.io/vuex-orm/store/retrieving-data.html#get-all-data), [`query()`](https://vuex-orm.github.io/vuex-orm/store/retrieving-data.html#query-builder) | [`fetch()`](/guide/fetch)
35+
**C**REATE | [`create()`](https://vuex-orm.github.io/vuex-orm/store/inserting-and-updating-data.html#inserts) or [`insert()`](https://vuex-orm.github.io/vuex-orm/store/inserting-and-updating-data.html#inserts) | [`$persist()`](/guide/persist)
36+
**U**PDATE | [`$update()`](https://vuex-orm.github.io/vuex-orm/store/inserting-and-updating-data.html#updates) | [`$push()`](/guide/push)
37+
**D**ELETE | [`$delete()`](https://vuex-orm.github.io/vuex-orm/store/deleting-data.html) | [`$destroy()`](/guide/destroy)
3838

3939
See the example below to get an idea of how this plugin interacts with Vuex-ORM.
4040

@@ -65,13 +65,13 @@ After [installing](/guide/setup) this plugin you can load data in your component
6565
/**
6666
* Returns all users with reactivity.
6767
*/
68-
users: () => User.getters['all']()
68+
users: () => User.all()
6969
},
7070
7171
7272
async mounted() {
7373
// Load all users form the server
74-
await User.dispatch('fetch');
74+
await User.fetch();
7575
},
7676
7777
@@ -80,8 +80,7 @@ After [installing](/guide/setup) this plugin you can load data in your component
8080
* Deletes the user from Vuex Store and from the server.
8181
*/
8282
async destroy(user) {
83-
User.dispatch('delete', user.id);
84-
await User.dispatch('destroy', { id: user.id });
83+
await user.$deleteAndDestroy();
8584
}
8685
}
8786
}

docs/guide/custom-mutations/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
Along with the CRUD mutations you may want to send custom GraphQL mutations. We support this via the `mutate` action:
77

88
```javascript
9-
Post.dispatch('mutate', { mutation: 'upvotePost', id: post.id });
9+
const post = Post.query().first();
10+
await post.$mutate({ mutation: 'upvotePost' });
11+
12+
// is the same as
13+
await Post.mutate({ mutation: 'upvotePost', id: post.id });
14+
15+
// or
16+
await Post.dispatch('mutate', { mutation: 'upvotePost', id: post.id });
1017
```
1118

1219
As you can see you have to privide the mutation name and any further arguments you want to pass. In this case we send

docs/guide/destroy/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
[[toc]]
44

5+
6+
## $destroy()
7+
58
Last thing you can do with a record is to delete it on the server after deleting (`delete` action) it on the client via
69
Vuex-ORM. For this use case we have the `destroy` action.
710

811
Via calling
912

1013
```javascript
11-
Post.dispatch('destroy', { id: post.id });
14+
await post.$destroy();
15+
// or
16+
await post.$dispatch('destroy', { id: post.id });
1217
```
1318

1419
the following GraphQL query is generated:
@@ -30,3 +35,7 @@ Variables:
3035
}
3136
```
3237

38+
## $deleteAndDestroy()
39+
40+
You can also use the `$deleteAndDestroy()` action to delete the record from the store and from the server. It's just a
41+
short convenience method for `$delete()` and `$destroy()`.

docs/guide/faq/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
[[toc]]
44

5+
56
## Is this plugin nativescript-vue compatible?
67

78
Yes, since version `0.0.38`.
9+
10+
11+
## What is `await`?
12+
13+
It's a nice way to work with promises. See https://javascript.info/async-await.

docs/guide/fetch/README.md

+23-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
[[toc]]
44

5-
The `fetch` action is for loading data from the GraphQL API into your Vuex-Store. The simplest way to call the fetch
5+
The `fetch` action is for loading data from the GraphQL API into your Vuex-Store.
6+
7+
8+
## Fetching all records
9+
10+
The simplest way to call the fetch
611
method is without any arguments. This will query all records from the GraphQL API:
712

813
```javascript
9-
Comment.dispatch('fetch');
14+
await Comment.fetch();
15+
// or
16+
await Comment.dispatch('fetch')
1017
```
1118

1219
This produces the following GraphQL query:
@@ -46,7 +53,7 @@ author user loaded.
4653
So using the regular Vuex-ORM getters should work out of the box now:
4754

4855
```javascript
49-
const comments = Comment.getters('all');
56+
const comments = Comment.all();
5057
```
5158

5259
When fetching all returned records replace the respective existing records in the Vuex-ORM database.
@@ -57,7 +64,9 @@ When fetching all returned records replace the respective existing records in th
5764
You can also fetch single records via ID:
5865

5966
```javascript
60-
Comment.dispatch('fetch', { filter: { id: 42 }});
67+
await Comment.fetch({ id: 42 });
68+
// or
69+
await Comment.dispatch('fetch', { filter: { id: 42 }})
6170
```
6271

6372
It automatically recognizes, that you're requesting a single record and sends a GraphQL Query for a single record:
@@ -95,7 +104,9 @@ query Comment($id: ID!) {
95104
Additionally you can pass a filter object to the fetch action like this:
96105

97106
```javascript
98-
Comment.dispatch('fetch', { filter: { postId: 15, deleted: false }});
107+
await Comment.fetch({ postId: 15, deleted: false });
108+
// or
109+
await Comment.dispatch('fetch', { filter: { postId: 15, deleted: false }})
99110
```
100111

101112
This will generate the following GraphQL query:
@@ -147,7 +158,7 @@ recommend the usage of async/await.
147158
export default {
148159
// Use a computed property for the component state to keep it reactive
149160
computed: {
150-
post: () => Post.getters('find')(1)
161+
post: () => Post.find(1)
151162
},
152163
153164
created () {
@@ -164,7 +175,7 @@ recommend the usage of async/await.
164175
methods: {
165176
// Loads the data from the server and stores them in the Vuex Store.
166177
async fetchData () {
167-
await Post.dispatch('fetch', { id: this.$route.params.id });
178+
await Post.fetch({ filter: { id: this.$route.params.id }});
168179
}
169180
}
170181
}
@@ -174,8 +185,11 @@ recommend the usage of async/await.
174185

175186
## Caching
176187

177-
Apollo-Client caches same queries. To bypass caching set the second param of the `fetch` action to `true`:
188+
Apollo-Client caches same queries. To bypass caching set the second param of the `fetch` action to `true`
189+
when using the convenience method or add `bypassCache: true` to the arguments of the `dispatch()` call
178190

179191
```javascript
180-
User.dispatch('fetch', { filter: { id: 42 }, bypassCache: true });
192+
await Comment.fetch({ id: 42 }, true );
193+
// Or
194+
await Comment.dispatch('fetch', { filter: { id: 42 }, bypassCache: true })
181195
```

docs/guide/graphql/README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
This plugin has an opinion of how the GraphQL API schema should look like:
66

7-
- Query for multiple records is plural camelCase: `blogPosts`.
8-
- Mutations begin with the verb (`create`, `update`, `delete`) and the camelCased entity: `createBlogPost` for example.
9-
- The create mutation expects the new record as a input type argument.
10-
- The update mutation expects two arguments: The ID and the new record as a input type.
11-
- The delete mutation expects the record ID to delete.
7+
- Query name for single record is singular camelCase: `blogPost`.
8+
- Query name for multiple records is plural camelCase: `blogPosts`.
129
- Multiple records are within a `nodes` object and filtered by a `filter` argument.
10+
- Mutations begin with the verb (`create`, `update`, `delete`) and the camelCased entity: `createBlogPost` for example.
11+
- The create mutation (generated by `$persist`) expects the new record as a input type argument (`createBlogPost(blogPost: BlogPostInput!)`).
12+
- The update mutation (generated by `$push`) expects two arguments: The ID and the new record as a input type (`updateBlogPost(id: ID!, blogPost: BlogPostInput!)`).
13+
- The delete mutation (generated by `$destroy`) expects the record ID to delete (`deleteBlogPost(id: ID!)`).
1314

14-
You will find concrete examples of the GraphQL queries which are generated by this plugin in the
15-
respective chapters of this documentation.
15+
You will find concrete examples of the GraphQL queries which are generated by this plugin in the respective chapters of
16+
this documentation. Also we recommend to activate the debug mode of the plugin, which will show you the queries which
17+
are sent.

docs/guide/persist/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ the `persist` action.
99
Via calling
1010

1111
```javascript
12-
Post.dispatch('persist', { id: post.id });
12+
const post = await Post.create({
13+
content: 'Lorem Ipsum dolor sit amet',
14+
title: 'Example Post',
15+
user: user.query().first()
16+
});
17+
18+
await post.$persist();
19+
// or
20+
await post.$dispatch('persist', { id: post.id });
1321
```
1422

1523
the post record is send to the GraphQL by generating the following query:

docs/guide/push/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ your server via GraphQL. For this use case we have the `push` action.
88
Via calling
99

1010
```javascript
11-
Post.dispatch('push', { data: post });
11+
const post = Post.query().first();
12+
await await post.$push();
13+
// or
14+
await post.$dispatch('push', { data: post });
1215
```
1316

1417
the post record is send to the GraphQL by generating the following query:

docs/guide/setup/README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
[[toc]]
44

5+
6+
## Installation
7+
58
Installation of the Apollo plugin is easy. First add the package to your dependencies:
69

710
```bash
@@ -15,20 +18,20 @@ $ npm install --save @vuex-orm/plugin-apollo
1518
```
1619

1720

18-
After that we setup the plugin. Add this after registering your models to the database:
21+
After that we setup the plugin. Add this after [registering your models to the database](https://vuex-orm.github.io/vuex-orm/prologue/getting-started.html#register-models-and-modules-to-the-vuex-store):
1922

2023
```javascript
2124
import VuexORMApollo from '@vuex-orm/plugin-apollo';
22-
VuexORM.use(VuexORMApollo, { database: database });
25+
VuexORM.use(VuexORMApollo, { database });
2326
```
2427

2528
## Possible options
2629

2730
These are the possible options to pass when calling `VuexORM.use()`:
2831

2932
- `database` (required): The Vuex-ORM database.
30-
- `url` (optional, default: '/graphql'): The URL to the graphql api. Will be passed to apollo-client.
31-
- `debug` (optional, default: false): Set to true to activate the debug logging.
33+
- `url` (optional, default: `/graphql`): The URL to the graphql api. Will be passed to apollo-client.
34+
- `debug` (optional, default: `false`): Set to true to activate the debug logging.
3235

3336
More options will come in future releases.
3437

0 commit comments

Comments
 (0)