Comments (6)
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.
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...
from grats.
from grats.
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 /** @gqlContext */
tag), it could allow you to define your arguments as positional arguments. See the #23 for more context.
from grats.
I've tried out the @gqlContext
approach (without positional arguments) here: #24
from grats.
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 args
mirrors that. Also, keeping the resolvers' signature as close as possible to what graphql-js
expects allows for easy migration to Grats, which is good for adoption.
Or maybe it's just that the args: {/* ... */}
syntax is most familiar to me, and my brain is just resisting change 😅
from grats.
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)
Btw I agree, it has to (nominally) resolve to the same type, so for example:
function resolverOne(_: Foo, args: {}, context: { foo: string; bar: number }) {}
function resolverTwo(_: Bar, args: {}, context: { foo: string; bar: number }) {}
It's my understanding that for TS, those two context
args don't have the same type, but rather two independent types that happen to be 100% compatible between them. Proper type checking is needed to ensure that the types actually are compatible. Also, in practice, devs will name the type and then refer to that by name (possibly with the @gqlContext
added as well) so IMO it's Ok to reject these cases and provide actions ("put the type under a name and use the name instead") as part of the error message.
from grats.
Related Issues (20)
- Support defining interfaces by extending classes
- Format error messages using markdown
- Support defining input types with TypeScript interfaces
- TS Plugin Feature Ideas HOT 2
- Report an error if unknown config options are passed HOT 1
- Fails with (0 , graphql_1.assertName) is not a function if using GraphQL 15.3.0
- When used with earlier versions of TypeScript (^3.8.3) all docblocks are reported as detached
- Create advanced example app
- __typename must be const HOT 5
- Check if we correctly report @gqlField on method/prop of non @gql* construct
- Clarify license is MIT
- Add support for @specifiedBy HOT 1
- Bug: Import paths in the generated typescript schema file contains backslashes when codegen is run on Windows 11 HOT 8
- Union used in a union should not report a __typename missing error
- Have generated schema file export a NodeInterfaceTypenames type
- Add the ability to define fields using static methods just like exported functions
- Allow function fields to be generic
- Always emit stable line endings
- Allow user to specify `importModuleSpecifierEnding` in their config
- Example: likes' count HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grats.