diff --git a/README.md b/README.md index 7cfaec81..03307cc9 100644 --- a/README.md +++ b/README.md @@ -145,16 +145,16 @@ export type typeConverterResolversOpts = { skip?: false, }, updateById?: false | { - input?: inputHelperArgsOpts | false, + record?: recordHelperArgsOpts | false, }, updateOne?: false | { - input?: inputHelperArgsOpts | false, + record?: recordHelperArgsOpts | false, filter?: filterHelperArgsOpts | false, sort?: sortHelperArgsOpts | false, skip?: false, }, updateMany?: false | { - input?: inputHelperArgsOpts | false, + record?: recordHelperArgsOpts | false, filter?: filterHelperArgsOpts | false, sort?: sortHelperArgsOpts | false, limit?: limitHelperArgsOpts | false, @@ -169,7 +169,7 @@ export type typeConverterResolversOpts = { filter?: filterHelperArgsOpts | false, }, createOne?: false | { - input?: inputHelperArgsOpts | false, + record?: recordHelperArgsOpts | false, }, count?: false | { filter?: filterHelperArgsOpts | false, @@ -177,7 +177,7 @@ export type typeConverterResolversOpts = { }; ``` -This is `opts.resolvers.[resolverName].[filter|sort|input|limit]` level of options. +This is `opts.resolvers.[resolverName].[filter|sort|record|limit]` level of options. You may tune every resolver's args independently as you wish. Here you may setup every argument and override some fields from the default input object type, described above in `opts.inputType`. ```js @@ -192,9 +192,9 @@ export type sortHelperArgsOpts = { sortTypeName?: string, // type name for `sort` }; -export type inputHelperArgsOpts = { - inputTypeName?: string, // type name for `input` - isRequired?: boolean, // set `input` arg as required (wraps in GraphQLNonNull) +export type recordHelperArgsOpts = { + recordTypeName?: string, // type name for `record` + isRequired?: boolean, // set `record` arg as required (wraps in GraphQLNonNull) removeFields?: string[], // provide fieldNames, that should be removed requiredFields?: string[], // provide fieldNames, that should be required }; diff --git a/resources/watch.js b/resources/watch.js index cb411382..c111df4c 100644 --- a/resources/watch.js +++ b/resources/watch.js @@ -15,7 +15,6 @@ function exec(command, options) { cmd: cmd, env: { ...process.env, - BABEL_ENV: 'cjs', }, stdio: 'inherit' }); @@ -122,6 +121,7 @@ function runTests(filepaths) { // '--reporter', 'progress', '--reporter', 'dot', '--require', './resources/mocha-bootload', + '--require', 'babel-polyfill', ].concat( allTests(filepaths) ? filepaths.map(srcPath) : diff --git a/src/definition.js b/src/definition.js index 376dd388..8a52b3ba 100644 --- a/src/definition.js +++ b/src/definition.js @@ -99,7 +99,7 @@ export type ExtendedResolveParams = ResolveParams & { export type genResolverOpts = { filter?: filterHelperArgsOpts, sort?: sortHelperArgsOpts, - input?: inputHelperArgsOpts, + record?: recordHelperArgsOpts, limit?: limitHelperArgsOpts, } @@ -144,16 +144,16 @@ export type typeConverterResolversOpts = { skip?: false, }, updateById?: false | { - input?: inputHelperArgsOpts | false, + input?: recordHelperArgsOpts | false, }, updateOne?: false | { - input?: inputHelperArgsOpts | false, + input?: recordHelperArgsOpts | false, filter?: filterHelperArgsOpts | false, sort?: sortHelperArgsOpts | false, skip?: false, }, updateMany?: false | { - input?: inputHelperArgsOpts | false, + input?: recordHelperArgsOpts | false, filter?: filterHelperArgsOpts | false, sort?: sortHelperArgsOpts | false, limit?: limitHelperArgsOpts | false, @@ -168,7 +168,7 @@ export type typeConverterResolversOpts = { filter?: filterHelperArgsOpts | false, }, createOne?: false | { - input?: inputHelperArgsOpts | false, + input?: recordHelperArgsOpts | false, }, count?: false | { filter?: filterHelperArgsOpts | false, @@ -187,8 +187,8 @@ export type sortHelperArgsOpts = { sortTypeName?: string, }; -export type inputHelperArgsOpts = { - inputTypeName?: string, +export type recordHelperArgsOpts = { + recordTypeName?: string, isRequired?: boolean, removeFields?: string[], requiredFields?: string[], diff --git a/src/resolvers/__tests__/createOne-test.js b/src/resolvers/__tests__/createOne-test.js index ebcd76ae..8d463015 100644 --- a/src/resolvers/__tests__/createOne-test.js +++ b/src/resolvers/__tests__/createOne-test.js @@ -23,9 +23,9 @@ describe('createOne() ->', () => { }); describe('Resolver.args', () => { - it('should have required `input` arg', () => { + it('should have required `record` arg', () => { const resolver = createOne(UserModel, UserTypeComposer); - const argConfig = resolver.getArg('input'); + const argConfig = resolver.getArg('record'); expect(argConfig).property('type').instanceof(GraphQLNonNull); expect(argConfig).deep.property('type.ofType.name', 'CreateOneUserInput'); }); @@ -38,24 +38,24 @@ describe('createOne() ->', () => { result.catch(() => 'catch error if appear, hide it from mocha'); }); - it('should rejected with Error if args.input is empty', async () => { + it('should rejected with Error if args.record is empty', async () => { const result = createOne(UserModel, UserTypeComposer).resolve({ args: {} }); - await expect(result).be.rejectedWith(Error, 'at least one value in args.input'); + await expect(result).be.rejectedWith(Error, 'at least one value in args.record'); }); it('should return payload.recordId', async () => { const result = await createOne(UserModel, UserTypeComposer).resolve({ args: { - input: { name: 'newName' }, + record: { name: 'newName' }, }, }); expect(result).property('recordId').to.be.ok; }); - it('should create document with args.input', async () => { + it('should create document with args.record', async () => { const result = await createOne(UserModel, UserTypeComposer).resolve({ args: { - input: { name: 'newName' }, + record: { name: 'newName' }, }, }); expect(result).have.deep.property('record.name', 'newName'); @@ -65,7 +65,7 @@ describe('createOne() ->', () => { const checkedName = 'nameForMongoDB'; createOne(UserModel, UserTypeComposer).resolve({ args: { - input: { name: checkedName }, + record: { name: checkedName }, }, }).then((res) => { UserModel.collection.findOne({ _id: res.record._id }, (err, doc) => { @@ -78,7 +78,7 @@ describe('createOne() ->', () => { it('should return payload.record', async () => { const result = await createOne(UserModel, UserTypeComposer).resolve({ args: { - input: { name: 'NewUser' }, + record: { name: 'NewUser' }, }, }); expect(result).have.deep.property('record.id', result.recordId); diff --git a/src/resolvers/__tests__/updateById-test.js b/src/resolvers/__tests__/updateById-test.js index 0dc116b7..6bf6793a 100644 --- a/src/resolvers/__tests__/updateById-test.js +++ b/src/resolvers/__tests__/updateById-test.js @@ -51,16 +51,16 @@ describe('updateById() ->', () => { }); describe('Resolver.args', () => { - it('should have `input` arg', () => { + it('should have `record` arg', () => { const resolver = updateById(UserModel, UserTypeComposer); - const argConfig = resolver.getArg('input'); + const argConfig = resolver.getArg('record'); expect(argConfig).property('type').instanceof(GraphQLNonNull); expect(argConfig).deep.property('type.ofType.name', 'UpdateByIdUserInput'); }); - it('should have `input._id` required arg', () => { + it('should have `record._id` required arg', () => { const resolver = updateById(UserModel, UserTypeComposer); - const argConfig = resolver.getArg('input') || {}; + const argConfig = resolver.getArg('record') || {}; expect(argConfig).deep.property('type.ofType').instanceof(GraphQLInputObjectType); if (argConfig.type && argConfig.type.ofType) { const _idFieldType = new InputTypeComposer(argConfig.type.ofType).getFieldType('_id'); @@ -77,34 +77,34 @@ describe('updateById() ->', () => { result.catch(() => 'catch error if appear, hide it from mocha'); }); - it('should rejected with Error if args.input._id is empty', async () => { - const result = updateById(UserModel, UserTypeComposer).resolve({ args: { input: {} } }); - await expect(result).be.rejectedWith(Error, 'requires args.input._id'); + it('should rejected with Error if args.record._id is empty', async () => { + const result = updateById(UserModel, UserTypeComposer).resolve({ args: { record: {} } }); + await expect(result).be.rejectedWith(Error, 'requires args.record._id'); }); it('should return payload.recordId', async () => { const result = await updateById(UserModel, UserTypeComposer).resolve({ args: { - input: { _id: user1.id, name: 'some name' }, + record: { _id: user1.id, name: 'some name' }, }, }); expect(result).have.property('recordId', user1.id); }); - it('should change data via args.input in model', async () => { + it('should change data via args.record in model', async () => { const result = await updateById(UserModel, UserTypeComposer).resolve({ args: { - input: { _id: user1.id, name: 'newName' }, + record: { _id: user1.id, name: 'newName' }, }, }); expect(result).have.deep.property('record.name', 'newName'); }); - it('should change data via args.input in database', (done) => { + it('should change data via args.record in database', (done) => { const checkedName = 'nameForMongoDB'; updateById(UserModel, UserTypeComposer).resolve({ args: { - input: { _id: user1.id, name: checkedName }, + record: { _id: user1.id, name: checkedName }, }, }).then(() => { UserModel.collection.findOne({ _id: user1._id }, (err, doc) => { @@ -118,7 +118,7 @@ describe('updateById() ->', () => { const checkedName = 'anyName123'; const result = await updateById(UserModel, UserTypeComposer).resolve({ args: { - input: { _id: user1.id, name: checkedName }, + record: { _id: user1.id, name: checkedName }, }, }); expect(result).have.deep.property('record.id', user1.id); diff --git a/src/resolvers/__tests__/updateMany-test.js b/src/resolvers/__tests__/updateMany-test.js index 84ad6cca..f215885d 100644 --- a/src/resolvers/__tests__/updateMany-test.js +++ b/src/resolvers/__tests__/updateMany-test.js @@ -67,9 +67,9 @@ describe('updateMany() ->', () => { expect(resolver.hasArg('sort')).to.be.true; }); - it('should have `input` arg', () => { + it('should have `record` arg', () => { const resolver = updateMany(UserModel, UserTypeComposer); - const argConfig = resolver.getArg('input'); + const argConfig = resolver.getArg('record'); expect(argConfig).property('type').instanceof(GraphQLNonNull); expect(argConfig).deep.property('type.ofType.name', 'UpdateManyUserInput'); }); @@ -82,17 +82,17 @@ describe('updateMany() ->', () => { result.catch(() => 'catch error if appear, hide it from mocha'); }); - it('should rejected with Error if args.input is empty', async () => { + it('should rejected with Error if args.record is empty', async () => { const result = updateMany(UserModel, UserTypeComposer).resolve({ args: {} }); - await expect(result).be.rejectedWith(Error, 'at least one value in args.input'); + await expect(result).be.rejectedWith(Error, 'at least one value in args.record'); }); - it('should change data via args.input in database', (done) => { + it('should change data via args.record in database', (done) => { const checkedName = 'nameForMongoDB'; updateMany(UserModel, UserTypeComposer).resolve({ args: { filter: { _id: user1.id }, - input: { name: checkedName }, + record: { name: checkedName }, }, }).then(() => { UserModel.collection.findOne({ _id: user1._id }, (err, doc) => { @@ -105,7 +105,7 @@ describe('updateMany() ->', () => { it('should return payload.numAffected', async () => { const result = await updateMany(UserModel, UserTypeComposer).resolve({ args: { - input: { gender: 'female' }, + record: { gender: 'female' }, }, }); expect(result).have.deep.property('numAffected', 2); diff --git a/src/resolvers/__tests__/updateOne-test.js b/src/resolvers/__tests__/updateOne-test.js index 59434fd2..06a8c20a 100644 --- a/src/resolvers/__tests__/updateOne-test.js +++ b/src/resolvers/__tests__/updateOne-test.js @@ -62,9 +62,9 @@ describe('updateOne() ->', () => { expect(resolver.hasArg('sort')).to.be.true; }); - it('should have required `input` arg', () => { + it('should have required `record` arg', () => { const resolver = updateOne(UserModel, UserTypeComposer); - const argConfig = resolver.getArg('input'); + const argConfig = resolver.getArg('record'); expect(argConfig).property('type').instanceof(GraphQLNonNull); expect(argConfig).deep.property('type.ofType.name', 'UpdateOneUserInput'); }); @@ -89,22 +89,22 @@ describe('updateOne() ->', () => { expect(result).have.property('recordId', user1.id); }); - it('should change data via args.input in model', async () => { + it('should change data via args.record in model', async () => { const result = await updateOne(UserModel, UserTypeComposer).resolve({ args: { filter: { _id: user1.id }, - input: { name: 'newName' }, + record: { name: 'newName' }, }, }); expect(result).have.deep.property('record.name', 'newName'); }); - it('should change data via args.input in database', (done) => { + it('should change data via args.record in database', (done) => { const checkedName = 'nameForMongoDB'; updateOne(UserModel, UserTypeComposer).resolve({ args: { filter: { _id: user1.id }, - input: { name: checkedName }, + record: { name: checkedName }, }, }).then(() => { UserModel.collection.findOne({ _id: user1._id }, (err, doc) => { diff --git a/src/resolvers/createOne.js b/src/resolvers/createOne.js index e056aa05..c7318b10 100644 --- a/src/resolvers/createOne.js +++ b/src/resolvers/createOne.js @@ -1,6 +1,6 @@ /* @flow */ /* eslint-disable no-param-reassign */ -import { inputHelperArgs } from './helpers/input'; +import { recordHelperArgs } from './helpers/record'; import { GraphQLObjectType } from 'graphql'; import GraphQLMongoID from '../types/mongoid'; @@ -45,26 +45,26 @@ export default function createOne( }, }), args: { - ...inputHelperArgs(typeComposer, { - inputTypeName: `CreateOne${typeComposer.getTypeName()}Input`, + ...recordHelperArgs(typeComposer, { + recordTypeName: `CreateOne${typeComposer.getTypeName()}Input`, removeFields: ['id', '_id'], isRequired: true, - ...(opts && opts.input), + ...(opts && opts.record), }), }, resolve: (resolveParams: ExtendedResolveParams) => { - const inputData = resolveParams.args && resolveParams.args.input || {}; + const recordData = resolveParams.args && resolveParams.args.record || {}; - if (!(typeof inputData === 'object') - || Object.keys(inputData).length === 0 + if (!(typeof recordData === 'object') + || Object.keys(recordData).length === 0 ) { return Promise.reject( new Error(`${typeComposer.getTypeName()}.createOne resolver requires ` - + 'at least one value in args.input') + + 'at least one value in args.record') ); } - return model.create(inputData) + return model.create(recordData) .then(record => { if (record) { return { diff --git a/src/resolvers/helpers/__tests__/input-test.js b/src/resolvers/helpers/__tests__/input-test.js deleted file mode 100644 index 92e1c3b6..00000000 --- a/src/resolvers/helpers/__tests__/input-test.js +++ /dev/null @@ -1,60 +0,0 @@ -/* @flow */ - -import { expect } from 'chai'; -import { inputHelperArgs } from '../input'; -import { UserModel } from '../../../__mocks__/userModel.js'; -import { GraphQLInputObjectType, GraphQLNonNull } from 'graphql'; -import { InputTypeComposer } from 'graphql-compose'; -import { mongooseModelToTypeComposer } from '../../../modelConverter'; - -const UserTypeComposer = mongooseModelToTypeComposer(UserModel); - -describe('Resolver helper `input` ->', () => { - describe('inputHelperArgs()', () => { - it('should throw error if `inputTypeName` not provided in opts', () => { - expect(() => inputHelperArgs(UserTypeComposer)) - .to.throw('provide non-empty `inputTypeName`'); - }); - - it('should return input field', () => { - const args = inputHelperArgs(UserTypeComposer, { - inputTypeName: 'InputUserType', - }); - expect(args).has.property('input'); - expect(args).has.deep.property('input.name', 'input'); - expect(args).has.deep.property('input.type').instanceof(GraphQLInputObjectType); - }); - - it('should for opts.isRequired=true return GraphQLNonNull', () => { - const args = inputHelperArgs(UserTypeComposer, { - inputTypeName: 'InputUserType', - isRequired: true, - }); - expect(args).has.property('input'); - expect(args).has.deep.property('input.name', 'input'); - expect(args).has.deep.property('input.type').instanceof(GraphQLNonNull); - }); - - it('should remove fields via opts.removeFields', () => { - const args = inputHelperArgs(UserTypeComposer, { - inputTypeName: 'InputUserType', - removeFields: ['name', 'age'], - }); - const inputTypeComposer = new InputTypeComposer(args.input.type); - expect(inputTypeComposer.hasField('name')).to.be.false; - expect(inputTypeComposer.hasField('age')).to.be.false; - expect(inputTypeComposer.hasField('gender')).to.be.true; - }); - - it('should set required fields via opts.requiredFields', () => { - const args = inputHelperArgs(UserTypeComposer, { - inputTypeName: 'InputUserType', - requiredFields: ['name', 'age'], - }); - const inputTypeComposer = new InputTypeComposer(args.input.type); - expect(inputTypeComposer.getFieldType('name')).instanceof(GraphQLNonNull); - expect(inputTypeComposer.getFieldType('age')).instanceof(GraphQLNonNull); - expect(inputTypeComposer.getFieldType('gender')).not.instanceof(GraphQLNonNull); - }); - }); -}); diff --git a/src/resolvers/helpers/__tests__/record-test.js b/src/resolvers/helpers/__tests__/record-test.js new file mode 100644 index 00000000..d2b0d571 --- /dev/null +++ b/src/resolvers/helpers/__tests__/record-test.js @@ -0,0 +1,60 @@ +/* @flow */ + +import { expect } from 'chai'; +import { recordHelperArgs } from '../record'; +import { UserModel } from '../../../__mocks__/userModel.js'; +import { GraphQLInputObjectType, GraphQLNonNull } from 'graphql'; +import { InputTypeComposer } from 'graphql-compose'; +import { mongooseModelToTypeComposer } from '../../../modelConverter'; + +const UserTypeComposer = mongooseModelToTypeComposer(UserModel); + +describe('Resolver helper `record` ->', () => { + describe('recordHelperArgs()', () => { + it('should throw error if `recordTypeName` not provided in opts', () => { + expect(() => recordHelperArgs(UserTypeComposer)) + .to.throw('provide non-empty `recordTypeName`'); + }); + + it('should return input field', () => { + const args = recordHelperArgs(UserTypeComposer, { + recordTypeName: 'RecordUserType', + }); + expect(args).has.property('record'); + expect(args).has.deep.property('record.name', 'record'); + expect(args).has.deep.property('record.type').instanceof(GraphQLInputObjectType); + }); + + it('should for opts.isRequired=true return GraphQLNonNull', () => { + const args = recordHelperArgs(UserTypeComposer, { + recordTypeName: 'RecordUserType', + isRequired: true, + }); + expect(args).has.property('record'); + expect(args).has.deep.property('record.name', 'record'); + expect(args).has.deep.property('record.type').instanceof(GraphQLNonNull); + }); + + it('should remove fields via opts.removeFields', () => { + const args = recordHelperArgs(UserTypeComposer, { + recordTypeName: 'RecordUserType', + removeFields: ['name', 'age'], + }); + const inputTypeComposer = new InputTypeComposer(args.record.type); + expect(inputTypeComposer.hasField('name')).to.be.false; + expect(inputTypeComposer.hasField('age')).to.be.false; + expect(inputTypeComposer.hasField('gender')).to.be.true; + }); + + it('should set required fields via opts.requiredFields', () => { + const args = recordHelperArgs(UserTypeComposer, { + recordTypeName: 'RecordUserType', + requiredFields: ['name', 'age'], + }); + const inputTypeComposer = new InputTypeComposer(args.record.type); + expect(inputTypeComposer.getFieldType('name')).instanceof(GraphQLNonNull); + expect(inputTypeComposer.getFieldType('age')).instanceof(GraphQLNonNull); + expect(inputTypeComposer.getFieldType('gender')).not.instanceof(GraphQLNonNull); + }); + }); +}); diff --git a/src/resolvers/helpers/input.js b/src/resolvers/helpers/input.js deleted file mode 100644 index d214e186..00000000 --- a/src/resolvers/helpers/input.js +++ /dev/null @@ -1,41 +0,0 @@ -/* @flow */ - -import { TypeComposer } from 'graphql-compose'; -import { GraphQLNonNull } from 'graphql'; - -import type { - GraphQLFieldConfigArgumentMap, - inputHelperArgsOpts, -} from '../../definition'; - -export const inputHelperArgs = ( - typeComposer: TypeComposer, - opts: inputHelperArgsOpts -): GraphQLFieldConfigArgumentMap => { - if (!(typeComposer instanceof TypeComposer)) { - throw new Error('First arg for inputHelperArgs() should be instance of TypeComposer.'); - } - - if (!opts || !opts.inputTypeName) { - throw new Error('You should provide non-empty `inputTypeName` in options.'); - } - - const inputTypeName: string = opts.inputTypeName; - const inputComposer = typeComposer.getInputTypeComposer().clone(inputTypeName); - if (opts && opts.removeFields) { - inputComposer.removeField(opts.removeFields); - } - - if (opts && opts.requiredFields) { - inputComposer.makeFieldsRequired(opts.requiredFields); - } - - return { - input: { - name: 'input', - type: opts.isRequired - ? new GraphQLNonNull(inputComposer.getType()) - : inputComposer.getType(), - }, - }; -}; diff --git a/src/resolvers/helpers/record.js b/src/resolvers/helpers/record.js new file mode 100644 index 00000000..7c0d5287 --- /dev/null +++ b/src/resolvers/helpers/record.js @@ -0,0 +1,41 @@ +/* @flow */ + +import { TypeComposer } from 'graphql-compose'; +import { GraphQLNonNull } from 'graphql'; + +import type { + GraphQLFieldConfigArgumentMap, + recordHelperArgsOpts, +} from '../../definition'; + +export const recordHelperArgs = ( + typeComposer: TypeComposer, + opts: recordHelperArgsOpts +): GraphQLFieldConfigArgumentMap => { + if (!(typeComposer instanceof TypeComposer)) { + throw new Error('First arg for recordHelperArgs() should be instance of TypeComposer.'); + } + + if (!opts || !opts.recordTypeName) { + throw new Error('You should provide non-empty `recordTypeName` in options.'); + } + + const recordTypeName: string = opts.recordTypeName; + const recordComposer = typeComposer.getInputTypeComposer().clone(recordTypeName); + if (opts && opts.removeFields) { + recordComposer.removeField(opts.removeFields); + } + + if (opts && opts.requiredFields) { + recordComposer.makeFieldsRequired(opts.requiredFields); + } + + return { + record: { + name: 'record', + type: opts.isRequired + ? new GraphQLNonNull(recordComposer.getType()) + : recordComposer.getType(), + }, + }; +}; diff --git a/src/resolvers/updateById.js b/src/resolvers/updateById.js index b73a6abc..bff13516 100644 --- a/src/resolvers/updateById.js +++ b/src/resolvers/updateById.js @@ -1,7 +1,7 @@ /* @flow */ /* eslint-disable no-param-reassign */ -import { inputHelperArgs } from './helpers/input'; +import { recordHelperArgs } from './helpers/record'; import findById from './findById'; import { GraphQLObjectType } from 'graphql'; import GraphQLMongoID from '../types/mongoid'; @@ -54,32 +54,32 @@ export default function updateById( }, }), args: { - ...inputHelperArgs(typeComposer, { - inputTypeName: `UpdateById${typeComposer.getTypeName()}Input`, + ...recordHelperArgs(typeComposer, { + recordTypeName: `UpdateById${typeComposer.getTypeName()}Input`, requiredFields: ['_id'], isRequired: true, - ...(opts && opts.input), + ...(opts && opts.record), }), }, resolve: (resolveParams: ExtendedResolveParams) => { - const inputData = resolveParams.args && resolveParams.args.input || {}; + const recordData = resolveParams.args && resolveParams.args.record || {}; - if (!(typeof inputData === 'object')) { + if (!(typeof recordData === 'object')) { return Promise.reject( - new Error(`${typeComposer.getTypeName()}.updateById resolver requires args.input value`) + new Error(`${typeComposer.getTypeName()}.updateById resolver requires args.record value`) ); } - if (!inputData._id) { + if (!recordData._id) { return Promise.reject( new Error( - `${typeComposer.getTypeName()}.updateById resolver requires args.input._id value` + `${typeComposer.getTypeName()}.updateById resolver requires args.record._id value` ) ); } - resolveParams.args._id = inputData._id; - delete inputData._id; + resolveParams.args._id = recordData._id; + delete recordData._id; return findByIdResolver.resolve(resolveParams) // save changes to DB @@ -87,8 +87,8 @@ export default function updateById( if (!doc) { return Promise.reject('Document not found'); } - if (inputData) { - doc.set(inputData); + if (recordData) { + doc.set(recordData); return doc.save(); } return doc; diff --git a/src/resolvers/updateMany.js b/src/resolvers/updateMany.js index 9c514a99..fbbe50fd 100644 --- a/src/resolvers/updateMany.js +++ b/src/resolvers/updateMany.js @@ -1,6 +1,6 @@ /* @flow */ /* eslint-disable no-param-reassign */ -import { inputHelperArgs } from './helpers/input'; +import { recordHelperArgs } from './helpers/record'; import { skipHelperArgs, skipHelper } from './helpers/skip'; import { limitHelperArgs, limitHelper } from './helpers/limit'; import { filterHelperArgs, filterHelper } from './helpers/filter'; @@ -48,11 +48,11 @@ export default function updateMany( }, }), args: { - ...inputHelperArgs(typeComposer, { - inputTypeName: `UpdateMany${typeComposer.getTypeName()}Input`, + ...recordHelperArgs(typeComposer, { + recordTypeName: `UpdateMany${typeComposer.getTypeName()}Input`, removeFields: ['id', '_id'], isRequired: true, - ...(opts && opts.input), + ...(opts && opts.record), }), ...filterHelperArgs(typeComposer, { filterTypeName: `FilterUpdateMany${typeComposer.getTypeName()}Input`, @@ -69,14 +69,14 @@ export default function updateMany( }), }, resolve: (resolveParams: ExtendedResolveParams) => { - const inputData = resolveParams.args && resolveParams.args.input || {}; + const recordData = resolveParams.args && resolveParams.args.record || {}; - if (!(typeof inputData === 'object') - || Object.keys(inputData).length === 0 + if (!(typeof recordData === 'object') + || Object.keys(recordData).length === 0 ) { return Promise.reject( new Error(`${typeComposer.getTypeName()}.updateMany resolver requires ` - + 'at least one value in args.input') + + 'at least one value in args.record') ); } @@ -87,7 +87,7 @@ export default function updateMany( limitHelper(resolveParams); resolveParams.query = resolveParams.query.setOptions({ multi: true }); // eslint-disable-line - resolveParams.query.update({ $set: toDottedObject(inputData) }); + resolveParams.query.update({ $set: toDottedObject(recordData) }); return resolveParams.query .exec() diff --git a/src/resolvers/updateOne.js b/src/resolvers/updateOne.js index 4e0685a3..22f0bb66 100644 --- a/src/resolvers/updateOne.js +++ b/src/resolvers/updateOne.js @@ -1,7 +1,7 @@ /* @flow */ /* eslint-disable no-param-reassign */ import { skipHelperArgs } from './helpers/skip'; -import { inputHelperArgs } from './helpers/input'; +import { recordHelperArgs } from './helpers/record'; import { filterHelperArgs } from './helpers/filter'; import { sortHelperArgs } from './helpers/sort'; import findOne from './findOne'; @@ -56,11 +56,11 @@ export default function updateOne( }, }), args: { - ...inputHelperArgs(typeComposer, { - inputTypeName: `UpdateOne${typeComposer.getTypeName()}Input`, + ...recordHelperArgs(typeComposer, { + recordTypeName: `UpdateOne${typeComposer.getTypeName()}Input`, removeFields: ['id', '_id'], isRequired: true, - ...(opts && opts.input), + ...(opts && opts.record), }), ...filterHelperArgs(typeComposer, { filterTypeName: `FilterUpdateOne${typeComposer.getTypeName()}Input`, @@ -74,7 +74,7 @@ export default function updateOne( ...skipHelperArgs(), }, resolve: (resolveParams: ExtendedResolveParams) => { - const inputData = resolveParams.args && resolveParams.args.input || null; + const recordData = resolveParams.args && resolveParams.args.record || null; const filterData = resolveParams.args && resolveParams.args.filter || {}; if (!(typeof filterData === 'object') @@ -89,8 +89,8 @@ export default function updateOne( return findOneResolver.resolve(resolveParams) // save changes to DB .then(doc => { - if (inputData) { - doc.set(inputData); + if (recordData) { + doc.set(recordData); return doc.save(); } return doc;