dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.79k stars 1.32k forks source link

rxjs map operator does not infer type #158

Closed darkbasic closed 6 years ago

darkbasic commented 6 years ago

map_does_not_infer_type

Test code: https://github.com/darkbasic/angularcli-graphql /src/app/apollo/apollo.component.ts

dotansimha commented 6 years ago

Did you try to use ApolloQueryObservable<..> instead of Observable<ApolloQueryResult<...>>?

Also, if it does not work, I would expect the result.data.allMembers to be marked as syntax error...

@darkbasic

darkbasic commented 6 years ago

Did you try to use ApolloQueryObservable<..> instead of Observable<ApolloQueryResult<...>>?

No, because I'm using apollo-client 2.0 and AFAIK I am supposed to use Observable<ApolloQueryResult<...>> with latest version:

export class QueryRef<T> {
  public valueChanges: Observable<ApolloQueryResult<T>>;

Also, if it does not work, I would expect the result.data.allMembers to be marked as syntax error...

Webstorm has several ways to infer types (it also looks for the text string to see if it has been used elsewhere, etc..), but if you CTRL + mouse hover on result it clearly shows that it's of type any:

map_does_not_infer_type_2

dotansimha commented 6 years ago

@darkbasic maybe it's because of the map usage? The signature of map in rxjs is:

export function map<T, R>(this: Observable<T>, project: (value: T, index: number) => R, thisArg?: any): Observable<R> {
  return higherOrderMap(project, thisArg)(this);
}

Did you try to add the output type as generic for map?

darkbasic commented 6 years ago

Did you try to add the output type as generic for map?

If you mean something like

const a = query.valueChanges.map<ApolloQueryResult<AllMembers.Query>, AllMembers.AllMembers[]>(result => result.data.allMembers);

it indeed works, but why should I need to specify something like that? It should be able to automatically infer it, in fact it already knows that query.valueChanges returns Observable<ApolloQueryResult<AllMembers.Query>>:

map_does_not_infer_type_3

With apollo-client 1.x it already worked that way.

darkbasic commented 6 years ago

map_does_not_infer_type_5

map_does_not_infer_type_4

Uhm... that's strange, maybe it's a bug in Webstorm?

darkbasic commented 6 years ago

https://youtrack.jetbrains.com/issue/WEB-29502

If you use Webstorm please vote for this issue.

dotansimha commented 6 years ago

@darkbasic , it makes sense. Also, which version of typescript do you use? And which version is in use in WebStorm settings?

darkbasic commented 6 years ago

2.4.2

dotansimha commented 6 years ago

@darkbasic same for me, with 2.5/2.6. I guess that it's a WebStorm issue. Do you think we can close the issue?

darkbasic commented 6 years ago

Yes I think so. Thanks.