Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Ionic application, we are integrating AWS-Amplify and we can able to insert and fetch the data. But while implementing subscription we are getting compiler error says

Property 'subscribe' does not exist on type 'Promise | Observable'. Property 'subscribe' does not exist on type 'Promise'.

In Subscription.ts

export const onCreateEmployee = `subscription OnCreateEmployee(
  $employee_id: Int
  $employee_name: String
) {
  onCreateEmployee(
    employee_id: $employee_id
    employee_name: $employee_name
  ) {
    employee_id
    employee_name
  }
}

In Main.ts

const subscription = API.graphql(
      graphqlOperation(onCreateEmployee)
    ).subscribe((eventData) => { console.log(eventData)
    });

In main.ts, it shows the error like cannot subscribe ..... (see the above error).

Can anybody know how to resolve this?

Thanks

like image 912
AnKr Avatar asked Apr 20 '26 14:04

AnKr


1 Answers

As the result from API.graphql() can be both, a Promise when you query data for immediate return and alternatively an Observable for subscriptions, you need to tell TypeScript what to expect:

(API.graphql(
  graphqlOperation(onCreateEmployee)
) as unknown as Observable<YourEventDataType>)
.subscribe(
  (eventData) => { console.log(eventData) }
);
like image 96
romor Avatar answered Apr 22 '26 11:04

romor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!