Skip to content

Commit 579f2ee

Browse files
committed
Bugfix
1 parent ef778de commit 579f2ee

File tree

4 files changed

+83
-41
lines changed

4 files changed

+83
-41
lines changed

dist/vuex-orm-apollo.esm.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7811,7 +7811,7 @@ var QueryBuilder = /** @class */ (function () {
78117811
Object.keys(data).forEach(function (key) {
78127812
var value = data[key];
78137813
// Ignore IDs and connections and empty fields
7814-
if (!relations.has(key) && !model.skipField(key) && key !== 'id' && value !== null) {
7814+
if (!relations.has(key) && !key.startsWith('$') && key !== 'id' && value !== null) {
78157815
returnValue[key] = value;
78167816
}
78177817
});
@@ -7821,35 +7821,37 @@ var QueryBuilder = /** @class */ (function () {
78217821
* Transforms a set of incoming data to the format vuex-orm requires.
78227822
*
78237823
* @param {Data | Array<Data>} data
7824+
* @param model
7825+
* @param mutation required to transform something like `disableUserAddress` to the actual model name.
78247826
* @param {boolean} recursiveCall
78257827
* @returns {Data}
78267828
*/
7827-
QueryBuilder.prototype.transformIncomingData = function (data, mutationResult, recursiveCall) {
7829+
QueryBuilder.prototype.transformIncomingData = function (data, model, mutation, recursiveCall) {
78287830
var _this = this;
7829-
if (mutationResult === void 0) { mutationResult = false; }
7831+
if (mutation === void 0) { mutation = false; }
78307832
if (recursiveCall === void 0) { recursiveCall = false; }
78317833
var result = {};
78327834
if (!recursiveCall) {
78337835
this.context.logger.group('Transforming incoming data');
78347836
this.context.logger.log('Raw data:', data);
78357837
}
78367838
if (data instanceof Array) {
7837-
result = data.map(function (d) { return _this.transformIncomingData(d, mutationResult, true); });
7839+
result = data.map(function (d) { return _this.transformIncomingData(d, model, mutation, true); });
78387840
}
78397841
else {
78407842
Object.keys(data).forEach(function (key) {
78417843
if (data[key]) {
78427844
if (data[key] instanceof Object) {
78437845
if (data[key].nodes) {
7844-
result[inflection.pluralize(key)] = _this.transformIncomingData(data[key].nodes, mutationResult, true);
7846+
result[inflection.pluralize(key)] = _this.transformIncomingData(data[key].nodes, model, mutation, true);
78457847
}
78467848
else {
78477849
var newKey = key;
7848-
if (mutationResult) {
7849-
newKey = newKey.replace(/^(create|update)(.+)/, '$2');
7850+
if (mutation && !recursiveCall) {
7851+
newKey = data[key].nodes ? model.pluralName : model.singularName;
78507852
newKey = downcaseFirstLetter(newKey);
78517853
}
7852-
result[inflection.singularize(newKey)] = _this.transformIncomingData(data[key], mutationResult, true);
7854+
result[newKey] = _this.transformIncomingData(data[key], model, mutation, true);
78537855
}
78547856
}
78557857
else if (key === 'id') {
@@ -8281,7 +8283,7 @@ var VuexORMApollo = /** @class */ (function () {
82818283
model = this.context.getModel(state.$name);
82828284
name = "" + (multiple ? model.pluralName : model.singularName);
82838285
query = this.queryBuilder.buildQuery('query', model, name, filter);
8284-
return [4 /*yield*/, this.apolloRequest(query, filter, false, bypassCache)];
8286+
return [4 /*yield*/, this.apolloRequest(model, query, filter, false, bypassCache)];
82858287
case 1:
82868288
data = _b.sent();
82878289
// Insert incoming data into the store
@@ -8432,7 +8434,7 @@ var VuexORMApollo = /** @class */ (function () {
84328434
if (!variables) return [3 /*break*/, 2];
84338435
id = variables.id ? variables.id : undefined;
84348436
query = this.queryBuilder.buildQuery('mutation', model, name, variables, multiple);
8435-
return [4 /*yield*/, this.apolloRequest(query, variables, true)];
8437+
return [4 /*yield*/, this.apolloRequest(model, query, variables, true)];
84368438
case 1:
84378439
newData = _a.sent();
84388440
if (id)
@@ -8445,13 +8447,14 @@ var VuexORMApollo = /** @class */ (function () {
84458447
};
84468448
/**
84478449
* Sends a request to the GraphQL API via apollo
8450+
* @param model
84488451
* @param {any} query The query to send (result from gql())
84498452
* @param {Arguments} variables Optional. The variables to send with the query
84508453
* @param {boolean} mutation Optional. If this is a mutation (true) or a query (false, default)
84518454
* @param {boolean} bypassCache If true the query will be send to the server without using the cache. For queries only
84528455
* @returns {Promise<Data>}
84538456
*/
8454-
VuexORMApollo.prototype.apolloRequest = function (query, variables, mutation, bypassCache) {
8457+
VuexORMApollo.prototype.apolloRequest = function (model, query, variables, mutation, bypassCache) {
84558458
if (mutation === void 0) { mutation = false; }
84568459
if (bypassCache === void 0) { bypassCache = false; }
84578460
return __awaiter(this, void 0, void 0, function () {
@@ -8472,7 +8475,7 @@ var VuexORMApollo = /** @class */ (function () {
84728475
_a.label = 4;
84738476
case 4:
84748477
// Transform incoming data into something useful
8475-
return [2 /*return*/, this.queryBuilder.transformIncomingData(response.data, mutation)];
8478+
return [2 /*return*/, this.queryBuilder.transformIncomingData(response.data, model, mutation)];
84768479
}
84778480
});
84788481
});

src/queryBuilder.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ export default class QueryBuilder {
152152
* Transforms a set of incoming data to the format vuex-orm requires.
153153
*
154154
* @param {Data | Array<Data>} data
155+
* @param model
156+
* @param mutation required to transform something like `disableUserAddress` to the actual model name.
155157
* @param {boolean} recursiveCall
156158
* @returns {Data}
157159
*/
158-
public transformIncomingData (data: Data | Array<Data>, mutationResult: boolean = false, recursiveCall: boolean = false): Data {
160+
public transformIncomingData (data: Data | Array<Data>, model: Model, mutation: boolean = false, recursiveCall: boolean = false): Data {
159161
let result: Data = {};
160162

161163
if (!recursiveCall) {
@@ -164,22 +166,22 @@ export default class QueryBuilder {
164166
}
165167

166168
if (data instanceof Array) {
167-
result = data.map(d => this.transformIncomingData(d, mutationResult, true));
169+
result = data.map(d => this.transformIncomingData(d, model, mutation, true));
168170
} else {
169171
Object.keys(data).forEach((key) => {
170172
if (data[key]) {
171173
if (data[key] instanceof Object) {
172174
if (data[key].nodes) {
173-
result[inflection.pluralize(key)] = this.transformIncomingData(data[key].nodes, mutationResult, true);
175+
result[inflection.pluralize(key)] = this.transformIncomingData(data[key].nodes, model, mutation, true);
174176
} else {
175177
let newKey = key;
176178

177-
if (mutationResult) {
178-
newKey = newKey.replace(/^(create|update)(.+)/, '$2');
179+
if (mutation && !recursiveCall) {
180+
newKey = data[key].nodes ? model.pluralName : model.singularName;
179181
newKey = downcaseFirstLetter(newKey);
180182
}
181183

182-
result[inflection.singularize(newKey)] = this.transformIncomingData(data[key], mutationResult, true);
184+
result[newKey] = this.transformIncomingData(data[key], model, mutation, true);
183185
}
184186
} else if (key === 'id') {
185187
result[key] = parseInt(data[key], 0);

src/vuex-orm-apollo.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export default class VuexORMApollo {
9393
const query = this.queryBuilder.buildQuery('query', model, name, filter);
9494

9595
// Send the request to the GraphQL API
96-
const data = await this.apolloRequest(query, filter, false, bypassCache as boolean);
96+
const data = await this.apolloRequest(model, query, filter, false, bypassCache as boolean);
9797

9898
// Insert incoming data into the store
9999
await this.insertData(data, dispatch);
@@ -211,7 +211,7 @@ export default class VuexORMApollo {
211211
const query = this.queryBuilder.buildQuery('mutation', model, name, variables, multiple);
212212

213213
// Send GraphQL Mutation
214-
const newData = await this.apolloRequest(query, variables, true);
214+
const newData = await this.apolloRequest(model, query, variables, true);
215215

216216
if (id) return VuexORMApollo.updateData(newData, dispatch, id);
217217
return null;
@@ -220,13 +220,14 @@ export default class VuexORMApollo {
220220

221221
/**
222222
* Sends a request to the GraphQL API via apollo
223+
* @param model
223224
* @param {any} query The query to send (result from gql())
224225
* @param {Arguments} variables Optional. The variables to send with the query
225226
* @param {boolean} mutation Optional. If this is a mutation (true) or a query (false, default)
226227
* @param {boolean} bypassCache If true the query will be send to the server without using the cache. For queries only
227228
* @returns {Promise<Data>}
228229
*/
229-
private async apolloRequest (query: any, variables?: Arguments, mutation: boolean = false,
230+
private async apolloRequest (model: Model, query: any, variables?: Arguments, mutation: boolean = false,
230231
bypassCache: boolean = false): Promise<Data> {
231232
let response;
232233
const fetchPolicy: FetchPolicy = bypassCache ? 'network-only' : 'cache-first';
@@ -240,7 +241,7 @@ export default class VuexORMApollo {
240241
}
241242

242243
// Transform incoming data into something useful
243-
return this.queryBuilder.transformIncomingData(response.data as Data, mutation);
244+
return this.queryBuilder.transformIncomingData(response.data as Data, model, mutation);
244245
}
245246

246247
/**

test/unit/QueryBuilder.spec.js

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,58 @@ class Comment extends ORMModel {
5353
}
5454
}
5555

56+
class ContractContractOption extends ORMModel {
57+
static entity = 'contractContractOptions';
58+
59+
static primaryKey = ['contractId', 'contractOptionId'];
60+
61+
static fields () {
62+
return {
63+
contractId: this.attr(null),
64+
contractOptionId: this.attr(null),
65+
}
66+
}
67+
}
68+
69+
class Contract extends ORMModel {
70+
static entity = 'contracts';
71+
72+
static fields () {
73+
return {
74+
id: this.increment(),
75+
name: this.attr(''),
76+
displayName: this.attr(''),
77+
slug: this.attr(''),
78+
79+
contractOptions: this.belongsToMany(ContractOption, ContractContractOption, 'contractId',
80+
'contractOptionId'),
81+
}
82+
}
83+
}
84+
85+
86+
class ContractOption extends ORMModel {
87+
static entity = 'contractOptions';
88+
89+
static fields () {
90+
return {
91+
id: this.increment(),
92+
name: this.attr(''),
93+
description: this.attr(''),
94+
95+
contracts: this.belongsToMany(Contract, ContractContractOption, 'contractOptionId', 'contractId')
96+
}
97+
}
98+
}
99+
100+
101+
56102
beforeEach(() => {
57-
[store, vuexOrmApollo] = createStore([{ model: User }, { model: Post }, { model: Comment }]);
103+
[store, vuexOrmApollo] = createStore([
104+
{ model: User }, { model: Post }, { model: Comment }, { model: ContractOption }, { model: Contract },
105+
{ model: ContractContractOption }
106+
]);
107+
58108
store.dispatch('entities/users/insert', { data: { id: 1, name: 'Charlie Brown' }});
59109
store.dispatch('entities/users/insert', { data: { id: 2, name: 'Peppermint Patty' }});
60110
store.dispatch('entities/posts/insert', { data: { id: 1, userId: 1, title: 'Example post 1', content: 'Foo' }});
@@ -130,9 +180,6 @@ describe('QueryBuilder', () => {
130180
"description": "Very foo, much more bar"
131181
}
132182
]
133-
},
134-
"documentReferences": {
135-
"nodes": []
136183
}
137184
},
138185
{
@@ -148,9 +195,6 @@ describe('QueryBuilder', () => {
148195
"description": "Very foo, much more bar"
149196
}
150197
]
151-
},
152-
"documentReferences": {
153-
"nodes": []
154198
}
155199
},
156200
{
@@ -166,9 +210,6 @@ describe('QueryBuilder', () => {
166210
"description": "Very foo, much more bar"
167211
}
168212
]
169-
},
170-
"documentReferences": {
171-
"nodes": []
172213
}
173214
}
174215
]
@@ -185,7 +226,6 @@ describe('QueryBuilder', () => {
185226
},
186227
],
187228
"displayName": "Contract S",
188-
"documentReferences": [],
189229
"id": 1,
190230
"name": "Contract S",
191231
"slug": "contract-s",
@@ -199,7 +239,6 @@ describe('QueryBuilder', () => {
199239
},
200240
],
201241
"displayName": "Contract M",
202-
"documentReferences": [],
203242
"id": 2,
204243
"name": "Contract M",
205244
"slug": "contract-m",
@@ -213,15 +252,15 @@ describe('QueryBuilder', () => {
213252
},
214253
],
215254
"displayName": "Contract L",
216-
"documentReferences": [],
217255
"id": 3,
218256
"name": "Contract L",
219257
"slug": "contract-l",
220258
},
221259
],
222260
};
223261

224-
expect(queryBuilder.transformIncomingData(incomingData)).toEqual(expectedData);
262+
const model = vuexOrmApollo.context.getModel('contract');
263+
expect(queryBuilder.transformIncomingData(incomingData, model, false)).toEqual(expectedData);
225264
});
226265
});
227266

@@ -242,9 +281,6 @@ describe('QueryBuilder', () => {
242281
"description": "Very foo, much more bar"
243282
}
244283
]
245-
},
246-
"documentReferences": {
247-
"nodes": []
248284
}
249285
}
250286
};
@@ -258,14 +294,14 @@ describe('QueryBuilder', () => {
258294
},
259295
],
260296
"displayName": "Contract S",
261-
"documentReferences": [],
262297
"id": 1,
263298
"name": "Contract S",
264299
"slug": "contract-s",
265300
}
266301
};
267302

268-
expect(queryBuilder.transformIncomingData(incomingData, true)).toEqual(expectedData);
303+
const model = vuexOrmApollo.context.getModel('contract');
304+
expect(queryBuilder.transformIncomingData(incomingData, model, true)).toEqual(expectedData);
269305
});
270306
});
271307

0 commit comments

Comments
 (0)