-
Notifications
You must be signed in to change notification settings - Fork 17
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
Allow Grats to validate context values #21
Comments
Another option is to simply enforce that the So this would be valid: function resolverOne(_: Foo, args: {}, context: MyContextType) {}
function resolverTwo(_: Bar, args: {}, context: MyContextType) {} but this would make Grats emit an error: function resolverOne(_: Foo, args: {}, context: MyContextType) {}
function resolverTwo(_: Bar, args: {}, context: string) {} because Similarly, this would be fine: function resolverOne(_: Foo, args: {}, context: string) {}
function resolverTwo(_: Bar, args: {}, context: string) {} This as well: function resolverOne(_: Foo, args: {}, context: MyContextType) {}
function resolverTwo(_: Bar, args: {}) {} // this doesn't use the `context` arg And maybe, this could be allowed as well function resolverOne(_: Foo, args: {}, context: MyContextType) {}
function resolverTwo(_: Bar, args: {}, context: unknown) {} Typing it as IMO this is an elegant way for Grats to enforce the type of It still on the developer to ensure the type of the context passed to the executor is the same the resolvers expect, I doubt Grats can help with that... |
Oh I like this! We can probably start here and follow-up with an explicit
option if we find there’s some value in making it more explicit.
I may pick a slightly more opinionated restriction: the ctx argument must
be typed using a type that resolves to the same definition. That saves us
from having to actually perform type checking (which is not fully supported
by the TS library)
…On Wed, Nov 1, 2023 at 8:00 PM Armando Pérez Marqués < ***@***.***> wrote:
Another option is to simply enforce that the context argument (if
present) on all resolvers have to reference the exact same type. I doesn't
matter what type it is (Request/string/{}/etc) but they all *have* to be
the same.
So this would be valid:
function resolverOne(_: Foo, args: {}, context: MyContextType) {}function resolverTwo(_: Bar, args: {}, context: MyContextType) {}
but this would make Grats emit an error:
function resolverOne(_: Foo, args: {}, context: MyContextType) {}function resolverTwo(_: Bar, args: {}, context: string) {}
because MyContextType is not string.
Similarly, this would be fine:
function resolverOne(_: Foo, args: {}, context: string) {}function resolverTwo(_: Bar, args: {}, context: string) {}
This as well:
function resolverOne(_: Foo, args: {}, context: MyContextType) {}function resolverTwo(_: Bar, args: {}) {} // this doesn't use the `context` arg
And maybe, this could be allowed as well
function resolverOne(_: Foo, args: {}, context: MyContextType) {}function resolverTwo(_: Bar, args: {}, context: unknown) {}
Typing it as unknown would be a safe-ish escape hatch, if one can't
really refer to a concrete type there, forcing the resolver implementation
to unsafely cast context to something else, or dynamically inspect the
value with instanceof or type guards etc.
IMO this is an elegant way for Grats to enforce the type of context,
doesn't require a config or docblock tags, etc.
—
Reply to this email directly, view it on GitHub
<#21 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABHXL5D2LBPII4KYJDQWD3YCMEDDAVCNFSM6AAAAAA62FYMDOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBZHE4DMMRQGU>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
A related topic to context variable typing is the ability to support positional arguments. I've written an issue for that in #23. In short, if Grats can explicitly know which arg is typed as the context value (for example via a |
I've tried out the |
Kinda torn on positional arguments... I can see how it might be more ergonomic for others; OTOH, since GraphQL field arguments are keyword arguments, IMO it's nice that the Or maybe it's just that the |
Btw I agree, it has to (nominally) resolve to the same type, so for example:
It's my understanding that for TS, those two |
The third argument to a resolver is always the request context. This can be anything the user configures, so it's generally hard to typecheck effectively. However, Grats knows each function/method that is a GraphQL field (thanks to the
@gqlField
annotations) so could be charged with ensuring all fields are typed in a way that matches the developer's expectations.This would require some way for Grats users to inform users what type they are passing to their executor. I'll need to brainstorm what options make sense here:
GratsContextValue
?) that can be defined by the user in their code and Grats will look for/** @gqlContext */
on the context type declaration.We should also examine how existing TS GraphQL servers address this. I'm thinking specifically of TypeGraphQL and Pothos.
The text was updated successfully, but these errors were encountered: