Skip to content

Commit

Permalink
feat: Allow to opt-out of mutation payload.error field (#314)
Browse files Browse the repository at this point in the history
* Allow to opt-out of mutation payload.error

Signed-off-by: Egi Ginting <[email protected]>

* Add test case

Signed-off-by: Egi Ginting <[email protected]>
  • Loading branch information
exneval authored Mar 18, 2021
1 parent d463913 commit fde394b
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 24 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

Expand All @@ -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;
}
```

Expand Down Expand Up @@ -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;
}
```

Expand All @@ -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;
}
```

Expand All @@ -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;
}
```

Expand All @@ -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;
}
```

Expand All @@ -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;
}
```

Expand All @@ -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;
}
```

Expand Down
153 changes: 153 additions & 0 deletions src/__tests__/github_issues/286-test.ts
Original file line number Diff line number Diff line change
@@ -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<IUser>('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
}"
`);
});
});
10 changes: 7 additions & 3 deletions src/resolvers/createMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -114,9 +116,11 @@ export function createMany<TSource = any, TContext = any, TDoc extends Document
},
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/createOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export 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;
}

type TArgs = {
Expand Down Expand Up @@ -95,9 +97,11 @@ export function createOne<TSource = any, TContext = any, TDoc extends Document =
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/removeById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export 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;
}

type TArgs = {
Expand Down Expand Up @@ -83,9 +85,11 @@ export function removeById<TSource = any, TContext = any, TDoc extends Document
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/removeMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export 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;
}

type TArgs = {
Expand Down Expand Up @@ -108,9 +110,11 @@ export function removeMany<TSource = any, TContext = any, TDoc extends Document
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/removeOne.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export 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;
}

type TArgs = {
Expand Down Expand Up @@ -107,9 +109,11 @@ export function removeOne<TSource = any, TContext = any, TDoc extends Document =
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/updateById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export 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;
}

type TArgs = {
Expand Down Expand Up @@ -117,9 +119,11 @@ export function updateById<TSource = any, TContext = any, TDoc extends Document
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
10 changes: 7 additions & 3 deletions src/resolvers/updateMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface UpdateManyResolverOpts {
sort?: SortHelperArgsOpts | false;
limit?: LimitHelperArgsOpts | false;
skip?: false;
/** Customize payload.error field. If true, then this field will be removed. */
disableErrorField?: boolean;
}

type TArgs = {
Expand Down Expand Up @@ -142,9 +144,11 @@ export function updateMany<TSource = any, TContext = any, TDoc extends Document
}) as any,
});

// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
if (!opts?.disableErrorField) {
// Add `error` field to payload which can catch resolver Error
// and return it in mutation payload
addErrorCatcherField(resolver);
}

return resolver;
}
Loading

0 comments on commit fde394b

Please sign in to comment.