TypeScript tweaks for “React Query with a custom fetcher setup” (useGraphQL) #9115
connorjs
started this conversation in
Docs Discussions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Overview
I have just finished trying out the recommended
@tanstack/react-query
+graphql-request
approach described in the docs. I wanted to discuss a few changes I made when writing theuseGraphql
hook in my own project.#8989 Presents a similar discussion, but I felt creating a new one was appropriate here.
Goal
I would like to receive feedback and/or discuss my changes. I plan to submit a pull request to change the documentation following this. (Even if only some minor pieces and not all of these.)
Thanks in advance for thoughts!
Original code
For completeness, the following shows the doc code at time of writing. (I will omit imports for brevity.)
No
any
The first change I made was to remove the
any
. I usedOperationDefinitionNode
instead given my understanding ofTypedDocumentNode
(and confirmed with my own query).I added some extra code as a result (for example, throwing an error for no name), but we do not need to include that in the update.
Change
variables
typeI made a couple changes to the
variable
types.First, I used
undefined
instead of[]
to remove the (imo) hard-to-read (for non TS experts) type spreading. This changes the hook to always require two parameters, but (a) few GraphQL queries actually have no variables and (b) many APIs seem to prefer “consistency” over “cute” these days.Second, this also means that we no longer need the ternary (or nullish coalescing operator) for the third parameter to
request
.ESLint disable
When I add Tanstack’s Query ESLint plugin (see docs), the
@tanstack/query/exhaustive-deps
rule throws. As mentioned in the related discussion, this is fine (although I found that discussion only once writing up this discussion). My code includes an eslint-disable comment.Naming
Finally, I renamed
useGraphql
touseGraphqlQuery
to allow for the correspondinguseGraphqlMutation
to build on top of Tanstack’suseMutation
. Note: I have not actually built/tried the mutation side. That’s up next on my list.My code
My code in its current form follows. Note: I prefer the
*T
naming for generics; this habit comes from Google’s style guide from my Java days.Beta Was this translation helpful? Give feedback.
All reactions