GithubHelp home page GithubHelp logo

Comments (6)

simoneb avatar simoneb commented on June 2, 2024 1

Just curious, why was this closed?

It was closed by automation.

from graphql-hooks.

luke88jones avatar luke88jones commented on June 2, 2024 1

@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.

machineghost avatar machineghost commented on June 2, 2024

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.

sepbot avatar sepbot commented on June 2, 2024

@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.

luke88jones avatar luke88jones commented on June 2, 2024

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.

simoneb avatar simoneb commented on June 2, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.