Skip to content

Commit

Permalink
feat(MongoID): allow to override build-in MongoID type
Browse files Browse the repository at this point in the history
```js
schemaComposer.set('MongoID', YOUR_TYPE_IMPLEMENTATION);
const UserTC = composeWithMongoose(UserModel, { schemaComposer });
```

Related #98
  • Loading branch information
nodkz committed Apr 20, 2018
1 parent 44a9a5c commit 720f0ba
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 38 deletions.
28 changes: 26 additions & 2 deletions src/__tests__/fieldConverter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,38 @@ describe('fieldConverter', () => {
};
expect(convertFieldToGraphQL(mongooseField, '', schemaComposer)).toBe('String');
});

it('should add GraphQLMongoID to schemaComposer', () => {
schemaComposer.clear();
expect(schemaComposer.has('MongoID')).toBeFalsy();
const mongooseField = {
path: 'strFieldName',
instance: 'ObjectID',
};
expect(convertFieldToGraphQL(mongooseField, '', schemaComposer)).toBe('MongoID');
expect(schemaComposer.get('MongoID')).toBe(GraphQLMongoID);
});

it('should use existed GraphQLMongoID in schemaComposer', () => {
schemaComposer.clear();
expect(schemaComposer.has('MongoID')).toBeFalsy();
schemaComposer.set('MongoID', ('MockGraphQLType': any));
const mongooseField = {
path: 'strFieldName',
instance: 'ObjectID',
};
expect(convertFieldToGraphQL(mongooseField, '', schemaComposer)).toBe('MongoID');
expect(schemaComposer.get('MongoID')).toBe('MockGraphQLType');
schemaComposer.delete('MongoID');
});
});

