Replies: 3 comments
-
We're currently working on that through |
Beta Was this translation helpful? Give feedback.
-
None of the options we have for typing an undefined field (as per the GraphQL schema) are really satisfactory:
I don't really want to list out all cases, but tl;dr there isn't really a great representation of a field that isn't on the schema, because it simply isn't a possible “state” i.e. the query will fail, there is no corresponding type, etc etc As you're saying the IDE protects you against this, and what we have planned is rather that the CLI will hard-error on these cases: #76 |
Beta Was this translation helpful? Give feedback.
-
Thank to both of you for quick replies. A CLI check would be perfect for our needs. Looking forward to it! |
Beta Was this translation helpful? Give feedback.
-
Our team wants to use this library to cut down on Typescript boilerplate… and boy, do we overtype our requests.
For full typings experience we also want the build to fail on issues – which happens in most cases, but not one. I would like to describe that one case below.
Scenario
Consider the following code:
The query has fields
id
andname
, but notactive
In the IDE, I can see an error:
Cannot query field "active" on type "Field"
The type of
data.fields
isArray<{id: number, name: string, active: unknown }>
Calling
active
does not throw an error.The issue for us here is that the error above is not actually a Typescript error, as it is handled by the LSP. So running
tsc --noEmit
, vite checker plugin, or just building the project won't fail on this missing field, and we are back to runtime errors in production.Proposed solution
What if you were to return the missing field as type
void
instead ofunknown
? In that case Typescript would natively error out on most attempts to use or assign said variable.With the type of
data.fields
beingArray<{id: number, name: string, active: void }>
, callingfield.active
fails as expected.Now, I have no idea if that would break some other behaviour. Hence putting up this thread first, not a PR. Also it's not exactly a bug, I think.
I think the code we are talking about is this line:
gql.tada/src/variables.ts
Line 42 in 1a0b07b
Beta Was this translation helpful? Give feedback.
All reactions