-
Notifications
You must be signed in to change notification settings - Fork 787
Type safe usage of @graphql
in TypeScript projects
#635
Comments
I think that's a great idea. We already do this kind of generic stuff in the I imagine adding something similar to the container would be great! We're also looking for people to help improve the developer experience and correctness of the TypeScript target for https://github.com/apollographql/apollo-codegen, which will enable you to automatically generate the TypeScript definitions for the query! I know @lewisf is going to work on the Flow stuff a bit soon, so you could coordinate :] |
I actually happen to have a WIP PR open on I've also seen that the people behind the const connect = compose<InjectedGraphQLProps<MessageData>, {}>(
graphql(query),
pure
);
const GraphQLMessage = connect(({data}) => {
const message = data!.allMessages && data!.allMessages![0].text;
const isLoading = data!.loading ? 'yes' : 'nope';
return (
<div>
<h2>Message from GraphQL server: <em>{message}</em></h2>
<h2>Currently loading?: {isLoading}</h2>
</div>
);
}); Once again all type safe. The downside is the recompose typings don't allow retuning null so that's why const GraphQLMessage: React.ComponentClass<MessageVars> What I could see being really helpful is documentation on use with TypeScript and examples of common TypeScript patterns. Ideally those principles should apply to flow as well. |
#394 will be a solution to this! I'll be picking it up this week! |
fixed via #695! |
Steps to Reproduce
I'm working on a TypeScript project and would like to use the
graphql
HOC as a decorator if possible. Currently my code looks like this below which I saw is similar to how@graphql
is used in the tests of this library. It works, but it really defeats the purpose of using TypeScript as all type safety is lost.Taking inspiration from
react-redux
I messed around with the declaration ofgraphql.d.ts
until I got this.This then let me write my component as this which is a notable improvement and is fully type safe. The only real downside is a bit of boilerplate and that if the component has no state,
void
and{}
don't work as generic arguments.Expected Behavior
It would be great to see use of the
graphql
decorator as more type safe. I know my current solution is a bit weird, and I'm wondering if anyone else has hacked around with this and came up with another solution?The text was updated successfully, but these errors were encountered: