I setup graphql & a app guard for my nestjs project, but has some problem with the process order of request.
This is my dto input for create a Post object
import { InputType, Field } from '@nestjs/graphql';
import { IsNotEmpty, IsNumber } from 'class-validator';
@InputType()
export class CreatePostDto {
@Field()
@IsNotEmpty()
title: string;
@Field()
@IsNotEmpty()
content: string;
@Field()
@IsNotEmpty()
@IsNumber()
authorId: number;
}
First i try to set authorId as string “4” it return like this
It return the error of input
But i expect it to return unauthorized error (exception throw in app guard). This is full output
{
"error": {
"errors": [
{
"message": "Float cannot represent non numeric value: "4"",
"locations": [
{
"line": 2,
"column": 72
}
],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"stacktrace": [
"GraphQLError: Float cannot represent non numeric value: "4"",
" at GraphQLScalarType.parseLiteral (D:\Code\NodeJS\fstack\backend\node_modules\graphql\type\scalars.js:169:13)",
" at isValidValueNode (D:\Code\NodeJS\fstack\backend\node_modules\graphql\validation\rules\ValuesOfCorrectTypeRule.js:177:30)",
" at Object.StringValue (D:\Code\NodeJS\fstack\backend\node_modules\graphql\validation\rules\ValuesOfCorrectTypeRule.js:141:28)",
" at Object.enter (D:\Code\NodeJS\fstack\backend\node_modules\graphql\language\visitor.js:301:32)",
" at Object.enter (D:\Code\NodeJS\fstack\backend\node_modules\graphql\utilities\TypeInfo.js:391:27)",
" at visit (D:\Code\NodeJS\fstack\backend\node_modules\graphql\language\visitor.js:197:21)",
" at validate (D:\Code\NodeJS\fstack\backend\node_modules\graphql\validation\validate.js:91:24)",
" at processGraphQLRequest (D:\Code\NodeJS\fstack\backend\node_modules\@apollo\server\src\requestPipeline.ts:246:40)",
" at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
" at internalExecuteOperation (D:\Code\NodeJS\fstack\backend\node_modules\@apollo\server\src\ApolloServer.ts:1331:12)"
]
}
}
]
}
}
The auth guard is set as the APP_GUARD and still work well when i post right input( authorId is 4 as number)
It return exception of guard
this is full output
{
"errors": [
{
"message": "Unauthorized",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createPost"
],
"extensions": {
"code": "UNAUTHENTICATED",
"stacktrace": [
"UnauthorizedException: Unauthorized",
" at AuthGuard.canActivate (D:\Code\NodeJS\fstack\backend\src\common\guards\auth.guard.ts:35:10)",
" at GuardsConsumer.tryActivate (D:\Code\NodeJS\fstack\backend\node_modules\@nestjs\core\guards\guards-consumer.js:15:34)",
" at canActivateFn (D:\Code\NodeJS\fstack\backend\node_modules\@nestjs\core\helpers\external-context-creator.js:155:59)",
" at target (D:\Code\NodeJS\fstack\backend\node_modules\@nestjs\core\helpers\external-context-creator.js:73:37)",
" at Object.createPost (D:\Code\NodeJS\fstack\backend\node_modules\@nestjs\core\helpers\external-proxy.js:9:30)",
" at field.resolve (D:\Code\NodeJS\fstack\backend\node_modules\@apollo\server\src\utils\schemaInstrumentation.ts:82:22)",
" at executeField (D:\Code\NodeJS\fstack\backend\node_modules\graphql\execution\execute.js:492:20)",
" at D:\Code\NodeJS\fstack\backend\node_modules\graphql\execution\execute.js:377:22",
" at promiseReduce (D:\Code\NodeJS\fstack\backend\node_modules\graphql\jsutils\promiseReduce.js:23:9)",
" at executeFieldsSerially (D:\Code\NodeJS\fstack\backend\node_modules\graphql\execution\execute.js:373:43)"
],
"originalError": {
"message": "Unauthorized",
"statusCode": 401
}
}
}
],
"data": null
}
It seem that graphql validate inputType for graphql query process before the app guard. Is that a bug or i use it wrong