I’m developing an API using apollo-server and type-graphql, but when I try to send an Input througth a Query or a Mutation arguments I don’t know what is happenning but I cannot get the data
UserResolver.ts
import { Arg, Query, Resolver } from 'type-graphql'
import { UserModel } from '../dtos/models/UserModel'
import { CreateUserInput } from '../dtos/inputs/CreateUserInput'
@Resolver(() => UserModel)
export class UserResolver {
// THIS WORK PERFECTLY
@Query(() => String)
async user(@Arg('name') name: string) {
return name
}
// THIS BREAKS
@Query(() => String)
async users(@Arg('data') data: CreateUserInput) {
return data.email
}
}
CreateUserInput.ts
import { Field, InputType } from 'type-graphql'
@InputType()
export class CreateUserInput {
@Field()
email: string
@Field()
password: string
@Field()
admin: boolean
}
The error
{
"errors": [
{
"message": "Argument Validation Error",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"users"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"validationErrors": [
{
"target": {
"email": "email",
"password": "password",
"admin": false
},
"children": [],
"constraints": {
"unknownValue": "an unknown value was passed to the validate function"
}
}
],
"stacktrace": [
"Error: Argument Validation Error",
" at Object.validateArg (/home/cerutti/Projects/good-morning-jenny-api/node_modules/type-graphql/dist/resolvers/validate-arg.js:29:15)",
" at processTicksAndRejections (node:internal/process/task_queues:95:5)",
" at async Promise.all (index 0)"
]
}
}
}
],
"data": null
}
I’ve tried everything form this page of the documentation https://typegraphql.com/docs/resolvers.html#input-types
New contributor
1 Answer
Looking at some of my own code, it looks like I have used @Args
instead of @Arg
– I seem to recall having an issue with @Arg
. So for example:
@Query(() => ItemsList)
async listItems(
@Args('listItemsInput') listItemsInput: ListItemsInput,
): Promise<ItemsList> {
return this.itemsService.list(listItemsInput);
}
That said, I noticed that the resolver in question seems to be intended to create a user – you may need to use @Mutation
rather than @Query
– not sure if that might also make a difference in what you’re getting back.