Skip to content

Commit df4a696

Browse files
authored
Feature 87: Adapters (#89)
1 parent 4b158fd commit df4a696

36 files changed

+598
-270
lines changed

dist/vuex-orm-graphql.es5.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.es5.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.umd.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vuex-orm-graphql.umd.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.vuepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module.exports = {
3030
collapsable: false,
3131
children: [
3232
'/guide/setup',
33-
'/guide/graphql',
3433
'/guide/fetch',
3534
'/guide/persist',
3635
'/guide/push',
@@ -43,6 +42,7 @@ module.exports = {
4342
title: 'Advanced Topics',
4443
collapsable: false,
4544
children: [
45+
'/guide/adapters',
4646
'/guide/connection-mode',
4747
'/guide/custom-queries',
4848
'/guide/eager-loading',

docs/guide/README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Introduction
22

3-
Vuex-ORM-GraphQL is a plugin for the amazing [Vuex-ORM](https://github.com/vuex-orm/vuex-orm), which brings
3+
Vuex-ORM/Plugin-GraphQL is a plugin for the amazing [Vuex-ORM](https://github.com/vuex-orm/vuex-orm), which brings
44
Object-Relational Mapping access to the Vuex Store. Vuex-ORM-GraphQL enhances Vuex-ORM to let you sync your Vuex state
55
via the Vuex-ORM models with your server via a [GraphQL API](http://graphql.org/).
66

7-
The plugin will automatically generate GraphQL queries and mutations based on your model definitions and by
8-
reading your and GraphQL schema from your server. Thus it hides the specifics of Network Communication, GraphQL,
9-
Caching, De- and Serialization of your Data and so on from the developer. Getting a record of a model from the server
7+
The plugin will automatically generate GraphQL queries and mutations on the fly based on your model definitions and by
8+
reading the GraphQL schema from your server. Thus it hides the specifics of Network Communication, GraphQL,
9+
Caching, De- and Serialization of your data, and so on from the developer. Getting a record of a model from the server
1010
is as easy as calling `Product.fetch()`. This allows you to write sophisticated Single-Page Applications fast and
1111
efficient without worrying about GraphQL.
1212

@@ -91,16 +91,18 @@ After [installing](setup.md) this plugin you can load data in your component:
9191
```
9292

9393
::: tip
94-
Vuex-ORM-GraphQL works with the [Apollo Dev Tools](https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm)!
94+
This plugin works with the [Apollo Dev Tools](https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm)!
9595
:::
9696

9797

98-
## Connection Mode
98+
## Adapters
9999

100-
It seems that there are several standards within the GraphQL community how connections (fields that returns multiple
101-
records) are designed. Some do this via a `nodes` field, some via a `edges { nodes }` query and some do neither of them.
102-
Vuex-ORM-GraphQL tries to be flexible and supports all of them, but the example queries in the documentation work with
103-
the `nodes` query, don't be irritated. You'll find [more details here](connection-mode.md).
100+
It seems that there are several standards within the GraphQL community how the schema is designed.
101+
Some do connections via a `nodes` field, some via a `edges { nodes }` query and some do neither of them.
102+
Some work with Input Types and some with plain parameter lists. This plugin supports all of them via
103+
a adapter pattern. There is a default adapter and all example queries in this documentation are
104+
generated by the DefaultAdapter but that doesn't mean the queries are tied to this format.
105+
When you have to customize it, you'll find how to do so in the [respective chapter](adapters.md).
104106

105107

106108
## License

docs/guide/adapters.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Adapters
2+
3+
[[toc]]
4+
5+
There is not single true way to design a GraphQL schema and thus there are
6+
some small differences between the implementations, however this plugin has to automatically
7+
generate GraphQL queries, has to parse the schema and de-/serialize the data. Thus we needed a way
8+
to customize how this plugin should behave and communicate with the API. For this we implemented an
9+
adapter pattern, which allows you to setup your own adapter and customize it.
10+
11+
12+
## Basics
13+
14+
Every adapter has to implement the `Adapter` interface (when your're working with TypeScript).
15+
However it's easier to just inherit from the DefaultAdapter:
16+
17+
`data/custom-adapter.js`:
18+
```javascript
19+
import DefaultAdapter from '@vuex-orm/plugin-graphql';
20+
21+
export default class CustomAdapter extends DefaultAdapter {
22+
// Your code here
23+
}
24+
```
25+
26+
Then register this adapter when setting up the plugin:
27+
28+
`data/store.js`:
29+
```javascript
30+
import CustomAdapter from './custom-adapter.ts';
31+
32+
// ...
33+
34+
VuexORM.use(VuexORMGraphQL, {
35+
database,
36+
adapter: CustomAdapter,
37+
});
38+
```
39+
40+
41+
That's it. In the next sections you can read what and how you can customize the adapter.
42+
43+
44+
## Methods
45+
46+
Each Adapter has to implement a bunch of methods. Here is the list of the currently supported
47+
method signatures and their value in the default adapter:
48+
49+
- `getRootQueryName(): string;`
50+
- Returns the name of the type all query types inherit.
51+
- Default adapter value: `Query`
52+
53+
- `getRootMutationName(): string;`
54+
- Returns the name of the type all mutation types inherit.
55+
- Default adapter value: `Mutation`
56+
57+
- `getNameForPersist(model: Model): string;`
58+
- Returns the mutation name for persisting (creating) a record.
59+
- Default adapter value example: `createPost`
60+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
61+
62+
- `getNameForPush(model: Model): string;`
63+
- Returns the mutation name for pushing (updating) a record.
64+
- Default adapter value example: `updatePost`
65+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
66+
67+
- `getNameForDestroy(model: Model): string;`
68+
- Returns the mutation name for destroying a record.
69+
- Default adapter value example: `deletePost`
70+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
71+
72+
- `getNameForFetch(model: Model, plural: boolean): string;`
73+
- Returns the query field for fetching a record.
74+
- Default adapter value example: `posts` or `post`
75+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
76+
- `plural` tells if one or multiple records (connection) are fetched.
77+
78+
- `getConnectionMode(): ConnectionMode;`
79+
- Returns the [ConnectionMode](connection-mode.md).
80+
- Default adapter value: `AUTO`
81+
82+
- `getArgumentMode(): ArgumentMode;`
83+
- Returns the ArgumentMode for filtering and inputs (push, persist).
84+
- Default adapter value: `TYPE`
85+
86+
- `getFilterTypeName(model: Model): string;`
87+
- Returns the name of the filter type for a model.
88+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
89+
- Default adapter value example: `PostFilter`
90+
91+
- `getInputTypeName(model: Model, action?: string): string;`
92+
- Returns the name of the input type for a model.
93+
- `model` is a instance of [Model](https://github.com/vuex-orm/plugin-graphql/blob/master/src/orm/model.ts)
94+
- Default adapter value example: `PostInput`
95+
96+
97+
## ArgumentMode
98+
99+
The `getArgumentMode()` methods determines the ArgumentMode, which knows to options: `LIST` and `TYPE`.
100+
It tells the plugin how arguments should be passed to queries and mutations.
101+
102+
103+
### `TYPE`
104+
105+
`TYPE` is the value in the default adapter and causes the plugin to use a `Input` or `Filter` type:
106+
107+
For `$persist()`:
108+
```
109+
mutation CreatePost($post: PostInput!) {
110+
createPost(post: $post) {
111+
...
112+
}
113+
}
114+
```
115+
116+
For `fetch()`:
117+
```
118+
query Posts($title: String!) {
119+
posts(filter: {title: $title}) {
120+
...
121+
}
122+
}
123+
```
124+
125+
126+
### `LIST`
127+
128+
`LIST` causes the plugin to use plain lists:
129+
130+
For `$persist()`:
131+
```
132+
mutation CreatePost($id: ID!, $authorId: ID!, $title: String!, $content: String!) {
133+
createPost(id: $id, authorId: $authorId, title: $title, content: $content) {
134+
...
135+
}
136+
}
137+
```
138+
139+
For `fetch()`:
140+
```
141+
query Posts($title: String!) {
142+
posts(title: $title) {
143+
...
144+
}
145+
}
146+
```

docs/guide/connection-mode.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
It seems that there are several standards within the GraphQL community how connections (fields that returns multiple
66
records) are designed. Some do this via a `nodes` field, some via a `edges { nodes }` query and some do neither of them.
7-
Vuex-ORM-GraphQL tries to be flexible and supports all of them, but the example queries in the documentation work with
8-
the `nodes` query, don't be irritated.
7+
Vuex-ORM-GraphQL tries to be flexible and supports all of them.
8+
9+
There are four possible modes: `AUTO`, `NODES`, `EDGES`, `PLAIN`. The Adapter you use will tell the
10+
plugin which ConnectionMode to use. In the DefaultAdapter this is `AUTO`.
911

1012

1113
## Automatic detection
1214

13-
The plugin will try to detect automatically which mode should be used by analyzing the GraphQL Schema. In the best
14-
case you don't have to bother with this at all.
15+
The plugin will try to detect automatically which mode should be used by analyzing the GraphQL
16+
Schema. In the best case you don't have to bother with this at all.
1517

1618

1719
## Manual setting
1820

19-
In rare cases the automatic detection might fail or report the wrong mode. In this case, you can manually set the
20-
`connectionQueryMode` config param to either `auto` (default), `nodes`, `edges`, `plain`. The modes and the resulting
21+
In rare cases the automatic detection might fail or report the wrong mode. In this case, you can
22+
manually set the connection mode via a custom adapter. The modes and the resulting
2123
queries are explained in the next sections.
2224

2325

docs/guide/graphql.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/guide/persist.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ Like when pushing, all records which are returned replace the respective existin
6565

6666
You can pass a object like this: `$perist({ captchaToken: 'asdfasdf' })`. All fields in the object will be passed as
6767
variables to the mutation.
68+
69+
70+
## Relationships
71+
72+
When persisting a record, all `belongsTo` relations are sent to the server too. `hasMany`/`hasOne`
73+
relations on the other hand are filtered out and have to be persisted on their own.

0 commit comments

Comments
 (0)