Skip to content

Commit c9c7f5e

Browse files
committed
More tests and bugfixes
1 parent a061bb2 commit c9c7f5e

File tree

4 files changed

+322
-65
lines changed

4 files changed

+322
-65
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export default class VuexORMApollo {
191191
* @param {Model|string} model
192192
* @returns {Model}
193193
*/
194-
private getModel (model: Model|string): Model {
194+
public getModel (model: Model|string): Model {
195195
if (!(model instanceof Model)) {
196196
model = this.models.get(inflection.singularize(model)) as Model;
197197
if (!model) throw new Error(`No such model ${model}!`);

src/queryBuilder.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,33 @@ export default class QueryBuilder {
4141
*
4242
* There are three types of arguments:
4343
*
44-
* 1) Signatures with attributes (signature = true)
44+
* 1) Signatures with simple types (signature = true)
4545
* mutation createUser($name: String!)
4646
*
47-
* 2) Signatures with object (signature = true, args = { user: { __type: 'User' }})
48-
* mutation createUser($user: User!)
47+
* 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
48+
* mutation createUser($user: UserInput!)
4949
*
50-
* 3) Field with values (signature = false, valuesAsVariables = false)
51-
* user(id: 15)
50+
* 3) Fields with values (signature = false, valuesAsVariables = false)
51+
* query user(id: 15)
5252
*
53-
* 4) Field with variables (signature = false, valuesAsVariables = true)
54-
* user(id: $id)
53+
* 4) Fields with variables (signature = false, valuesAsVariables = true)
54+
* query user(id: $id)
5555
*
56-
* 5) Field with object value (signature = false, valuesAsVariables = false, args = { user: { __type: 'User' }})
57-
* createUser(user: {...})
56+
* 5) Fields with object value (signature = false, valuesAsVariables = false, args = { user: { __type: 'User' }})
57+
* mutation createUser(user: {...})
5858
*
5959
* @param {Arguments | undefined} args
6060
* @param {boolean} signature When true, then this method generates a query signature instead of key/value pairs
6161
* @param {boolean} valuesAsVariables When true and abstract = false, then this method generates filter arguments with
6262
* variables instead of values
6363
* @returns {String}
6464
*/
65-
public buildArguments(args: Arguments | undefined, signature: boolean = false,
66-
valuesAsVariables: boolean = false): string {
65+
public buildArguments(args: Arguments | undefined,
66+
signature: boolean = false,
67+
valuesAsVariables: boolean = false): string {
6768
let returnValue: string = '';
6869
let any: boolean = false;
70+
let first: boolean = true;
6971

7072
if (args) {
7173
Object.keys(args).forEach((key: string) => {
@@ -90,14 +92,15 @@ export default class QueryBuilder {
9092
} else {
9193
if (typeof value === 'object' && value.__type) {
9294
// Case 3 ({name: 'Helga Hufflepuff"})
93-
typeOrValue = value;
95+
typeOrValue = JSON.stringify(value);
9496
} else {
9597
// Case 3 ("someValue")
9698
typeOrValue = typeof value === 'number' ? value : `"${value}"`;
9799
}
98100
}
99101

100-
returnValue = `${returnValue} ${(signature ? '$' : '') + key}: ${typeOrValue}`;
102+
returnValue = `${returnValue}${first ? '' : ', '}${(signature ? '$' : '') + key}: ${typeOrValue}`;
103+
first = false;
101104
}
102105
});
103106

@@ -108,17 +111,24 @@ export default class QueryBuilder {
108111
}
109112

110113

111-
112-
113-
114+
/**
115+
* Transforms outgoing data. Use for variables param.
116+
*
117+
* Omits relations and id fields.
118+
*
119+
* @param {Data} data
120+
* @returns {Data}
121+
*/
114122
public transformOutgoingData(data: Data): Data {
123+
const model: Model = this.getModel(data.$self().entity);
124+
const relations: Map<string, Field> = model.getRelations();
115125
const returnValue: Data = {};
116126

117127
Object.keys(data).forEach((key) => {
118128
const value = data[key];
119129

120130
// Ignore IDs and connections
121-
if (!(value instanceof Array || key === 'id')) {
131+
if (!relations.has(key) && key !== 'id') {
122132
returnValue[key] = value;
123133
}
124134
});

test/support/Helpers.js

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,10 @@ import _ from 'lodash';
22
import Vue from 'vue';
33
import Vuex from 'vuex';
44
import VuexORM, { Database, Model } from '@vuex-orm/core';
5+
// import installVuexORMApollo from 'app';
56

67
Vue.use(Vuex);
78

8-
/**
9-
* Create whole application.
10-
*/
11-
export function createApplication (namespace, entities) {
12-
const database = new Database();
13-
14-
_.forEach(entities, (entity) => {
15-
database.register(entity.model, entity.module || {})
16-
});
17-
18-
database.registerNamespace(namespace);
19-
20-
return database;
21-
}
22-
239
/**
2410
* Create a new Vuex Store.
2511
*/
@@ -30,39 +16,9 @@ export function createStore (entities) {
3016
database.register(entity.model, entity.module || {})
3117
});
3218

19+
// VuexORM.use(installVuexORMApollo, { database: database });
20+
3321
return new Vuex.Store({
3422
plugins: [VuexORM.install(database)]
3523
});
3624
}
37-
38-
/**
39-
* Create a new Vuex State.
40-
*/
41-
export function createState (namespace, state) {
42-
return {
43-
name: namespace,
44-
45-
..._.mapValues(state, data => {
46-
return { data }
47-
})
48-
};
49-
}
50-
51-
/**
52-
* Get action context that can be passed to action as argument.
53-
*/
54-
export function actionContext (context = {}) {
55-
return {
56-
state: context.state || {},
57-
rootState: context.rootState || {},
58-
getters: context.getters || sinon.spy(),
59-
rootGetters: context.rootGetters || sinon.spy(),
60-
dispatch: context.dispatch || sinon.spy(),
61-
commit: context.commit || sinon.spy()
62-
};
63-
}
64-
65-
export default {
66-
createApplication,
67-
actionContext
68-
}

0 commit comments

Comments
 (0)