Skip to content

Commit

Permalink
fix(api/guards): fixed null exception
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenSoftware committed May 4, 2022
1 parent 4ac048f commit a807f2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ describe('RejectNestedCreateGuard', () => {
data: {
text: 'sample',
published: true,
stub: null,
stub2: undefined,
author: {
create: {
username: 'mean_human',
Expand Down
16 changes: 9 additions & 7 deletions apps/api/src/app/auth/guards/reject-nested-create.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { ExecutionContext, HttpException, Injectable, Logger } from '@nestjs/com
import { GqlExecutionContext } from '@nestjs/graphql';

export function containsNestedCreate(args: any) {
for (const [key, value] of Object.entries(args)) {
if (key === 'create') {
return true;
}
if (args !== null && args !== undefined) {
for (const [key, value] of Object.entries(args)) {
if (key === 'create') {
return true;
}

if (typeof value === 'object' && containsNestedCreate(value) === true) {
return true;
if (typeof value === 'object' && containsNestedCreate(value) === true) {
return true;
}
}
}

Expand All @@ -20,7 +22,7 @@ export function containsNestedCreate(args: any) {
* Rejects mutations with nested create argument
*/
export class RejectNestedCreateGuard {
static async canActivate(context: ExecutionContext) {
async canActivate(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);

if (ctx.getInfo()?.operation?.operation === 'mutation') {
Expand Down

0 comments on commit a807f2d

Please sign in to comment.