You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
User wants to set variables or perform another side effect before the element first subscribes
Example
import{authClient}from'../my-auth-client';// some JS SDK for user authclassProductElementextendsApolloQuery<typeofProductQuery>{query=ProductQuery;overrideshouldSubscribe(): boolean{const{ searchParams }=newURL(location.href);// we could abuse `shouldSubscribe` to inject `this.variables = { ... }` herereturn!!searchParams.has('product');}overrideasyncbeforeSubscribe(): Promise<void>{// but instead, it's better to make our side-effecting hook explicit// plus which, this can be asyncconst{ searchParams }=newURL(location.href);this.variables={input: {productId: searchParams.get('product'),apiKey: awaitauthClient.getUser().apiKey,},};}}
Proposed implementation
Add an optional member to ApolloQueryInterface and ApolloSubscriptionInterface
asyncbeforeSubscribe()?: Promise<void>;
and wait on it in ApolloQueryMixin and ApolloSubscriptionMixin:
ApolloQuery#subscribe is currently synchronous and returns a subscription. Is this a breaking change requiring a major version? (ApolloSubscription#subscribe doesn't return anything so that's not an issue there)
Should beforeSubscribe take any parameters? if so, which? for class components, everything's available on this, but for haunted and hybrids, maybe it should take variables?
The text was updated successfully, but these errors were encountered:
I can return a subscription from subscribe() which first converts the promise from beforeSubscribe() to an Observable, then when it resolves, creates the ObservableQuery instance, but existing code expects observableQuery to already exist
I can await the promise in the private documentChanged and variablesChanged methods, but that's awkward too, as many of my tests (and, it stands to reason, real world code) relies on the observableQuery being synchronously created.
ObservableQuery has an onSubscribe method, but it's private. an Ideal solution here would be to hook in to ObservableQuery
Case
User wants to set variables or perform another side effect before the element first subscribes
Example
Proposed implementation
Add an optional member to
ApolloQueryInterface
andApolloSubscriptionInterface
and wait on it in
ApolloQueryMixin
andApolloSubscriptionMixin
:+ await this.beforeSubscribe?.(); this.observableQuery = this.watchQuery(options);
+ await this.beforeSubscribe?.(); this.observableSubscription = this.observable?.subscribe({
Questions
ApolloQuery#subscribe
is currently synchronous and returns a subscription. Is this a breaking change requiring a major version? (ApolloSubscription#subscribe
doesn't return anything so that's not an issue there)beforeSubscribe
take any parameters? if so, which? for class components, everything's available onthis
, but for haunted and hybrids, maybe it should takevariables
?The text was updated successfully, but these errors were encountered: