Generate resolver return types of the same shape as the client graphql query/mutation #5637
-
Hello, TL;DR: I would like to generate the resolver return types that match the queries and mutations I execute client side but don't know if this is possible? I started using graphql-code-generator but am having an issue with my return response types when trying to return non full model, but only partial. User model: @ObjectType({ implements: BaseEntity })
export class User extends BaseEntity {
@Field({ nullable: true })
name?: string;
@Field({ nullable: true })
job?: Job;
} Job model: @ObjectType({ implements: BaseEntity })
export class Job extends BaseEntity {
@Field()
name: string;
@Field()
category: string;
} Client side I execute a query that should return: fragment User on User {
id
name
job {
name
}
}
query GetUsers {
getUsers {
...User
}
}
In my resolver I do a raw query that return exactly the data returned by the graphql query. @Query(() => [User], { nullable: true })
getUsers(): Promise<User[]> {
// Sql query that returns all users with only their id, name and job name.
return users = ...
} The problem is as I return User[] from the promise, it excepts a full User entity, thus throwing the error: Cannot return null for non-nullable field Job.category. I don't know if I am missing something but it doesn't seem that codegen generated types directly for the whole GetUsers query, something like Thanks in advance for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You can use |
Beta Was this translation helpful? Give feedback.
You can use
mappers
configuration and use any kind of type/class that you wish. see: https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen