Skip to content

Commit 5a3dd21

Browse files
committed
Better object detection
1 parent 3b18d1d commit 5a3dd21

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/common/logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DocumentNode } from 'graphql';
22
import { Arguments } from '../support/interfaces';
33
import { FetchPolicy } from 'apollo-client';
4-
import { prettify } from '../support/utils';
4+
import { isObject, prettify } from '../support/utils';
55

66
/**
77
* Vuex-ORM-Apollo Debug Logger.
@@ -87,8 +87,8 @@ export default class Logger {
8787
if (this.enabled) {
8888
try {
8989
let prettified = '';
90-
if (typeof query === 'object' && query.loc) {
91-
prettified = prettify(query.loc.source.body);
90+
if (isObject(query) && (query as DocumentNode).loc) {
91+
prettified = prettify((query as DocumentNode).loc!.source.body);
9292
} else {
9393
prettified = prettify(query as string);
9494
}

src/graphql/query-builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Model from '../orm/model';
22
import { Arguments, Field } from '../support/interfaces';
3-
import { upcaseFirstLetter } from '../support/utils';
3+
import { isObject, upcaseFirstLetter } from '../support/utils';
44
import gql from 'graphql-tag';
55
import Context from '../common/context';
66
const inflection = require('inflection');
@@ -105,7 +105,7 @@ export default class QueryBuilder {
105105
if (!args) throw new Error('args is undefined');
106106

107107
Object.keys(args).forEach((key: string) => {
108-
if (args && args[key] && typeof args[key] === 'object') {
108+
if (args && args[key] && isObject(args[key])) {
109109
args[key] = { __type: upcaseFirstLetter(key) };
110110
}
111111
});
@@ -172,7 +172,7 @@ export default class QueryBuilder {
172172
const type = schema.getType(model.singularName + (filter ? 'Filter' : ''));
173173
const schemaField = (filter ? type.inputFields! : type.fields!).find(f => f.name === key);
174174

175-
if (typeof value === 'object' && value.__type) {
175+
if (isObject(value) && value.__type) {
176176
// Case 2 (User!)
177177
typeOrValue = value.__type + 'Input!';
178178
} else if (schemaField && schemaField.type.name) {

src/support/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ export function downcaseFirstLetter (input: string) {
2929
export function prettify (query: string): string {
3030
return print(parse(query));
3131
}
32+
33+
/**
34+
* Tells if a value is a object (returns false for arrays)
35+
* @param {any} a
36+
* @returns {boolean}
37+
*/
38+
export function isObject (a: any): boolean {
39+
return (!!a) && (a.constructor === Object);
40+
}

src/vuex-orm-graphql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Destroy, Fetch, Mutate, Persist, Push } from './actions';
55
import Query from './actions/query';
66
import SimpleQuery from './actions/simple-query';
77
import SimpleMutation from './actions/simple-mutation';
8+
import { isObject } from './support/utils';
89

910
/**
1011
* Main class of the plugin. Setups the internal context, Vuex actions and model methods
@@ -48,7 +49,7 @@ export default class VuexORMGraphQL {
4849
// Register static model convenience methods
4950
(context.components.Model as (typeof PatchedModel)).fetch = async function (filter: any, bypassCache = false) {
5051
let filterObj = filter;
51-
if (typeof filterObj !== 'object') filterObj = { id: filter };
52+
if (!isObject(filterObj)) filterObj = { id: filter };
5253
return this.dispatch('fetch', { filter: filterObj, bypassCache });
5354
};
5455

0 commit comments

Comments
 (0)