Skip to content

Commit

Permalink
Switch type check to use 'instanceof' instead of constructor.name for…
Browse files Browse the repository at this point in the history
… supporting extended types
  • Loading branch information
jhbuchanan45 committed May 10, 2021
1 parent e32b441 commit 643bae0
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/resolvers/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ConnectionResolverOpts as _ConnectionResolverOpts,
ConnectionTArgs,
} from 'graphql-compose-connection';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { CountResolverOpts, count } from './count';
import { FindManyResolverOpts, findMany } from './findMany';
import { getUniqueIndexes, extendByReversedIndexes, IndexT } from '../utils/getIndexesFromModel';
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/count.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
filterHelper,
Expand Down Expand Up @@ -29,7 +29,7 @@ export function count<TSource = any, TContext = any, TDoc extends Document = any
throw new Error('First arg for Resolver count() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('Second arg for Resolver count() should be instance of ObjectTypeComposer.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/createMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createMany<TSource = any, TContext = any, TDoc extends Document
throw new Error('First arg for Resolver createMany() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver createMany() should be instance of ObjectTypeComposer.'
);
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/createOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function createOne<TSource = any, TContext = any, TDoc extends Document =
throw new Error('First arg for Resolver createOne() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver createOne() should be instance of ObjectTypeComposer.'
);
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/dataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
projectionHelper,
Expand Down Expand Up @@ -40,7 +39,7 @@ export function dataLoader<TSource = any, TContext = any, TDoc extends Document
throw new Error('First arg for Resolver dataLoader() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver dataLoader() should be instance of ObjectTypeComposer.'
);
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/dataLoaderMany.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
projectionHelper,
Expand Down Expand Up @@ -42,7 +41,7 @@ export function dataLoaderMany<TSource = any, TContext = any, TDoc extends Docum
);
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver dataLoaderMany() should be instance of ObjectTypeComposer.'
);
Expand Down
10 changes: 6 additions & 4 deletions src/resolvers/findById.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
projectionHelper,
Expand All @@ -9,6 +8,7 @@ import {
} from './helpers';
import type { ExtendedResolveParams } from './index';
import { beforeQueryHelper, beforeQueryHelperLean } from './helpers/beforeQueryHelper';
// import { EDiscriminatorTypeComposer } from '../enhancedDiscriminators';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FindByIdResolverOpts {
Expand Down Expand Up @@ -39,15 +39,17 @@ export function findById<TSource = any, TContext = any, TDoc extends Document =
throw new Error('First arg for Resolver findById() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('Second arg for Resolver findById() should be instance of ObjectTypeComposer.');
}

const aliases = prepareNestedAliases(model.schema);
const aliasesReverse = prepareAliasesReverse(model.schema);

// const typeTC = tc instanceof EDiscriminatorTypeComposer ? tc.getDInterface() : tc;

return tc.schemaComposer.createResolver<TSource, TArgs>({
type: tc,
type: tc.getTypeName(),
name: 'findById',
kind: 'query',
args: {
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/findByIds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
limitHelper,
Expand Down Expand Up @@ -48,7 +47,7 @@ export function findByIds<TSource = any, TContext = any, TDoc extends Document =
throw new Error('First arg for Resolver findByIds() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver findByIds() should be instance of ObjectTypeComposer.'
);
Expand Down
6 changes: 3 additions & 3 deletions src/resolvers/findMany.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
limitHelper,
Expand Down Expand Up @@ -58,15 +58,15 @@ export function findMany<TSource = any, TContext = any, TDoc extends Document =
throw new Error('First arg for Resolver findMany() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('Second arg for Resolver findMany() should be instance of ObjectTypeComposer.');
}

const aliases = prepareNestedAliases(model.schema);
const aliasesReverse = prepareAliasesReverse(model.schema);

return tc.schemaComposer.createResolver<TSource, TArgs>({
type: tc.NonNull.List.NonNull,
type: tc.schemaComposer.getAnyTC(tc.getTypeName()).NonNull.List.NonNull,
name: 'findMany',
kind: 'query',
args: {
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/findOne.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
skipHelper,
Expand Down Expand Up @@ -53,7 +53,7 @@ export function findOne<TSource = any, TContext = any, TDoc extends Document = a
throw new Error('First arg for Resolver findOne() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('Second arg for Resolver findOne() should be instance of ObjectTypeComposer.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/helpers/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function recordHelperArgs<TDoc extends Document = any>(
tc: ObjectTypeComposer<TDoc, any>,
opts?: RecordHelperArgsOpts
): ObjectTypeComposerArgumentConfigMapDefinition<{ record: any }> {
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('First arg for recordHelperArgs() should be instance of ObjectTypeComposer.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/helpers/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function sortHelperArgs<TDoc extends Document = any>(
model: Model<TDoc>,
opts?: SortHelperArgsOpts
): ObjectTypeComposerArgumentConfigMapDefinition<{ sort: any }> {
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error('First arg for sortHelperArgs() should be instance of ObjectTypeComposer.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
preparePaginationResolver,
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/removeById.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import { findById } from './findById';
import type { ExtendedResolveParams } from './index';
Expand Down Expand Up @@ -28,7 +27,7 @@ export function removeById<TSource = any, TContext = any, TDoc extends Document
throw new Error('First arg for Resolver removeById() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver removeById() should be instance of ObjectTypeComposer.'
);
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/removeMany.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
filterHelperArgs,
Expand Down Expand Up @@ -35,7 +35,7 @@ export function removeMany<TSource = any, TContext = any, TDoc extends Document
throw new Error('First arg for Resolver removeMany() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver removeMany() should be instance of ObjectTypeComposer.'
);
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/removeOne.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
filterHelperArgs,
Expand Down Expand Up @@ -37,7 +37,7 @@ export function removeOne<TSource = any, TContext = any, TDoc extends Document =
throw new Error('First arg for Resolver removeOne() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver removeOne() should be instance of ObjectTypeComposer.'
);
Expand Down
5 changes: 2 additions & 3 deletions src/resolvers/updateById.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toInputType } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer, toInputType } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import { recordHelperArgs, RecordHelperArgsOpts } from './helpers/record';
import { findById } from './findById';
Expand Down Expand Up @@ -33,7 +32,7 @@ export function updateById<TSource = any, TContext = any, TDoc extends Document
throw new Error('First arg for Resolver updateById() should be instance of Mongoose Model.');
}

if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver updateById() should be instance of ObjectTypeComposer.'
);
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/updateMany.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { Resolver, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import {
limitHelper,
Expand Down Expand Up @@ -51,7 +51,7 @@ export function updateMany<TSource = any, TContext = any, TDoc extends Document
if (!model || !model.modelName || !model.schema) {
throw new Error('First arg for Resolver updateMany() should be instance of Mongoose Model.');
}
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver updateMany() should be instance of ObjectTypeComposer.'
);
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/updateOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function updateOne<TSource = any, TContext = any, TDoc extends Document =
if (!model || !model.modelName || !model.schema) {
throw new Error('First arg for Resolver updateOne() should be instance of Mongoose Model.');
}
if (!tc || tc.constructor.name !== 'ObjectTypeComposer') {
if (!tc || !(tc instanceof ObjectTypeComposer)) {
throw new Error(
'Second arg for Resolver updateOne() should be instance of ObjectTypeComposer.'
);
Expand Down

0 comments on commit 643bae0

Please sign in to comment.