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.

GraphQLResult and Observable<object> incorrect types for API.graphql

See original GitHub issue
Type 'GraphQLResult | Observable<object>' is not assignable to type '{ data: CreateDocumentMutation; }'.
  Type 'GraphQLResult' is not assignable to type '{ data: CreateDocumentMutation; }'.
    Types of property 'data' are incompatible.
      Type 'object | undefined' is not assignable to type 'CreateDocumentMutation'.
        Type 'undefined' is not assignable to type 'CreateDocumentMutation'.  TS2322

Getting the above error using typescript after the new Amplify js version.

It would be better to use Observable<any> in this case because using type object is incompatible. In TypeScript the type object is the opposite of any and is not assignable to anything.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:31
  • Comments:31 (8 by maintainers)

github_iconTop GitHub Comments

60reactions
michalmikolajczykcommented, May 1, 2020

This problem popped up for me during the React Amplify tutorial https://docs.amplify.aws/start/getting-started/data-model/q/integration/react#connect-frontend-to-api

Line 23-24:

const todoData = await API.graphql(graphqlOperation(listTodos))
const todos = todoData.data.listTodos.items

The error is:

Property 'data' does not exist on type 'GraphQLResult<object> | Observable<object>'.
  Property 'data' does not exist on type 'Observable<object>'.ts(2339)

I managed to go around this problem by using this obvious kludge:

const todoData: any = await API.graphql(graphqlOperation(listTodos))

It would be nice with a proper solution though 😉

46reactions
chernandocommented, May 19, 2020

@michalmikolajczyk @selipso as you know using any disables type checking.

Until #5208 or #3748 get through, try something like this:

import { GraphQLResult } from "@aws-amplify/api";
import { ListTodosQuery } from "./api";

// ...

const todoData = (
  await API.graphql(graphqlOperation(listTodos))
) as GraphQLResult<ListTodosQuery>;

const todos = todoData.data.listTodos.items
Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript - AWS-Amplify : Connect API and database to the app
I created my GraphQL and I'm trying to display the result of my Query 4. All seemed to be ok when I added...
Read more >
How to use an AWS Amplify GraphQL API with a React ...
This post will show you how to setup a GraphQL API with ... function return type is from Promise<GraphQLResult> | Observable<object>.
Read more >
GraphQLResult | amplify-js
Type parameters. T. Hierarchy. GraphQLResult ... Properties. Optional data. data: T. Defined in packages/api-graphql/src/types/index.ts:17 ...
Read more >
Apollo GraphQL Part 2 - SwiftUI Advanced Handbook
The API.swift file is auto-generated by Apollo and you should never edit it as it ... LaunchViewModel.swift final class LaunchViewModel: ObservableObject ...
Read more >
AWS-Amplify/Lobby - Gitter
... 'subscribe' does not exist on type 'Promise<GraphQLResult<object>> | Observable<object>'. ... I am working on an app and I am using Graphql as...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found