Intended outcome:
When trying the typescript hero example in the apollo docs, props passed through from apollo-react's graphql HOC approach should pick up loading, error and hero props with accurate types.
Actual outcome:
My setup returns the typescript issue with:
[ts] Type 'DataValue<Response, OperationVariables> | undefined' has no property 'loading' and no string index signature.
and similar messages for error and hero
How to reproduce the issue:
Using base create-react-app-typescript application (). Then adding necessary apollo/graphql modules: yarn add graphql graphql-tag react-apollo apollo-client
Here is the code:
import gql from "graphql-tag";
import * as React from "react";
import { graphql } from "react-apollo";
const HERO_QUERY = gql`
query GetCharacter() {
hero() {
name
id
friends {
name
id
appearsIn
}
}
}
`;
type Hero = {
name: string;
id: string;
appearsIn: string[];
friends: Hero[];
};
type Response = {
hero: Hero;
};
const withCharacter = graphql<{}, Response>(HERO_QUERY);
export default withCharacter(({ data: { loading, hero, error } }) => {
if(loading) {
return <div>Loading</div>;
}
if(error) {
return <h1>ERROR</h1>;
}
return <div/>;
});
Version
I figured out a work around to make my IDE (both Sublime Text 3 and VSCode) to stop flagging this error by removing "strictNullChecks": true from my tsconfig.json file.
Not sure why it does this, but at least my IDE is picking up the types correctly again.
Having the same issue as @dustinlakin . Getting the type error [ts] Type 'DataValue<Response, Variables> | undefined' has no property 'loading' and no string index signature.
Version
ββ [email protected]
ββ [email protected]
ββ [email protected]
ββ [email protected]
ββ [email protected]
ββ [email protected]
ββ [email protected]
ββ [email protected]
In the same boat. Interested to know if anyone has resolved this. On [email protected]
Anyone find a way to deal with this?
My solution right now is to provide defaults for each parameter which is somewhat cumbersome:
const SomeComponent = withQuery(({ data: { loading = true, error = null, value = null } = {} }) => {
// ....
})
This will compile even in strict mode. Seems like this should be unnecessary though.
Same problem, any idea how to fix it?
the only solution i found so far is to add "strictNullChecks": false to .tsconfig as suggested earlier, not a good fix, but at least it works
Take a look at how UserChildProps is used in my example
import React, { Component } from "react";
import gql from "graphql-tag";
import { graphql, ChildDataProps } from "react-apollo";
type Response = {
users: [User];
};
type Variables = {
q: string;
};
type InputProps = {
q: string;
};
type UserChildProps = ChildDataProps<InputProps, Response, Variables>;
class UsersResult extends Component<UserChildProps, { }> {
render() {
const { loading, error, users } = this.props.data;
if (loading) {
return <h4>Loading...</h4>;
}
if (error) {
return <h4>{error.message}</h4>;
}
return <div />;
}
}
const UserSearch = gql`
query UserSearch {
user($q: String!) @rest(type: "User", path: "/users?{args.q}&{context.apiKey}") {
id,
city,
company,
country,
created_on,
display_name,
fields,
first_name,
images,
last_name,
occupation,
state,
url,
username
}
}
`;
type User = {
id: string;
city: string;
company: string;
country: string;
created_on: Date;
display_name: string;
fields: string[];
first_name: string;
images: {
[key: string]: string;
};
last_name: string;
occupation: string;
state: string;
url: string;
username: string;
};
const UsersResultQuery = graphql<InputProps, Response, Variables, UserChildProps>(UserSearch, {
options: ({ q }) => {
return { variables: { q } };
}
})(UsersResult);
I have the same problem. So I went for
Hi guys, try casting operator: as
export default withCharacter(({ data: qData }) => {
if (qData && qData.loading) return <div>Loading</div>;
if (qData && qData.error) return <h1>ERROR</h1>;
return (
<div className="App">
{qData && (qData as any).hero.map((n: SomeType, i: Int) => {})}
</div>
);
});
try it, the way around which without disable strictNullChecks in tscnofig:
export default withCharacter(({ data }) => {
const { loading, error, hero } = data!;
if (loading) return <div>Loading</div>;
if (error) return <h1>ERROR</h1>;
// process hero
return <div />;
});
Thanks @taozhi8833998
If anyone is interested in working on a PR for this, we'll definitely review it. Thanks!
Most helpful comment
try it, the way around which without disable
strictNullChecksin tscnofig: