Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Changed typings of updateQuery's previousQueryResult to be potentially undefined#12276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

Cellule
Copy link
Contributor

Related to#12228

There's an outstanding issue wherepreviousQueryResult sometimes, flakily, ends upundefined
This change aims to have the typings reflect the actual runtime behavior allowing devs to write safe code that can handle the missingpreviousQueryResult
An alternative could be to simply not callmapFn ifpreviousQueryResult is missing. While neither is ideal nor fixes the underlying issue of "why"previousQueryResult isundefined. At least it would avoid bad crashes.

I've been rolling with this patch for years now and I haven't seen any bad side-effects so far. As far as I can tell, the case wherepreviousQueryResult can be safely ignored and is likely going to be called again with actual data when it matters

Typings Update forupdateQuery:

  • Changed typings ofupdateQuery'spreviousQueryResult to be potentially undefined in@apollo/client.
  • UpdatedupdateQuery method insrc/core/ObservableQuery.ts to reflect the new typings.
  • ModifiedUpdateQueryFn type insrc/core/watchQueryOptions.ts to allowpreviousQueryResult to be undefined.
  • UpdatedObservableQueryFields interface insrc/react/types/types.ts to reflect the new typings.

VSCode Configuration:

  • Addededitor.codeActionsOnSave setting to disable organizing imports on save in.vscode/settings.json.

AdrienPoupa, mathieumg, and lin72h reacted with thumbs up emoji
@netlifyNetlify
Copy link

netlifybot commentedJan 15, 2025
edited
Loading

👷 Deploy request forapollo-client-docs pending review.

Visit the deploys page to approve it

NameLink
🔨 Latest commitb817562

@svc-apollo-docs
Copy link

svc-apollo-docs commentedJan 15, 2025
edited
Loading

⚠️ Docs preview not attached to branch

The preview was not built because the PR's base branchrelease-3.13 is not in the list of sources.

An Apollo team member can comment one of the following commands to dictate which branch to attach the preview to:

  • !docs set-base-branch version-2.6
  • !docs set-base-branch main

Build ID: f38bc6ce53417f54a5403a5f

@changeset-botchangeset-bot
Copy link

changeset-botbot commentedJan 15, 2025
edited
Loading

🦋 Changeset detected

Latest commit:b817562

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@apollo/clientMinor

Not sure what this means?Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@jerelmillerjerelmiller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm generally ok with this change, but would you be willing to add a test that demonstrates the case wherepreviousData isundefined? I'd like to make sure its "documented" in some form through runtime behavior, not just the types. That should help us prevent regressions in the future. That would also help determine whether this is an actual bug, or if this behavior was intentional. I'd like to avoid a band-aid to the types just in casepreviousData was never intended to beundefined.

Thanks for the contribution!

