-
|
Hi! type GQLUser {
fullName: String
}
type Query {
getUser(args: Args): GQLUser
}I have a case that there is a query resolver with this shape: Query: {
getUser: (parents, args) => db.readUser(args); // <-- Error #1
}and assuming the db model looks like interface DBUser {
name: string
}And i want to use chained resolver this way: GQLUser: {
fullName: (parent) => parent.name // <-- Error #2
}so the end result of resolvers looks like: import { Resolvers } from '__generated__';
const resolvers: Resolvers = {
GQLUser: {
fullName: (parent) => parent.name // <-- Error #2
},
Query: {
getUser: (parents, args) => db.readUser(args); // <-- Error #1
}
};As noted in the code, it raises 2 problems:
Tried to solve it by setting import { UserResolvers, Resolvers } from '__generated__';
const userResolvers: UserResolvers<Context, DBUser> = {
GQLUser: {
fullName: (parent) => parent.name // <-- now it works
}
};
const resolvers: Resolvers = {
GQLUser: userResolvers, // <-- now this fails with Type 'UserResolvers<Context, DBUser>' is not assignable to type 'UserResolver<Context, GQLUser> | undefined'
Query: {
getUser: (parents, args) => db.readUser(args); // <-- still Error #1
}
};Adding a mapper from GQLUser to DBUser wont help because i want to return GQLUser from is there a way to solve these issues? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
After reading this: https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen |
Beta Was this translation helpful? Give feedback.
After reading this: https://the-guild.dev/blog/better-type-safety-for-resolvers-with-graphql-codegen
i would like to add that the reason that i don't want to use mapper is because the
GQLUserresolver will be omitted there wont be any TS error but clearly the return value ofgetUserwould be wrong and gql will fail at runtime (because nothing will transformDBUsertoGQLUser) and that's what im trying to avoid.