Skip to content

Commit

Permalink
Rename input arg in mutation to record (due graphql-compose-relay…
Browse files Browse the repository at this point in the history
… compatibility).
  • Loading branch information
nodkz committed Jul 8, 2016
1 parent 58af81c commit 166605a
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 190 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -169,15 +169,15 @@ export type typeConverterResolversOpts = {
filter?: filterHelperArgsOpts | false,
},
createOne?: false | {
input?: inputHelperArgsOpts | false,
record?: recordHelperArgsOpts | false,
},
count?: false | {
filter?: filterHelperArgsOpts | false,
},
};
```

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
Expand All @@ -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
};
Expand Down
2 changes: 1 addition & 1 deletion resources/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function exec(command, options) {
cmd: cmd,
env: {
...process.env,
BABEL_ENV: 'cjs',
},
stdio: 'inherit'
});
Expand Down Expand Up @@ -122,6 +121,7 @@ function runTests(filepaths) {
// '--reporter', 'progress',
'--reporter', 'dot',
'--require', './resources/mocha-bootload',
'--require', 'babel-polyfill',
].concat(
allTests(filepaths) ?
filepaths.map(srcPath) :
Expand Down
14 changes: 7 additions & 7 deletions src/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export type ExtendedResolveParams = ResolveParams & {
export type genResolverOpts = {
filter?: filterHelperArgsOpts,
sort?: sortHelperArgsOpts,
input?: inputHelperArgsOpts,
record?: recordHelperArgsOpts,
limit?: limitHelperArgsOpts,
}

Expand Down Expand Up @@ -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,
Expand All @@ -168,7 +168,7 @@ export type typeConverterResolversOpts = {
filter?: filterHelperArgsOpts | false,
},
createOne?: false | {
input?: inputHelperArgsOpts | false,
input?: recordHelperArgsOpts | false,
},
count?: false | {
filter?: filterHelperArgsOpts | false,
Expand All @@ -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[],
Expand Down
18 changes: 9 additions & 9 deletions src/resolvers/__tests__/createOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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');
Expand All @@ -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) => {
Expand All @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions src/resolvers/__tests__/updateById-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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) => {
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions src/resolvers/__tests__/updateMany-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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) => {
Expand All @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/resolvers/__tests__/updateOne-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand All @@ -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) => {
Expand Down
18 changes: 9 additions & 9 deletions src/resolvers/createOne.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 166605a

Please sign in to comment.