Giter Club home page Giter Club logo

Comments (7)

PowerKiKi avatar PowerKiKi commented on June 7, 2024 4

For your use-case I think it would be possible to write your own function that wraps everything you need. Probably something similar to:

function apolloToSignal<T, Q extends keyof T, U>(
    query : Observable<ApolloQueryResult<T>, 
    queryName: Q, 
    initialValue: U
): Signal<T[Q] | U>

It's only pseudo-code, but it should be possible to make it work.

For the time being I'd probably avoid introducing signal-related things in this package, unless we have a clear and strong need for it.

from apollo-angular.

JesseZomer avatar JesseZomer commented on June 7, 2024

Hello,

I agree that using observables in the library seems like the best plan. I do however have some questions about using apollo-angular together with signals. There are a few things that I'm running into that make it harder to work with than I want it to be, hopefully you have some ideas how to remedy these.

When I want to consume the query results in my component I use the provided toSignal from Angular. I don't want to check for null values everywhere, so I would like to use { requireSync: true } with an initialValue.

I tried using useInitialLoading: true but that isn't sync. That would also give me back data: Data | null which isn't really what I want.

Now I solve it by adding a startWith({data: initialData, loading: true, errors: undefined }) to the pipe. Before that I have a map operator that removes the queryname property from the data field

        map((res: ApolloQueryResult<T>) => ({
            ...res,
            data: res.data.queryname
        }))

because having that extra property only gets in the way (especially in the types, because its dynamic). But that's probably there to accommodate the __typename field I guess?

But having to do this for every query is a bit cumbersome. I would love for an initialValue option in (Watch)QueryOptions that would do the work of the startWith operator above.

Now it might seem a bit weird to have initial data when loading is true, but when you create a signal from your query, I would prefer to not have to null check every computed that is derived from that data. eg:

 query = toSignal(...);
 multiplied = computed(() => query().data.selection * 2

instead of

 query = toSignal(...)
 multiplied = computed(() => (query().data?.selection ?? 1) * 2)

Got any tips op workarounds for how best deal with these situations?

from apollo-angular.

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.