From fde394b58db3daf1721b670a79d3217ec6fb8a4f Mon Sep 17 00:00:00 2001 From: Egi Ginting Date: Thu, 18 Mar 2021 20:06:05 +0700 Subject: [PATCH] feat: Allow to opt-out of mutation `payload.error` field (#314) * Allow to opt-out of mutation payload.error Signed-off-by: Egi Ginting * Add test case Signed-off-by: Egi Ginting --- README.md | 16 +++ src/__tests__/github_issues/286-test.ts | 153 ++++++++++++++++++++++++ src/resolvers/createMany.ts | 10 +- src/resolvers/createOne.ts | 10 +- src/resolvers/removeById.ts | 10 +- src/resolvers/removeMany.ts | 10 +- src/resolvers/removeOne.ts | 10 +- src/resolvers/updateById.ts | 10 +- src/resolvers/updateMany.ts | 10 +- src/resolvers/updateOne.ts | 10 +- 10 files changed, 225 insertions(+), 24 deletions(-) create mode 100644 src/__tests__/github_issues/286-test.ts diff --git a/README.md b/README.md index 3df36029..0460e75b 100644 --- a/README.md +++ b/README.md @@ -409,6 +409,8 @@ interface CreateManyResolverOpts { records?: RecordHelperArgsOpts; /** Customize payload.recordIds field. If false, then this field will be removed. */ recordIds?: PayloadRecordIdsHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -422,6 +424,8 @@ interface CreateOneResolverOpts { record?: RecordHelperArgsOpts; /** Customize payload.recordId field. If false, then this field will be removed. */ recordId?: PayloadRecordIdHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -573,6 +577,8 @@ interface RemoveByIdResolverOpts { suffix?: string; /** Customize payload.recordId field. If false, then this field will be removed. */ recordId?: PayloadRecordIdHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -585,6 +591,8 @@ interface RemoveManyResolverOpts { /** Customize input-type for `filter` argument. If `false` then arg will be removed. */ filter?: FilterHelperArgsOpts | false; limit?: LimitHelperArgsOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -599,6 +607,8 @@ interface RemoveOneResolverOpts { sort?: SortHelperArgsOpts | false; /** Customize payload.recordId field. If false, then this field will be removed. */ recordId?: PayloadRecordIdHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -612,6 +622,8 @@ interface UpdateByIdResolverOpts { record?: RecordHelperArgsOpts; /** Customize payload.recordId field. If false, then this field will be removed. */ recordId?: PayloadRecordIdHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -628,6 +640,8 @@ interface UpdateManyResolverOpts { sort?: SortHelperArgsOpts | false; limit?: LimitHelperArgsOpts | false; skip?: false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` @@ -645,6 +659,8 @@ interface UpdateOneResolverOpts { skip?: false; /** Customize payload.recordId field. If false, then this field will be removed. */ recordId?: PayloadRecordIdHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } ``` diff --git a/src/__tests__/github_issues/286-test.ts b/src/__tests__/github_issues/286-test.ts new file mode 100644 index 00000000..2b14f83b --- /dev/null +++ b/src/__tests__/github_issues/286-test.ts @@ -0,0 +1,153 @@ +import { SchemaComposer } from 'graphql-compose'; +import { composeMongoose } from '../../index'; +import { mongoose } from '../../__mocks__/mongooseCommon'; +import { Document } from 'mongoose'; + +const schemaComposer = new SchemaComposer<{ req: any }>(); + +const UserSchema = new mongoose.Schema({ + _id: { type: Number }, + name: { type: String, required: true }, + age: { type: Number }, +}); +interface IUser extends Document { + _id: number; + name: string; + age?: number; +} + +const UserModel = mongoose.model('User', UserSchema); +const UserTC = composeMongoose(UserModel, { schemaComposer }); + +schemaComposer.Query.addFields({ + userById: UserTC.mongooseResolvers.findById(), + userFindOne: UserTC.mongooseResolvers.findOne(), +}); + +// const schema = schemaComposer.buildSchema(); +// console.log(schemaComposer.toSDL()); + +beforeAll(async () => { + await UserModel.base.createConnection(); + await UserModel.create({ _id: 1, name: 'User1' }); +}); +afterAll(() => UserModel.base.disconnect()); + +describe('issue #286 - Allow to provide `disableErrorField` option for mutation resolvers configs', () => { + it('Resolver:createMany', () => { + const resolver = UserTC.mongooseResolvers.createMany({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type CreateManyUserPayload { + \\"\\"\\"Documents IDs\\"\\"\\" + recordIds: [Int!]! + + \\"\\"\\"Created documents\\"\\"\\" + records: [User!] + + \\"\\"\\"Number of created documents\\"\\"\\" + createdCount: Int! + }" + `); + }); + + it('Resolver:createOne', () => { + const resolver = UserTC.mongooseResolvers.createOne({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type CreateOneUserPayload { + \\"\\"\\"Document ID\\"\\"\\" + recordId: Int + + \\"\\"\\"Created document\\"\\"\\" + record: User + }" + `); + }); + + it('Resolver:removeById', () => { + const resolver = UserTC.mongooseResolvers.removeById({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type RemoveByIdUserPayload { + \\"\\"\\"Document ID\\"\\"\\" + recordId: Int + + \\"\\"\\"Removed document\\"\\"\\" + record: User + }" + `); + }); + + it('Resolver:removeMany', () => { + const resolver = UserTC.mongooseResolvers.removeMany({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type RemoveManyUserPayload { + \\"\\"\\"Affected documents number\\"\\"\\" + numAffected: Int + }" + `); + }); + + it('Resolver:removeOne', () => { + const resolver = UserTC.mongooseResolvers.removeOne({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type RemoveOneUserPayload { + \\"\\"\\"Document ID\\"\\"\\" + recordId: Int + + \\"\\"\\"Removed document\\"\\"\\" + record: User + }" + `); + }); + + it('Resolver:updateById', () => { + const resolver = UserTC.mongooseResolvers.updateById({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type UpdateByIdUserPayload { + \\"\\"\\"Document ID\\"\\"\\" + recordId: Int + + \\"\\"\\"Updated document\\"\\"\\" + record: User + }" + `); + }); + + it('Resolver:updateMany', () => { + const resolver = UserTC.mongooseResolvers.updateMany({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type UpdateManyUserPayload { + \\"\\"\\"Affected documents number\\"\\"\\" + numAffected: Int + }" + `); + }); + + it('Resolver:updateOne', () => { + const resolver = UserTC.mongooseResolvers.updateOne({ + disableErrorField: true, + }); + expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(` + "type UpdateOneUserPayload { + \\"\\"\\"Document ID\\"\\"\\" + recordId: Int + + \\"\\"\\"Updated document\\"\\"\\" + record: User + }" + `); + }); +}); diff --git a/src/resolvers/createMany.ts b/src/resolvers/createMany.ts index 9eeea2cd..c5965aff 100644 --- a/src/resolvers/createMany.ts +++ b/src/resolvers/createMany.ts @@ -12,6 +12,8 @@ export interface CreateManyResolverOpts { records?: RecordHelperArgsOpts; /** Customize payload.recordIds field. If false, then this field will be removed. */ recordIds?: PayloadRecordIdsHelperOpts | false; + /** Customize payload.error field. If true, then this field will be removed. */ + disableErrorField?: boolean; } type TArgs = { @@ -114,9 +116,11 @@ export function createMany