Property 'subscribe' does not exist on type 'Promise<GraphQLResult<object>>'. TS2339

See original GitHub issue

Describe the bug

Property 'subscribe' does not exist on type 'Promise<GraphQLResult<object>> | Observable<object>'.
  Property 'subscribe' does not exist on type 'Promise<GraphQLResult<object>>'.  TS2339

    91 |       console.log('start subscription to sensors');
    92 |
  > 93 |       const subscriber = API.graphql(graphqlOperation(onCreateSensorValues)).subscribe({
       |                                                                              ^
    94 |         next: (response: ISensorsSubscriptionResponse) => {
    95 |
    96 |           //update the sensor's status in state

To Reproduce Steps to reproduce the behavior: FYI, I’m running this sample, Everything worked just fine until I upgraded aws-amplify to 3.x.

Expected behavior A clear and concise description of what you expected to happen.

What is Configured?

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
    Memory: 142.06 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 12.18.4 - /usr/local/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 6.14.11 - ~/n/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Chrome: 88.0.4324.96
    Safari: 14.0.1
  npmPackages:
    @aws-amplify/ui-react: ^0.2.34 => 0.2.34 
    @babel/cli: ^7.10.1 => 7.10.1 
    @babel/core: ^7.10.2 => 7.10.2 
    @babel/preset-env: ^7.10.2 => 7.10.2 
    @material-ui/core: ^4.8.3 => 4.10.1 
    @material-ui/icons: ^4.5.1 => 4.9.1 
    @testing-library/jest-dom: ^4.2.4 => 4.2.4 
    @testing-library/react: ^9.4.0 => 9.5.0 
    @testing-library/user-event: ^7.2.1 => 7.2.1 
    @types/chart.js: ^2.9.11 => 2.9.21 
    @types/jest: ^24.0.25 => 24.9.1 
    @types/node: ^12.12.24 => 12.12.47 
    @types/react: ^16.9.17 => 16.9.36 
    @types/react-chartjs-2: ^2.5.7 => 2.5.7 
    @types/react-dom: ^16.9.4 => 16.9.8 
    @types/react-map-gl: ^5.1.0 => 5.2.4 
    @types/react-router-dom: ^5.1.3 => 5.1.5 
    aws-amplify: ^3.3.14 => 3.3.14 
    aws-amplify-react: ^3.1.6 => 3.1.9 
    chart.js: ^2.9.3 => 2.9.3 
    react: ^16.12.0 => 16.13.1 
    react-chartjs-2: ^2.9.0 => 2.9.0 
    react-dom: ^16.12.0 => 16.13.1 
    react-map-gl: ^5.2.1 => 5.2.7 
    react-router-dom: ^5.1.2 => 5.2.0 
    react-scripts: ^3.3.0 => 3.4.1 
    typescript: ^3.7.4 => 3.9.5 
  npmGlobalPackages:
    @aws-amplify/cli: 4.41.2
    @nestjs/cli: 7.5.1
    @snow-tree/camera-probe: 7.0.13
    appcenter-cli: 2.7.3
    claudia: 5.12.0
    firebase-tools: 8.16.2
    ios-deploy: 1.11.3
    iot-solutions: 3.1.1
    npm: 6.14.11
    serve: 11.3.2
    snyk: 1.437.3

Additional context #5447

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:13
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
manueliglesiascommented, Aug 8, 2022

Hi,

The API.graphql return types were improved by #9197 , the PR has some information on how to use it, but in summary, there are two new types you can import and pass to hint the compiler of your intent (GraphQLQuery and GraphQLSubscription)

import { GraphQLSubscription, GraphQLQuery } from "@aws-amplify/api";

const res = await API.graphql<GraphQLQuery<MyType>>({ query: 'query' }); // GraphQLResult<MyType>
const res2 = API.graphql<GraphQLSubscription<MyType>>({ query: 'query' }); // Observable<GraphQLResult<MyType>>
5reactions
rafaelfariacommented, May 7, 2022

Any updates on this? The casting used to work (on older applications) however, now I get

Conversion of type 'Promise<GraphQLResult<any>> | Observable<object>' to type 'Observable<object>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
  Type 'Observable<object>' is missing the following properties from type 'Observable<object>': concat, [Symbol.observable]

How do we get around it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular - Property 'subscribe' does not exist on type 'Promise ...
As the result from API.graphql() can be both, a Promise when you query data ... It's a workaround, but you can convert Observable...
Read more >
Angular 2 Property subscribe does not exist on type Promise
Best Solution. subscribe method is for 'Observable', so you can just return an Observable Object. then do what you want to do like ......
Read more >
How to use an AWS Amplify GraphQL API with a React ...
Without the @ts-ignore the TypeScript compiler would complain that subscribe does not exist on type Promise<GraphQLResult> | Observable<object> ...
Read more >
[ts] Property 'subscribe' does not exist on type '(res: any) => any'
Hi guys, I'm working on a http provider. The issue is with the subscribe module after mapping the json response. Here is the...
Read more >
[Solved]-Property 'subscribe' does not exist on type 'Promise ...
[Solved]-Property 'subscribe' does not exist on type 'Promise<Observable<any>>'-angular.js ... async functions don't return promises, you are trying to subscribe ...
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