question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapper types don't apply everywhere they should for typescript-resolvers

See original GitHub issue

Describe 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 Posts required fields filled in.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dotansimhacommented, Mar 19, 2019

Fix available in 1.0.0 👍

1reaction
kamilkisielacommented, Mar 16, 2019

It’s going to be fixed in the upcoming release, right @dotansimha ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better Type Safety for your GraphQL resolvers with GraphQL ...
Type -check your resolvers' implementation in order to find issues in build-time. Simply integrate with your existing TypeScript data models ...
Read more >
typescript - How to apply mappers only to the parent and not to ...
I tried with a graphql-codegen plugin using a property called mappers which allows to override the default parent and return type of resolvers...
Read more >
Local resolvers - Apollo GraphQL Docs
The resolver map is an object with resolver functions for each GraphQL object type. To visualize how this all lines up, it's useful...
Read more >
GraphQL | RedwoodJS Docs
The only Redwood-specific thing you should really be aware of is resolver args. ... And that's because if you don't define a resolver,...
Read more >
4. Functions - Programming TypeScript [Book] - O'Reilly
When you invoke a function in TypeScript, you don't need to provide any additional type information—just pass in some arguments, and TypeScript will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found