TypedDocumentNode react-query hooks #1557
Unanswered
dusty
asked this question in
Show and tell
Replies: 3 comments 8 replies
-
Why wouldn't you just use the hooks generated by the code generator? |
Beta Was this translation helpful? Give feedback.
6 replies
-
Was wondering how you handle hydration? |
Beta Was this translation helpful? Give feedback.
2 replies
-
I think the type of |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I stumbled upon
TypedDocumentNode
and its a pretty nice helper for typescript and graphql.https://the-guild.dev/blog/typed-document-node
https://graphql-code-generator.com/docs/plugins/typed-document-node
Using that, I was able to build a pretty simple and fully typed wrapper around graphql using fetch and then utilize that in some simple react-query graphql hooks. In this implementation, I prefixes all cache keys with the operationName, which makes it easy to do cache invalidations after a mutation.
I made a little sample here at this codepen, feel free to check it out. Let me know if you see any opportunities for improvement.
https://codesandbox.io/s/festive-villani-ngswo?file=/src/App.tsx
NOTE: The free api I'm using doesn't actually persist the mutations, so you won't see a create mutation show up in the list, but you can see the that the query was invalidated and refetched after the mutation.
Here is a taste of what the API looks like. See the
useGraphql.ts
file for implementation details.You can see the types come through
The idea is that you create some
.graphql
files with your schema and then your documents. You runnpm run codegen
and it will spit out all the types and the typed document nodes into a file. You can then import those typed document nodes and pass the to any of theuseGraphql
functions.Beta Was this translation helpful? Give feedback.
All reactions