Mapper types don't apply everywhere they should for typescript-resolvers
See original GitHub issueDescribe the bug Hey all, I opened a task for this a month ago, but I think the test case isn’t quite right. Sorry for disappearing there, I went offline for thanksgiving and this fell off my radar. I can fix, but I want to confirm that this is actually a problem and I’m not missing something.
Test case addition for #886 https://github.com/dotansimha/graphql-code-generator/commit/b1bf6983522211a0432392ff3d8cea6b014e98dc?diff=unified
So let’s say I’m writing a resolver using apollo-server for the following schema’s mutation: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript-resolvers/tests/typescript-resolvers.spec.ts#L779-L789
// Assume I have mapper from Post to PostEntity
const resolvers: { RootMutation: RootMutationResolvers.Resolvers } = {
RootMutation: {
async upvotePost(id) {
// ... perform mutation code
// reload entity
const post = await PostEntity.findOne({ id });
// This is a type error using the generated code
// as UpvotePostPayload is presumably: (see 2 below)
return { post };
}
}
}
UpvotePostPayload is presumably here:
export interface UpvotePostPayload {
post?: Maybe<Post>;
}
… but then I have to return Post
in my upvotePost
function, not PostEntity
. Apollo handles nested resolvers fine.
What I expect this to essentially be, for RootMutationResolver
is, notice the changes:
- export interface UpvotePostPayload {
+ export interface UpvotePostPayload<T = Post> {
- post?: Maybe<Post>;
+ post?: Maybe<T>;
}
export namespace RootMutationResolvers {
export interface Resolvers<Context = {}, TypeParent = {}> {
- upvotePost?: UpvotePostResolver<Maybe<UpvotePostPayload>, TypeParent, Context>;
+ upvotePost?: UpvotePostResolver<Maybe<UpvotePostPayload<PostEntity>>, TypeParent, Context>;
}
- export type UpvotePostResolver<R = Maybe<UpvotePostPayload>, Parent = {}, Context = {}> = Resolver<R, Parent, Context, UpvotePostArgs>;
+ export type UpvotePostResolver<R = Maybe<UpvotePostPayload<PostEntity>>, Parent = {}, Context = {}> = Resolver<R, Parent, Context, UpvotePostArgs>;
export interface UpvotePostArgs {
id: string;
}
}
… as things stand now, I would have to map PostEntity
to Post
in this mutation, with all of Post
s required fields filled in.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
Fix available in 1.0.0 👍
It’s going to be fixed in the upcoming release, right @dotansimha ?