@@ -241,6 +241,9 @@ describe("subscribeToMore", () => {
}
`,
updateQuery: (prev, { subscriptionData }) => {
if (!prev) {
Copy link
Member

@jerelmillerjerelmillerJan 17, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I definitely understand why you did this for this test, but unfortunately this makes it more difficult to know if we introduced regressions by accidentally makingprev undefined when it should have a value. The assertion below isn't run in that case, so it would appear that the test would still pass (or at the very least, the source of where the test would fail would be further away from the actual problem).

Instead, I'd recommend updating theprev toprev!, that way this test will crash if this ever switches toundefined.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Make sense, I wasn't 100% sure how to handle it, but you're right the expectation is thatprev should be provided here

Copy link
ContributorAuthor

@CelluleCellule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

would you be willing to add a test that demonstrates the case where previousData is undefined

If you read the issue, the problem is that we can't figure out the repro steps to haveundefined at runtime.
It is likely a race condition that might be hard to reproduce in a testing environment.
I admit I haven't tried recently, this is an issue we've faced years ago and kept the patch.
I can try to get a repro, but I can't make any guarantee

@@ -241,6 +241,9 @@ describe("subscribeToMore", () => {
}
`,
updateQuery: (prev, { subscriptionData }) => {
if (!prev) {
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Make sense, I wasn't 100% sure how to handle it, but you're right the expectation is thatprev should be provided here

@jerelmiller
Copy link
Member

If you read the issue, the problem is that we can't figure out the repro steps to have undefined at runtime.

Ah shoot. I read that and it went right over my head. Let me play around with this a bit more and see if I can think of any way this could happen. I'll push a test if I can, or respond back and let you know that I couldn't come up with something.

@jerelmiller
Copy link
Member

@Cellule the closest I can get to reproducing this isupdateQuery passing an empty object{} asprevious. Just to double check, the value you're seeing on the crashes isundefined and not access on a nested property whenprevious is{} correct?

updateQuery callscache.diff withreturnPartialData: true:

const{ result}=queryManager.cache.diff<TData>({
query:this.options.query,
variables:this.variables,
returnPartialData:true,
optimistic:false,
});

ThatreturnPartialData: true returns an empty object if there is absolutely no cache data available for the given query. I've never actually seenresult returnundefined and have been unable to reproduce a situation where it returnsundefined instead of{}. I've traced much of the code and am not seeing anything stick out either.

Is there anything else you can tell me about the queries where you see this happen, such as directives used, type policies, etc? Perhaps the combination of one of those things hits this case.

@Cellule
Copy link
ContributorAuthor

I admit it's been a long time since I've seen the issue because we've mitigated it by always checkingif(!prev) return prev!
I am fairly certainprev === undefined
I'll try to get in a bad state on my side

jerelmiller reacted with thumbs up emoji

@jerelmiller
Copy link
Member

That would be super helpful if you can. I fear this change is more of a band-aid to the real underlying issue and would love to figure that out if we can. Otherwise this could be a fairly disruptive change to a lot of existing users who could now potentially see a lot of TypeScript errors after upgrading.

@Cellule
Copy link
ContributorAuthor

Well maybe I was wrong, it seems it might be{} after all (so my patch wouldn't fix anything really)
image

In my case I'm usingupdateQuery inquery.subscribeToMore
In order to repro I'm doing some operations that will cause the subscription to send more data then navigate to another page "quickly-ish"
The other page is still on the same entity, so it will effectively

  • change variables inuseQuery
  • ThroughuseEffect it will resubscribe the subscription `
  • updateQuery seems to be called with data from the previous variables for a new query with no data (hence empty object)
{"subscriptionData": {"data": {"messageUpserted": {"message": {"id":9627,"content": {"text":"removed the work order from being on hold.","tokens": [                            {"value":"removed the work order from being on hold.","__typename":"TextToken"                            }                        ],"__typename":"TokenizedText"                    },"author": {"id":1338,"img":"https://img.maintainx-dev.com/mx-dev-cac1-uploads/static/user_placeholders/RandomPicture4.png","firstName":"Michael","lastName":"Ferris","displayName":"Michael Ferris","__typename":"User","alternateImg":null,"availabilityStatus":null                    },"createdAt":"2025-01-22T19:50:41.676Z","reactions": [],"__typename":"SystemWorkOrderStatusChangedMessage","extraData": {"oldStatus":"ON_HOLD","newStatus":"OPEN","newStatusVariant":null,"failures":null,"escalatedUsers": [],"escalatedTeams": [],"__typename":"SystemWorkOrderStatusChangedExtraData"                    }                },"parent": {"id":876,// Note the id of the parent that the subscription subscribes to"isUnreadForMe":false,"comments": {"totalCount":0,"unreadCount":0,"__typename":"MessageThread"                    },"__typename":"WorkOrder"                },"transcription":null,"__typename":"MessageUpsertedSubscription"            }        }    },"variables": {"noRedirect":false,"id":4,// Note the id in the variables do not match with the data from the subscription."pagination": {"cursor":"","limit":25        },"shouldFetchAutomationInformation":false    }}

@jerelmiller
Copy link
Member

jerelmiller commentedJan 22, 2025
edited
Loading

it seems it might be{} after all

Ok great! I was hoping that would be the case and this one actually makes a lot more sense to me. I could reproduce this fairly easily in a test by starting a query, subscribing to the subscription, then having the subscription emit an event before the query finished on the network. In this case, no data had been written to the cache from the query, soprevious would be that empty object.

I think the right change here might be to wrapTData inDeepPartial since its possible that at any given time, you may only get back partial data. It still may ruffle a few feathers, but it would be a more accurate type. Let me converse with@phryneas to make sure we're in alignment, but I'm fairly confident this is the right TypeScript fix. Assuming so, I think it makes most sense to target this change with 3.13 so we can better call it out in the changelog.

As for the other behavior you're seeing, it looks likeupdateQuery is called withthis.variables onObservableQuery, which is just agetter forthis.options.variables. Perhaps this is a race condition between when the options actually change inObservableQuery and when you callsubscribeToMore again? Perhaps you could logobservable.options.variables to see if that value is updated with the new variables.

@Cellule
Copy link
ContributorAuthor

There's still something that bugs me with thevariables
From what I can test, it's possible the variables passed tosubscribeToMore.updateQuery do not match at all the variables passed to the subscription.
It seems entirely possible for the ObservableQuery to have receive new variables, but the subscription is still on the old variables

The scenario seems to be as follows

  • Variables are changed (navigation or whatever)
  • useQuery receives new variables and updates the underlying ObservableQuery
  • useEffect(() => query.subscribeToMore(...), [query.variables]) causes the subscription to be slated to be disconnected
  • Data arrives in the subscription, theupdateQuery is called for the previous variables, but the cache looks for query data using the new variables (leading to{})
  • useEffect unsubscribes the subscription then resubscribes correctly
    It looks like a race condition, but so far it hasn't been hard to reproduce on my side

Real quick it sounds like it would be "safe" to simply not callmapFn inupdateQuery ifresults === {} || results === undefined.
So far I haven't been able to reproduce the variables mismatch when there's a cache hit, but it sounds possible in theory

Another thing to mention, theSubscribeToMoreOptions.updateQuery implies the variables will have typeTSubscriptionVariables but in reality, the variables returned areTVariables (not currently passed in the generics).
This type is simply wrong, while it's unlikely to be used, if the variables of the query and the variable of the subscription differ enough, it can cause bugs. I had to cast it just to confirm the bad state

makeSubscription<IWorkOrderDetailsQueryData,IWoMessageUpsertedSubscriptionSubscription,IWoMessageUpsertedSubscriptionSubscriptionVariables>("messageUpsertedSubscription",query.subscribeToMore,{document:messageUpsertedSubscription,variables:{parent:{id:workOrderId,type:IUserMessageParentType.WorkOrder},},updateQuery(prev,{ subscriptionData, variables}){constnewMessage=subscriptionData?.data?.messageUpserted?.message;constworkOrderUpdates=subscriptionData?.data?.messageUpserted?.parent;consttranscription=subscriptionData.data.messageUpserted?.transcription;if((variablesasanyasIWorkOrderDetailsQueryVariables).id!==(workOrderUpdatesasany).id){debugger;returnprev;}if(!newMessage||workOrderUpdates?.__typename!=="WorkOrder"||!prev?.workOrder){returnprev!;}

@Cellule
Copy link
ContributorAuthor

One issue I see with usingDeepPartial is that it will then become a typings nightmare to properly return the correct object

image

I wonder if a better alternative is to actually only update the query if it is complete
I had that idea in the beginning because in my opinion, if for whatever reason the previous data is missing or incomplete, I will want to no-op the update.
It does make a difference in the runtime and could potentially affect some legit use case (although I can't think of one) I feel it might be the better/safer approach.
Likely, if you have a good reason to want to write in the cache of an incomplete query, you're more likely to callcache.writeQuery manually than rely onupdateQuery

publicupdateQuery<TVarsextendsOperationVariables=TVariables>(mapFn:(previousQueryResult:Unmasked<TData>,options:Pick<WatchQueryOptions<TVars,TData>,"variables">)=>Unmasked<TData>|undefined):void{const{queryManager}= this;const{result,complete}=queryManager.cache.diff<Unmasked<TData>>({query:this.options.query,variables:this.variables,returnPartialData:true,optimistic:false,});if(complete&&result){constnewResult=mapFn(result,{variables:(thisasany).variables,});if(newResult){queryManager.cache.writeQuery({query:this.options.query,data:newResult,variables:this.variables,});queryManager.broadcastQueries();}}}

@jerelmiller
Copy link
Member

I go back and forth on this. This sounds like a reasonable change, but I fear its too much of a breaking change for a patch/minor release. Since we don't provide any additional information in the callback (i.e. whether the previous result is complete), there could be cases where suddenly your callback is no longer called and its difficult to tell why.

That said, the callback does take anoptions argument as the 2nd argument. Perhaps we can provide thecomplete flag here and let the user decide whether to use the partial data to return a full result or to ignore the update (i.e. returnundefined). At least this gives the best of both worlds and puts it into the user's hands on how to handle it.

Looking at the return type ofupdateQuery, currently its set toUnmasked<TData>, but it does appear the implementation isok returning a falsey value and will do nothing if thats the case. We should probably reflect this in the types as well by updating the return type toUnmasked<TData> | undefined so that TypeScript allows this.

The tricky part would be updating the TypeScript type here. It would be ideal if it were something like this:

updateQuery(previous,{ complete}){if(complete){previous;// ^? TData}previous;// ^? DeepPartial<TData>}

Thoughts on this?

@Cellule
Copy link
ContributorAuthor

I go back and forth on this. This sounds like a reasonable change, but I fear its too much of a breaking change for a patch/minor release. Since we don't provide any additional information in the callback (i.e. whether the previous result is complete), there could be cases where suddenly your callback is no longer called and its difficult to tell why.

Yeah this is exactly why I'm on the fence with the change as well

That said, the callback does take anoptions argument as the 2nd argument. Perhaps we can provide thecomplete flag here and let the user decide whether to use the partial data to return a full result or to ignore the update (i.e. returnundefined). At least this gives the best of both worlds and puts it into the user's hands on how to handle it.

Humm yeah that could work, but indeed the typings would be the tricky part

Looking at the return type ofupdateQuery, currently its set toUnmasked<TData>, but it does appear the implementation isok returning a falsey value and will do nothing if thats the case. We should probably reflect this in the types as well by updating the return type toUnmasked<TData> | undefined so that TypeScript allows this.

Right, I did want to update at least the return type which is more accurate and backward compatible.
This is why I have to do this in my code currentlyupdateQuery(prev) { if (!prev) return prev!; ... }

The tricky part would be updating the TypeScript type here. It would be ideal if it were something like this:

updateQuery(previous,{ complete}){if(complete){previous;// ^? TData}previous;// ^? DeepPartial<TData>}

Thoughts on this?

Keeping backward compatibility and typescript relation between the 2 variables sounds like a challenge!
I'll try to give it some thoughts, but whatever solution it is likely to cause friction after the update, which I think is unavoidable

@jerelmiller
Copy link
Member

whatever solution it is likely to cause friction after the update, which I think is unavoidable

Definitely unfortunate, but I'd wager its more preferable than a crash in production!

@CelluleCellule changed the base branch frommain torelease-3.13January 27, 2025 20:57
@pkg-pr-newpkg.pr.new
Copy link

pkg-pr-newbot commentedJan 27, 2025
edited
Loading

npm i https://pkg.pr.new/@apollo/client@12276

commit:b817562

@Cellule
Copy link
ContributorAuthor

Alright here's my attempt

This shows my reasoning best I think

constupdateQuery:Parameters<typeofobservable.updateQuery>[0]=jest.fn((previousResult)=>{expect(previousResult.complete).toBe(true);// Type guardif(!previousResult.complete){returnundefined;}return{user:{ ...previousResult.user,name:"User (updated)"}};});

There's no way I can think of to bind the type of a parameter with a type guard on another parameter.
The details can be tweaked, but my idea is to attach acomplete property straight on the result. Obviously this doesn't work as-is since the result could have acomplete property.
In the callback, if you don't check forprev.complete the type will bePartial<Unmasked<TData>> (I gave up onDeepPartial because it was giving me typescript errors due to type being too complex)

I cleaned up a bunch of types that we're not shared where it should
I fixed the typings forvariables in the options to beTVariables and added a newsubscriptionVariables: TSubscriptionVariables in the subscribeToMore case

Let me know what you think

@jerelmiller
Copy link
Member

This is turning out to be a much more difficult change than expected 🙃.

Unfortunately adding acomplete field to the result itself is a no-go. We want to avoid doing anything withdata since it could conflict with the fields returned from the GraphQL result. We do our best to avoid that at all costs.

Unfortunately changing that first argument in any capacity is a breaking change. That said, how urgent is this change for you? We have started working on v4 and we could certainly think about moving this change there. I'd be open to redesigning theupdateQuery API to be more type-safe and v4 would give us a chance to make any necessary breaking changes to facilitate this.

Thoughts?

>();
}

return undefined;
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Fair, I considered it but wasn't sure if we preferred it

subscriptionData: { data: Unmasked<TSubscriptionData> };
variables?: TSubscriptionVariables;
subscriptionVariables: TSubscriptionVariables | undefined;
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Sure I can remove this part

jerelmiller reacted with thumbs up emoji
Comment on lines 201 to 203
/**
* @deprecated Use `options.previousQueryResult` instead.
*/
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thanks!

* Might have partial or missing data.
*/
complete: false;
previousQueryResult: DeepPartial<Unmasked<TData>> | undefined;
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Done

jerelmiller reacted with hooray emoji
* @deprecated Use `options.previousQueryResult` instead.
*/
previousQueryResult: Unmasked<TData>,
options: TOptions & UpdateQueryOptions<TData, TVariables>
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Done

jerelmiller reacted with hooray emoji
@@ -538,6 +537,7 @@ Did you mean to call refetch(variables) instead of refetch({ variables })?`,
optimistic: false,
},
(previous) =>
// REVIEW: Code smell here with the `!` and cast, is it possible for previous to be null or have partial data ?
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@jerelmiller what do you think about this?
Do you want to keep the comment or remove it and address this separately?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's address separately since it was already there. Feel free to remove this for now.cache.updateQuery might be a whole other animal.

const { queryManager } = this;
const { result} = queryManager.cache.diff<TData>({
const { result, complete} = queryManager.cache.diff<Unmasked<TData>>({
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This type change also affects the first argument of the mapFn which was being cast toUnmasked<TData>
In essence I just moved the cast from 1 place to another
If I change that back toTData then I'll have to put backresult! as Unmasked<TData> below

Comment on lines 735 to 751
const variables = (this as any).variables;
let updateOptions: UpdateQueryOptions<TData, TVariables>;
if (complete && result) {
updateOptions = {
variables,
complete: true,
previousQueryResult: result,
};
} else {
updateOptions = {
variables,
complete: false,
previousQueryResult: result as DeepPartial<Unmasked<TData>>,
};
}

const newResult = mapFn(result!, updateOptions);
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, I was really trying to avoid the typecast, but it's true that the runtime is actually the same in both branches

jerelmiller reacted with thumbs up emoji
@@ -164,31 +164,98 @@ export interface FetchMoreQueryOptions<TVariables, TData = any> {
context?: DefaultContext;
}

export type UpdateQueryFn<
export interface UpdateQueryFn<
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm confused by my changes at this point, will push the other fixes and review this

jerelmiller reacted with thumbs up emoji
return internalQueryRef.observable.subscribeToMore(options);
return internalQueryRef.observable.subscribeToMore(
// TODO: The internalQueryRef doesn't have TVariables' type information so we have to cast it here
options as any as SubscribeToMoreOptions<TData, OperationVariables>
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think you are right about the other call sites, but here there seems to be an incompatibility due to TSubscriptionData vs TData

Conversion of type 'SubscribeToMoreOptions<TData, TSubscriptionVariables, TSubscriptionData, TVariables>' to type 'SubscribeToMoreOptions<TData, OperationVariables, TData, OperationVariables>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.  Type 'TSubscriptionData' is not comparable to type 'TData'.    'TData' could be instantiated with an arbitrary type which could be unrelated to 'TSubscriptionData'.ts(2352)

@Cellule
Copy link
ContributorAuthor

Alright I think it mostly looks good.
If you think there are other things that needs to be fixed I won't mind if you decide to take care of it yourself. I will likely not have more time before next week as I'm working on a presentation for Fragment Masking to my company this Friday :)

@jerelmiller
Copy link
Member

Sounds great! Really appreciate the back and forth. I'll make sure this change makes it into the 3.13 beta 🙂.

Hope the presentation goes well! Feel free to ping me on Discord (@jerelmiller) if you've got questions about data masking 🙂. I'd love to answer them!

@jerelmillerjerelmiller merged commit670f112 intoapollographql:release-3.13Feb 6, 2025
39 of 41 checks passed
@github-actionsgithub-actionsbot mentioned this pull requestFeb 6, 2025
@github-actionsgithub-actionsbot mentioned this pull requestFeb 13, 2025
@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsMar 9, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@jerelmillerjerelmillerjerelmiller approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@Cellule@svc-apollo-docs@jerelmiller

[8]ページ先頭

©2009-2025 Movatter.jp