Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tighten QueryTuple to indicate observable fields may be absent #5935

Merged
merged 2 commits into from
Feb 15, 2020

Conversation

benmosher
Copy link

@benmosher benmosher commented Feb 11, 2020

fixes #5933

npm t and npm run build both came back clean for me.

Checklist:

  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)

@apollo-cla
Copy link

@benmosher: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@@ -84,13 +85,13 @@ export class QueryData<TData, TVariables> extends OperationData {
queryResult,
lazy = false,
}: {
queryResult: QueryResult<TData, TVariables>;
queryResult: QueryResult<TData, TVariables> | LazyQueryResult<TData, TVariables>;
Copy link
Author

@benmosher benmosher Feb 11, 2020

Choose a reason for hiding this comment

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

I know this is redundant (since LazyQueryResult<D,V> already includes QueryResult<D,V>) but this felt like a reasonable duplication in the interest of clarity (since both lazy and non-lazy paths go through here).

lazy?: boolean;
}) {
this.isMounted = true;

if (!lazy || this.runLazy) {
this.handleErrorOrCompleted(queryResult);
this.handleErrorOrCompleted(queryResult as QueryResult<TData, TVariables>);
Copy link
Author

Choose a reason for hiding this comment

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

!lazy || this.runLazy does properly enforce this type cast at runtime, AFAICT. but TS compiler doesn't know that.

data: undefined;
}

type Impartial<T> = {
Copy link
Author

Choose a reason for hiding this comment

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

named as such as a complement to Partial<T>. I am totally fine with a less cute name if you have one in mind.

export type LazyQueryResult<TData, TVariables> =
| UnexecutedLazyResult
| QueryResult<TData, TVariables>;

export type QueryTuple<TData, TVariables> = [
Copy link
Author

Choose a reason for hiding this comment

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

FWIW: was tempted to rename this to LazyQueryTuple but I didn't want to rock the boat too much.

@@ -82,7 +82,7 @@ export interface QueryResult<TData = any, TVariables = OperationVariables>
error?: ApolloError;
loading: boolean;
networkStatus: NetworkStatus;
called: boolean;
called: true;
Copy link
Author

Choose a reason for hiding this comment

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

assuming this field does not mean anything outside of lazy results, I believe this is correct now, as of the preceding commit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi,

I was wondering why you think this is correct.
By looking at the code I can see that called can definitely be false:

I spotted this strange type declaration because I'm getting an @typescript-eslint/no-unnecessary-condition warning on if (result.called) { ... The warning is correct but the type is not.

In my interpretation called should be false if the lazy initialization is not yet called. This how it seems to be implemented.
Is this perhaps deprecated? Did you intend to override called back to boolean for LazyQueryResult?

Copy link
Member

Choose a reason for hiding this comment

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

The type of called was relaxed to boolean again in v3.5.8, thanks to #9328.

Copy link
Author

Choose a reason for hiding this comment

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

it's been ages, but I think you're right that I meant to have LazyQueryResult set called: boolean. sorry for the bug!

@codecov
Copy link

codecov bot commented Feb 11, 2020

Codecov Report

Merging #5935 into master will not change coverage by %.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #5935   +/-   ##
=======================================
  Coverage   94.59%   94.59%           
=======================================
  Files          91       91           
  Lines        3703     3703           
  Branches      908      875   -33     
=======================================
  Hits         3503     3503           
  Misses        177      177           
  Partials       23       23           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9cd5e8f...73659fc. Read the comment docs.

@benjamn benjamn requested a review from hwillson February 13, 2020 17:16
@benjamn benjamn added this to the Release 3.0 milestone Feb 13, 2020
@hwillson hwillson self-assigned this Feb 14, 2020
Copy link
Member

@hwillson hwillson left a comment

Choose a reason for hiding this comment

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

Thanks very much for working on this @benmosher! This looks great. 👍

@hwillson hwillson merged commit 8ec3672 into apollographql:master Feb 15, 2020
hwillson added a commit that referenced this pull request Feb 15, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unsafe queryData.executeLazy TS type cast to QueryResult can lead to runtime errors
5 participants