describe('scalarToGraphQL()', () => {
it('should properly convert mongoose scalar type to default graphQL types', () => {
expect(scalarToGraphQL({ instance: 'String' })).toBe('String');
expect(scalarToGraphQL({ instance: 'Number' })).toBe('Float');
expect(scalarToGraphQL({ instance: 'Boolean' })).toBe('Boolean');
expect(scalarToGraphQL({ instance: 'ObjectID' })).toBe(GraphQLMongoID);
expect(scalarToGraphQL({ instance: 'ObjectID' })).toBe('MongoID');
});

it('should properly convert mongoose scalar type to scalar graphql-compose types', () => {
Expand Down Expand Up @@ -196,7 +220,7 @@ describe('fieldConverter', () => {

describe('referenceToGraphQL()', () => {
it('should return type of field', () => {
expect(referenceToGraphQL(fields.user)).toBe(GraphQLMongoID);
expect(referenceToGraphQL(fields.user)).toBe('MongoID');
});
});
});
2 changes: 2 additions & 0 deletions src/composeWithMongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
SortHelperArgsOpts,
RecordHelperArgsOpts,
} from './resolvers/helpers';
import MongoID from './types/mongoid';

export type TypeConverterOpts = {
schemaComposer?: SchemaComposer<any>,
Expand Down Expand Up @@ -120,6 +121,7 @@ export function composeWithMongoose(
const name: string = (opts && opts.name) || model.modelName;

const sc = opts.schemaComposer || schemaComposer;
sc.set('MongoID', MongoID);
const tc = convertModelToGraphQL(model, name, sc);

if (opts.description) {
Expand Down
6 changes: 5 additions & 1 deletion src/fieldsConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ export function convertFieldToGraphQL(
prefix?: string = '',
schemaComposer: SchemaComposer<any>
): ComposeOutputType {
if (!schemaComposer.has('MongoID')) {
schemaComposer.set('MongoID', GraphQLMongoID);
}

const complexType = deriveComplexType(field);
switch (complexType) {
case ComplexTypes.SCALAR:
Expand Down Expand Up @@ -257,7 +261,7 @@ export function scalarToGraphQL(field: MongooseFieldT): ComposeScalarType {
case 'Boolean':
return 'Boolean';
case 'ObjectID':
return GraphQLMongoID;
return 'MongoID';
default:
return 'JSON';
}
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/createOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('createOne() ->', () => {
describe('Resolver.args', () => {
it('should have required `record` arg', () => {
const resolver = createOne(UserModel, UserTC);
const argConfig: any = resolver.getArg('record');
const argConfig: any = resolver.getArgConfig('record');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType.name).toBe('CreateOneUserInput');
});
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/findById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('findById() ->', () => {
it('should have non-null `_id` arg', () => {
const resolver = findById(UserModel, UserTC);
expect(resolver.hasArg('_id')).toBe(true);
const argConfig: any = resolver.getArg('_id');
const argConfig: any = resolver.getArgConfig('_id');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType).toBe(GraphQLMongoID);
});
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/findByIds-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('findByIds() ->', () => {
it('should have non-null `_ids` arg', () => {
const resolver = findByIds(UserModel, UserTC);
expect(resolver.hasArg('_ids')).toBe(true);
const argConfig: any = resolver.getArg('_ids');
const argConfig: any = resolver.getArgConfig('_ids');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType).toBeInstanceOf(GraphQLList);
expect(argConfig.type.ofType.ofType).toBe(GraphQLMongoID);
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/removeById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('removeById() ->', () => {
it('should have non-null `_id` arg', () => {
const resolver = removeById(UserModel, UserTC);
expect(resolver.hasArg('_id')).toBe(true);
const argConfig: any = resolver.getArg('_id');
const argConfig: any = resolver.getArgConfig('_id');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType).toBe(GraphQLMongoID);
});
Expand Down
4 changes: 2 additions & 2 deletions src/resolvers/__tests__/updateById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ describe('updateById() ->', () => {
describe('Resolver.args', () => {
it('should have `record` arg', () => {
const resolver = updateById(UserModel, UserTC);
const argConfig: any = resolver.getArg('record');
const argConfig: any = resolver.getArgConfig('record');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType.name).toBe('UpdateByIdUserInput');
});

it('should have `record._id` required arg', () => {
const resolver = updateById(UserModel, UserTC);
const argConfig: any = resolver.getArg('record') || {};
const argConfig: any = resolver.getArgConfig('record') || {};
expect(argConfig.type.ofType).toBeInstanceOf(GraphQLInputObjectType);
if (argConfig.type && argConfig.type.ofType) {
const _idFieldType = new InputTypeComposer(argConfig.type.ofType).getFieldType('_id');
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/updateMany-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('updateMany() ->', () => {

it('should have `record` arg', () => {
const resolver = updateMany(UserModel, UserTC);
const argConfig: any = resolver.getArg('record');
const argConfig: any = resolver.getArgConfig('record');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType.name).toBe('UpdateManyUserInput');
});
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/__tests__/updateOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('updateOne() ->', () => {

it('should have required `record` arg', () => {
const resolver = updateOne(UserModel, UserTC);
const argConfig: any = resolver.getArg('record');
const argConfig: any = resolver.getArgConfig('record');
expect(argConfig.type).toBeInstanceOf(GraphQLNonNull);
expect(argConfig.type.ofType.name).toBe('UpdateOneUserInput');
});
Expand Down
3 changes: 1 addition & 2 deletions src/resolvers/createOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import { recordHelperArgs } from './helpers';
import GraphQLMongoID from '../types/mongoid';
import type { ExtendedResolveParams, GenResolverOpts } from './index';

export default function createOne(
Expand All @@ -24,7 +23,7 @@ export default function createOne(
const outputType = tc.constructor.schemaComposer.getOrCreateTC(outputTypeName, t => {
t.addFields({
recordId: {
type: GraphQLMongoID,
type: 'MongoID',
description: 'Created document ID',
},
record: {
Expand Down
7 changes: 1 addition & 6 deletions src/resolvers/findById.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import { GraphQLNonNull } from 'graphql-compose/lib/graphql';
import GraphQLMongoID from '../types/mongoid';
import { projectionHelper } from './helpers';
import type { ExtendedResolveParams, GenResolverOpts } from './index';

Expand All @@ -25,10 +23,7 @@ export default function findById(
name: 'findById',
kind: 'query',
args: {
_id: {
name: '_id',
type: new GraphQLNonNull(GraphQLMongoID),
},
_id: 'MongoID!',
},
resolve: (resolveParams: ExtendedResolveParams) => {
const args = resolveParams.args || {};
Expand Down
7 changes: 1 addition & 6 deletions src/resolvers/findByIds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import { GraphQLNonNull, GraphQLList } from 'graphql-compose/lib/graphql';
import GraphQLMongoID from '../types/mongoid';
import {
limitHelper,
limitHelperArgs,
Expand Down Expand Up @@ -31,10 +29,7 @@ export default function findByIds(
name: 'findByIds',
kind: 'query',
args: {
_ids: {
name: '_ids',
type: new GraphQLNonNull(new GraphQLList(GraphQLMongoID)),
},
_ids: '[MongoID]!',
...limitHelperArgs({
...(opts && opts.limit),
}),
Expand Down
9 changes: 2 additions & 7 deletions src/resolvers/removeById.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import { GraphQLNonNull } from 'graphql-compose/lib/graphql';
import findById from './findById';
import GraphQLMongoID from '../types/mongoid';
import type { ExtendedResolveParams, GenResolverOpts } from './index';

export default function removeById(
Expand All @@ -27,7 +25,7 @@ export default function removeById(
const outputType = tc.constructor.schemaComposer.getOrCreateTC(outputTypeName, t => {
t.addFields({
recordId: {
type: GraphQLMongoID,
type: 'MongoID',
description: 'Removed document ID',
},
record: {
Expand All @@ -46,10 +44,7 @@ export default function removeById(
'2) Return removed document.',
type: outputType,
args: {
_id: {
name: '_id',
type: new GraphQLNonNull(GraphQLMongoID),
},
_id: 'MongoID!',
},
resolve: async (resolveParams: ExtendedResolveParams) => {
const args = resolveParams.args || {};
Expand Down
3 changes: 1 addition & 2 deletions src/resolvers/removeOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import GraphQLMongoID from '../types/mongoid';
import { filterHelperArgs, sortHelperArgs } from './helpers';
import findOne from './findOne';
import type { ExtendedResolveParams, GenResolverOpts } from './index';
Expand All @@ -27,7 +26,7 @@ export default function removeOne(
const outputType = tc.constructor.schemaComposer.getOrCreateTC(outputTypeName, t => {
t.addFields({
recordId: {
type: GraphQLMongoID,
type: 'MongoID',
description: 'Removed document ID',
},
record: {
Expand Down
3 changes: 1 addition & 2 deletions src/resolvers/updateById.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Resolver, TypeComposer } from 'graphql-compose';
import type { MongooseModel } from 'mongoose';
import { recordHelperArgs } from './helpers/record';
import findById from './findById';
import GraphQLMongoID from '../types/mongoid';

import type { ExtendedResolveParams, GenResolverOpts } from './index';

Expand All @@ -28,7 +27,7 @@ export default function updateById(
const outputType = tc.constructor.schemaComposer.getOrCreateTC(outputTypeName, t => {
t.addFields({
recordId: {
type: GraphQLMongoID,
type: 'MongoID',
description: 'Updated document ID',
},
record: {
Expand Down
3 changes: 1 addition & 2 deletions src/resolvers/updateOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { MongooseModel } from 'mongoose';
import type { ExtendedResolveParams, GenResolverOpts } from './index';
import { skipHelperArgs, recordHelperArgs, filterHelperArgs, sortHelperArgs } from './helpers';
import findOne from './findOne';
import GraphQLMongoID from '../types/mongoid';

export default function updateOne(
model: MongooseModel,
Expand All @@ -26,7 +25,7 @@ export default function updateOne(
const outputType = tc.constructor.schemaComposer.getOrCreateTC(outputTypeName, t => {
t.addFields({
recordId: {
type: GraphQLMongoID,
type: 'MongoID',
description: 'Updated document ID',
},
record: {
Expand Down

0 comments on commit 720f0ba

Please sign in to comment.