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
Uh oh!
There was an error while loading. Please reload this page.
-
Overview
I have just finished trying out the recommended
@tanstack/react-query+graphql-requestapproach described in the docs. I wanted to discuss a few changes I made when writing theuseGraphqlhook 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
anyThe first change I made was to remove the
any. I usedOperationDefinitionNodeinstead 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
variablestypeI made a couple changes to the
variabletypes.First, I used
undefinedinstead 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-depsrule 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
useGraphqltouseGraphqlQueryto allow for the correspondinguseGraphqlMutationto 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
*Tnaming for generics; this habit comes from Google’s style guide from my Java days.Beta Was this translation helpful? Give feedback.
All reactions