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

Improved type definitions for the observable query #3140

Merged
merged 8 commits into from
Jun 1, 2018

Conversation

excitement-engineer
Copy link
Contributor

The Query component in react-apollo improved upon the typescript definitions provided by apollo-client.

I am now porting these improvements back to apollo-client so that all users can benefit from these improvements and so that react-apollo can simply import the type definitions directly from apollo-client.

This PR makes introduce generic types for the data TData and variables TVariables in the ObservableQuery class.

@apollo-cla
Copy link

apollo-cla commented Mar 10, 2018

Warnings
⚠️

There are library changes, but not tests. That's OK as long as you're refactoring existing code

Generated by 🚫 dangerJS

@@ -421,17 +430,14 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
*
*/
public setVariables(
variables: any,
variables?: TVariables,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question: Is this supposed to be nullable. The type was any before so anything was allowed. If I make the variables nonnull here then I get typescript errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, I don't the the intention is for it to be nullable here. What issues did you run into?

fetchMoreResult?: { [key: string]: any };
variables: { [key: string]: any };
fetchMoreResult?: TData;
variables?: TVariables;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Question: Is this supposed to be nullable? It wasn't before but typescript is giving me errors if I make this nonnull.

Copy link
Contributor

Choose a reason for hiding this comment

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

@excitement-engineer what errors did you get?

Copy link
Contributor

Choose a reason for hiding this comment

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

but, yes variables can be null here

@excitement-engineer
Copy link
Contributor Author

@jbaxleyiii @leoasis

Copy link
Contributor

@jbaxleyiii jbaxleyiii left a comment

Choose a reason for hiding this comment

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

@excitement-engineer lets figure out the setVariables bit, but otherwise YAYAYAYA

fetchMoreResult?: { [key: string]: any };
variables: { [key: string]: any };
fetchMoreResult?: TData;
variables?: TVariables;
Copy link
Contributor

Choose a reason for hiding this comment

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

@excitement-engineer what errors did you get?

fetchMoreResult?: { [key: string]: any };
variables: { [key: string]: any };
fetchMoreResult?: TData;
variables?: TVariables;
Copy link
Contributor

Choose a reason for hiding this comment

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

but, yes variables can be null here

@@ -421,17 +430,14 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
*
*/
public setVariables(
variables: any,
variables?: TVariables,
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, I don't the the intention is for it to be nullable here. What issues did you run into?

@jbaxleyiii
Copy link
Contributor

oh @excitement-engineer could you add a changelog here as well? Thanks!

@@ -982,4 +982,5 @@ describe('getDirectivesFromDocument', () => {
const doc = getDirectivesFromDocument([{ name: 'client' }], query, true);
expect(print(doc)).toBe(print(expected));
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was missing in apollo-client. This was causing the prettier pre-commit hook to fail...

tryFetch: boolean = false,
fetchResults = true,
): Promise<ApolloQueryResult<T>> {
): Promise<ApolloQueryResult<TData>> {
// since setVariables restarts the subscription, we reset the tornDown status
this.isTornDown = false;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the line below here we are doing:

const newVariables = variables ? variables : this.variables;

So I am wondering if it isn't possible to make the type variables?: TVariables?

@@ -399,7 +408,7 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
false;

return this.setVariables(
this.options.variables,
this.options.variables as TVariables,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the issue I ran into with setVariables having a non-null TVariables type. this.options.variables is nullable here so I had to make an explicit cast to get typescript to work

@hwillson hwillson self-assigned this Jun 1, 2018
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 for all of your work on this @excitement-engineer! We should be all set here, so LGTM!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants