Comments (6)
Just curious, why was this closed?
It was closed by automation.
from graphql-hooks.
@machineghost I don't believe adding support for TypedDocumentNode
makes sense for this library. This feature is intended to allow a response type to be inferred from the DocumentNode
object passed to the request, however, graphql-hooks
works with GQL queries as strings. All of the current library methods support generic types so it is possible to provide type information for your requests e.g.
import { useQuery } from 'graphql-hooks'
type MyResponse = {
id: string
name: string
}
const HOMEPAGE_QUERY = `query HomePage($limit: Int) {
users(limit: $limit) {
id
name
}
}`
function MyComponent() {
const { loading, error, data } = useQuery<MyResponse>(HOMEPAGE_QUERY, {
variables: {
limit: 10
}
})
if (loading) return 'Loading...'
if (error) return 'Something Bad Happened'
return (
<ul>
{data.users.map(({ id, name }) => (
<li key={id}>{name}</li>
))}
</ul>
)
}
I'll update the existing library documentation to make the typescript integration clearer.
from graphql-hooks.
Just curious, why was this closed? Is it a lot of work to implement, or does graphql-hooks
just not care about supporting TypeScript?
from graphql-hooks.
@luke88jones typed document node is supposed to be used alongside of the code generator, which will generate overloaded functions that will map queries declared as string literal to document node. so you pass your query declared in a string literal to a graphql
function that returns a DocumentNode
with the correct types defined, allowing inference of the return type from the query string.
your example requires declaring the return type yourself and removing the compile time checks that you could be getting otherwise.
from graphql-hooks.
Hey @sepbot the problem is that graphql-hooks
receives the GQL query as a string so we're unable to support the type. Below is the example of adding support linked above. This has an overloaded function signature where the operation
variable extends DocumentNode
. As we don't support receiving Document Nodes as input we can't support this feature.
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
type GqlFetchResult<TData = any> = {
data?: TData;
errors?: Error[];
};
export function gqlFetch<TData = any, TVariables = Record<string, any>>(
operation: TypedDocumentNode<TData, TVariables>,
variables?: TVariables
): GqlFetchResult<TData>;
export function gqlFetch<TData = any, TVariables = Record<string, any>>(
operation: DocumentNode,
variables?: TVariables
): GqlFetchResult<TData> {
// ...
}
from graphql-hooks.
Upon further consideration, we could have a look at what it'd take to support DocumentNode queries. I'm not entirely sure how to go about it as we always only supported queries, mutations and subscriptions in the form of strings, but this might be a good start: https://github.com/apollographql/graphql-tag.
Ideally we should not depend on anything from the Apollo library though, so I'd see how other implementations how graphql clients generally do it.
from graphql-hooks.
Related Issues (20)
- client.setQueryData not updating the UI
- Npm install failing HOT 2
- How would one mock an error with partial response? HOT 4
- Release pending! HOT 1
- Release pending! HOT 1
- Subscription won't update on variables change
- Release pending! HOT 1
- Release pending!
- Release pending! HOT 1
- Release pending! HOT 1
- Release pending!
- Release pending!
- Type 'GraphQLClient' is not assignable to type 'Client' HOT 3
- Build failure
- Release pending!
- Correct release version for graphql-hooks
- all examples have the wrong version
- Release pending!
- chore(deps-dev): bump lerna from 7.4.2 to 8.1.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 graphql-hooks.