Giter Club home page Giter Club logo

Comments (3)

phryneas avatar phryneas commented on June 24, 2024

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.

StrikeAgainst avatar StrikeAgainst commented on June 24, 2024

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.

github-actions avatar github-actions commented on June 24, 2024

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)

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.