Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typescript-resolvers] Allow passing parent types to Resolvers #5517

Open
Tracked by #8296 ...
migueloller opened this issue Feb 8, 2021 · 5 comments
Open
Tracked by #8296 ...

[typescript-resolvers] Allow passing parent types to Resolvers #5517

migueloller opened this issue Feb 8, 2021 · 5 comments
Labels
core Related to codegen core/cli plugins

Comments

@migueloller
Copy link

migueloller commented Feb 8, 2021

Problem

We use this wonderful tool together with @graphql-utilities/schema to generate type safe mocks for our UI component stories. In those stories, we've given developers the ability to mock the GraphQL schema by passing in resolvers. These resolvers might have different parent types depending on what the developer is presenting in the story. At the moment, it's only possible to generate resolver types with specific parent types or overridable parent types via allowParentTypeOverride. Unfortunately, this means that each resolver needs to have types added, though. Also, allowParentTypeOverride is just for the generics and doesn't change the valid types that can be returned from a resolver and one might want to return a different type like when using mappers.

Solution/Request

It would be great if when using Resolvers we could pass ResolversParentTypes. It would look something like this:

const resolvers: Resolvers<Context, { User: { id: string } }>

Internally, Resolvers would replace the parent types given and fall back to the current behavior when a type is missing. This could be done like this:

type Resolvers<TContext = Context, TResolverParentTypes = ResolverParentTypes> = {
  // ...
  User?: UserResolvers<TContext, (Omit<ResolverParentTypes, keyof TResolverParentTypes> & TResolverParentTypes)['User']
  // ...
}

A similar change would be needed for type resolvers where the mappers are also able to be inferred from this second generic of Resolvers

@migueloller migueloller changed the title [@graphql-codegen/typescript-resolvers] Allow passing parent types to Resolvers [@graphql-codegen/typescript-resolvers] Allow passing parent types to Resolvers Feb 8, 2021
@migueloller migueloller changed the title [@graphql-codegen/typescript-resolvers] Allow passing parent types to Resolvers [typescript-resolvers] Allow passing parent types to Resolvers Feb 8, 2021
@migueloller
Copy link
Author

At the moment we have to use defaultMapper: any and then manually import <TypeName>Resolvers so that we can pass our own custom parent type. Ideally we can just leave the default defaultMapper and just import Resolvers and pass the parent type map as the second generic to Resolvers

@dotansimha
Copy link
Owner

@migueloller this sounds like a very useful idea. What do you think about creating a PR?

@migueloller
Copy link
Author

@dotansimha, I've been meaning to respond to this for a while now. Sorry for the delay! I would love to contribute with a PR, but unfortunately I'm quite busy at my job right now.

@sunaulo-suhail
Copy link

Any update on this?

@migueloller
Copy link
Author

I can't help with a PR right now, but maybe an example of an OSS project that successfully does something similar could help? Here's how Slate, a rich text editing library achieves this with interface declaration merging: https://docs.slatejs.org/concepts/12-typescript#defining-editor-element-and-text-types

The original discussion can be seen here: ianstormtaylor/slate#3725

My suggestion here is to do it via generics, but the result is the same: the ability to extend and customize the types.

@charlypoly charlypoly added the core Related to codegen core/cli label Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to codegen core/cli plugins
Projects
None yet
Development

No branches or pull requests

4 participants