[Feedback] `API.graphql` return signature is hard to use in TypeScript
See original GitHub issueIs your feature request related to a problem? Please describe.
When I try to await
an API.graphql
call in TS, I get this:
This is because you have defined the graphql
method to also return an Observable
in case of a subscription:
This is understandable but makes it very annoying to use, particularly because if i need to assert types, the GraphQLResult
type is not also exported for me to use.
Describe the solution you’d like
use overloading and/or a different function for subscriptions to make this experience easier.
ultimately i want to be able to write this:
const query = await API.graphql<TodoType[]>({query: listTodos})
and it should not complain. (a REALLY good experience, ofc, would give me that return type for free based on listTodos
, but that would involve a much deeper redesign)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:31
- Comments:13 (8 by maintainers)
Top Results From Across the Web
GraphQL error handling to the max with Typescript, codegen ...
A GraphQL API will always return a 200 OK Status Code, even in case of error. You'll get a 5xx error in case...
Read more >Integrate TypeScript with GraphQL using TypeGraphQL
Build an API to integrate TypeScript with GraphQL using the TypeGraphQL library, which simplifies creating GraphQL APIs in Node.js.
Read more >Writing query resolvers | Full-Stack Quickstart - Apollo GraphQL
This query returns the details of the Launch object with the id 60 . Instead of hard-coding the argument like the query above,...
Read more >Step by step guide of how to painlessly type GraphQL ...
Tagged with graphql, typescript, react, apollo. ... </p> if (error) return <p>Error :(</p> // Type signature of `data` is: // { // rates: ......
Read more >Maximising the Power of TypeScript with GraphQL - Medium
I mostly use Apollo Client and Apollo Server, and I've just started ... I'll give a brief explanation of how you set up...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
for those finding this… heres how to write a vaguely ergonomic wrapper around this.
usage:
Secondly, specifying the return signature like this:
https://github.com/aws-amplify/amplify-js/blob/b5d14124dfd5f1000f102ac8c84aa964c9d916c5/packages/api/src/API.ts#L173-L178
always assigns the generic type
T
to beobject
because that is how theGraphQLResult
interface is designed. This causes nasty, nasty type errors when people try to assign the results of the graphql query to a properly typed variable:more info on this error here: https://stackoverflow.com/questions/56505560/could-be-instantiated-with-a-different-subtype-of-constraint-object
but TLDR:
i would recommend making
graphql
a generic as well so I can pass along the type i want: