Skip to content

Commit 54cbe4d

Browse files
committed
Fix connection detection for polymorphic relations
1 parent 56101c1 commit 54cbe4d

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

dist/vuex-orm-graphql.esm.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,6 +3661,18 @@ var Model = /** @class */ (function () {
36613661
field instanceof context.components.Number ||
36623662
field instanceof context.components.Boolean;
36633663
};
3664+
/**
3665+
* Tells if a field which represents a relation is a connection (multiple).
3666+
* @param {Field} field
3667+
* @returns {boolean}
3668+
*/
3669+
Model.isConnection = function (field) {
3670+
var context = Context.getInstance();
3671+
return !(field instanceof context.components.BelongsTo ||
3672+
field instanceof context.components.HasOne ||
3673+
field instanceof context.components.MorphTo ||
3674+
field instanceof context.components.MorphOne);
3675+
};
36643676
/**
36653677
* Adds $isPersisted and other meta fields to the model by overwriting the fields() method.
36663678
* @todo is this a good way to add fields?
@@ -3914,7 +3926,9 @@ function valueToObjectRepresentation(argObj, name, value, variables) {
39143926
argObj[name.value] = null;
39153927
}
39163928
else {
3917-
throw new Error("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\" is not supported.\n Use variables instead of inline arguments to overcome this limitation.");
3929+
throw new Error("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
3930+
'is not supported. Use variables instead of inline arguments to ' +
3931+
'overcome this limitation.');
39183932
}
39193933
}
39203934
function storeKeyNameFromField(field, variables) {
@@ -6766,7 +6780,7 @@ var QueryManager = /** @class */ (function () {
67666780
// clear out the latest new data, since we're now using it
67676781
_this.setQuery(queryId, function () { return ({ newData: null }); });
67686782
data = newData.result;
6769-
isMissing = !newData.complete ? !newData.complete : false;
6783+
isMissing = !newData.complete || false;
67706784
}
67716785
else {
67726786
if (lastResult && lastResult.data && !errorStatusChanged) {
@@ -10333,11 +10347,9 @@ var QueryBuilder = /** @class */ (function () {
1033310347
var ignore = path.includes(singularizedFieldName);
1033410348
// console.log(`-----> Will ${ignore ? '' : 'not'} ignore ${model.singularName}.${name}, path: ${path.join('.')}`);
1033510349
if (model.shouldEagerLoadRelation(name, field, relatedModel) && !ignore) {
10336-
var multiple = !(field instanceof context.components.BelongsTo ||
10337-
field instanceof context.components.HasOne);
1033810350
var newPath = path.slice(0);
1033910351
newPath.push(singularizedFieldName);
10340-
relationQueries.push(_this.buildField(relatedModel, multiple, undefined, newPath, name, false));
10352+
relationQueries.push(_this.buildField(relatedModel, Model.isConnection(field), undefined, newPath, name, false));
1034110353
}
1034210354
});
1034310355
return relationQueries.join('\n');

src/graphql/query-builder.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,10 @@ export default class QueryBuilder {
270270
// console.log(`-----> Will ${ignore ? '' : 'not'} ignore ${model.singularName}.${name}, path: ${path.join('.')}`);
271271

272272
if (model.shouldEagerLoadRelation(name, field, relatedModel) && !ignore) {
273-
const multiple: boolean = !(field instanceof context.components.BelongsTo ||
274-
field instanceof context.components.HasOne);
275-
276273
const newPath = path.slice(0);
277274
newPath.push(singularizedFieldName);
278275

279-
relationQueries.push(this.buildField(relatedModel, multiple, undefined, newPath, name, false));
276+
relationQueries.push(this.buildField(relatedModel, Model.isConnection(field), undefined, newPath, name, false));
280277
}
281278
});
282279

src/orm/model.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ export default class Model {
7676
field instanceof context.components.Boolean;
7777
}
7878

79+
/**
80+
* Tells if a field which represents a relation is a connection (multiple).
81+
* @param {Field} field
82+
* @returns {boolean}
83+
*/
84+
public static isConnection (field: Field): boolean {
85+
const context = Context.getInstance();
86+
87+
return !(
88+
field instanceof context.components.BelongsTo ||
89+
field instanceof context.components.HasOne ||
90+
field instanceof context.components.MorphTo ||
91+
field instanceof context.components.MorphOne
92+
);
93+
}
94+
7995
/**
8096
* Adds $isPersisted and other meta fields to the model by overwriting the fields() method.
8197
* @todo is this a good way to add fields?

test/integration/VuexORMGraphQL.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,13 +974,13 @@ query status {
974974
result = await store.dispatch('entities/simpleQuery', { query, variables: {} });
975975
});
976976

977-
const idSymbol = Object.getOwnPropertySymbols(result)[0];
977+
// Remove the ID Symbol
978+
delete Object.getOwnPropertySymbols(result)[0];
978979

979980
expect(result).toEqual({
980981
backend: true,
981982
smsGateway: false,
982-
paypalIntegration: true,
983-
[idSymbol]: "ROOT_QUERY"
983+
paypalIntegration: true
984984
});
985985
expect(request.query).toEqual(`
986986
query status {

0 commit comments

Comments
 (0)