Replies: 1 comment 2 replies
-
@strazto Hi, did you end up finding a solution to your issue? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The problem
I have a lot of resolvers already implemented, with data models that don't yet have types implemented.
I'm looking to incrementally improve the type safety, type inference and IDE auto-completion for my resolvers, but don't wish to implement every single mapper just yet.
Current
any
escape hatch workaroundPresently, I've gotten around this using
any
as the default mapper:For now, the hammer of
any
lets me use the generated fields for type-safety for the arguments of my resolvers, like so:I think this is still an improvement on what I previously had.
But this use of
any
clobbers type inference for my parent objects & resolvers a little more than I'd like.A more elegant solution I'd like
Ideally, I'd like to be able to use generics to refer to fields from the generated types, as well as union this with some fields that are common to any entry in my db (eg,
_key
,_id
, etc), and then fall back toany
, or maybeunknown
ornever
for fields that dont match this.TL;DR
Can anyone recommend a pattern / generic that lets me has a decent
defaultMapper
, ideally unioning something like:while allowing me to add better mapper types incrementally?
What I've been able to find so far
From the docs typescript-resolvers#wrap-default-types-with-partial, I know I can use generics for my types, and from this guide: Better Type Safety for your GraphQL resolvers with GraphQL Codegen, I'm given a reference for how I might use Partial generics.
That blog posts links the following issue comment, on the topic of "default mappers": #1133 (comment),
The common essentially demonstrates using a union between the given type parameter, and a generic string indexed object, to allow some type inference & autocomplete for fields that should be resolved, while allowing reference to fields present on the model but not on the parent type.
The problem, however, is that If I change my config like so:
With the demo'd
StringIndexed
generic:I get errors on my resolvers, particularly in the instances where the field being referenced comes from the generated parent type, as in:
Beta Was this translation helpful? Give feedback.
All reactions