question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feedback] `API.graphql` return signature is hard to use in TypeScript

See original GitHub issue

Is your feature request related to a problem? Please describe.

When I try to await an API.graphql call in TS, I get this:

image

This is because you have defined the graphql method to also return an Observable in case of a subscription:

https://github.com/aws-amplify/amplify-js/blob/b5d14124dfd5f1000f102ac8c84aa964c9d916c5/packages/api/src/API.ts#L173-L178

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:open
  • Created 3 years ago
  • Reactions:31
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
sw-yxcommented, Jul 20, 2020

for those finding this… heres how to write a vaguely ergonomic wrapper around this.

import { GraphQLOptions} from '@aws-amplify/api-graphql'
async function gql<T extends object>(options: GraphQLOptions, additionalHeaders?: {
  [key: string]: string;
}): Promise<GraphQLResult<T>> {
  const query = API.graphql(options, additionalHeaders) as Promise<GraphQLResult<T>>
  return query
}

usage:

  const [todos, setTodos] = React.useState<TodoType[]>([])
  React.useEffect(() => {
    const query = gql<TodoType[]>({query: listTodos})
    query.then(({data}) => setTodos(data || []))
  })
7reactions
sw-yxcommented, Jul 20, 2020

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 be object because that is how the GraphQLResult 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:

image

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:

// pseudocode, modified from source slightly
	graphql<T = object>(
		options: GraphQLOptions,
		additionalHeaders?: { [key: string]: string }
	): Promise<GraphQLResult<T>> | Observable<object> {
		return this._graphqlApi.graphql(options, additionalHeaders);
	}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found