Comments (3)
This is because the return value of useQuery
uses TData
both in covariant and contravariant positions.
I'd recommend that you use TypedDocumentNode
here and then let inference take over:
const getById = <TData>(gql: TypedDocumentNode<TData>, id: string) => {
return useQuery(
gql,
computed(() => ({ id }))
)
}
and then use it like
const result = getById(MyQuery, "1")
Assuming that your queries are typed correctly (and this is probably the single most important thing to do when using GraphQL with TypeScript!), everything should be typed correctly from there on.
Your manual annotations only add inconsistencies and possibly type erasure, so I'd try to avoid those.
That said, this still breaks the rule of hooks.
What is the real value here instead of just using
const result = useQuery(gql, computed(() => ({ id })))
in your component?
from apollo-client.
Thanks for the hint, I actually forgot about the concept of contravariance, also I was not aware that there is TypedDocumentNode
.
I'm currently abstracting CRUD operations for a multitude of different entity types, where all come with their own queries and result / variables types, hence why I need some form of generic typing for - in this case - an ID-based single entity query. With your hint I got the typing down correct now though, so thanks a lot!
from apollo-client.
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
from apollo-client.
Related Issues (20)
- [Testing utilities] Create separate entrypoint for new testing utilities HOT 4
- `MockLink` doesn't work with `@nonreactive` directive. HOT 2
- [Tests] Investigate flaky test in `src/link/persisted-queries/__tests__/persisted-queries.test.ts`
- subscribeToMore unsubscribes after partial error HOT 3
- `useFragment` doesn't respect the `@nonreactive` directive HOT 2
- `ApolloError.graphQLErrors` are not of type `GraphQLError` HOT 2
- AutoCleanedWeakCache causes failure when running tests in fakeAsync zone HOT 4
- useQuery fails to resolve ssr renderPromise due to JSON.stringify reordering HOT 6
- useSuspenseQuery returns an error instead of throwing an exception HOT 6
- Initializing ApolloClient before DataDog RUM causes GraphQL calls to not have the correct headers HOT 5
- Skipped Query returns outdated data after cache clearing HOT 7
- Merge function at the type level not working (+ suggested fix) HOT 13
- Unexpected Data Fetching with `useBackgroundQuery`, StrictMode, `fetchPolicy`: `network-only` (or `no-cache`), and state changes HOT 6
- When use the Apollo Client server side rendering, How to share the cache in cluster server (k8s)? ( did we have file cache? or db cache? ) HOT 3
- Feedback for schema-driven testing utilities HOT 1
- Add `subscribeToMore` functionality to `useQueryRefHandlers`, `useBackgroundQuery`
- [Docs] Anchor links seem to be stripped HOT 4
- Changes to `context` cause refetches in `useQuery` HOT 11
- Add an option in ApolloClient constructor to return a deep copy of query results (or a way to globally transform results after the caching step) HOT 2
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 apollo-client.