|
| 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 | +``` |
0 commit comments