aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.41k stars 2.12k forks source link

Subscription Typescript Error #5447

Closed karrettgelley closed 4 years ago

karrettgelley commented 4 years ago

Describe the bug When I try to create a subscription I get the following error Property 'subscribe' does not exist on type 'Promise<GraphQLResult<object>>'.ts(2339).

To Reproduce

  1. Create a blank amplify react app
  2. Create a subscription: ` useEffect(() => { let userSubscription: any; try { userSubscription = API.graphql( graphqlOperation(onUpdateUser, { id: userId }), ).subscribe({ next: async (value: { data: OnUpdateUserSubscription }) => { return; }, }); } catch (e) { setError(true); console.log('sub error', e); }

    return () => { if (userSubscription) { userSubscription.unsubscribe(); } }; } }, [ userId, ]);`

Expected behavior Subscription should work as in the subscription example in the amplify docs.

Additional context I have been trying to convert my react app to ts recently and I haven't been able to properly convert my subscriptions. I can't find any errors specific to amplify, and the stackoverflow answers do not seem to solve this problem which makes me think this is a bug specific to amplify.

humphreylitan commented 4 years ago

I'm also experiencing this same issue. I couldn't seem to find the solution in the internet. I've seen some but they didn't work. 😪

saevarb commented 4 years ago

I would highly recommend you use the DataStore API instead of trying to use the generated types and queries. They are nearly impossible to work with in any generic way, and the DataStore stuff automatically generates proper models for all your schemas and lets you do crud operations much more easily.

karrettgelley commented 4 years ago

@saevarb DataStore is, in my opinion, not production ready and very expensive (see #5119 ). It is still undergoing large changes and I can't ship with that kind of volatility. Also it operates under a completely different paradigm. Long term I would love to use it, but not now. Thanks for your suggestion though

For now I just ignored the error with // ts-ignore. The problem might be as simple as updating the ts type for API.graphql(), as it doesn't break my code by adding the ignore.

saevarb commented 4 years ago

@karrettgelley Thank you very much for bringing that issue to my attention! I was not aware of this.

I have been perusing the thread and linked threads, and I just need to make sure I'm not misunderstanding this: Is DataStore syncing the entire database, with no regard for auth, so that I have other users' data on every other client? Because I am having a hard time believing that someone would actually implement that.

As for volatility and such, I can definitely understand and relate to your concerns.

However, as someone who spent a not insignificant amount of time working with the types and queries that are generated for you, I can tell you that it's not gonna be pretty. If I had to generate types such that it would be as hard as possible to come up with a general abstraction that unifies them, the types I would generate would not be far from the types that amplify currently generates.

karrettgelley commented 4 years ago

@saevarb I appreciate the input but please only comment if you have a fix or a related concern. This post is not about DataStore, and you can read through other issues if you want to know more. This issue also doesn't have to do with my schema or the generated types from my schema. It has to do with the types defined in the aws packages (likely @aws-amplify/api). The subscribe function is not recognized as a chainable function of API.graphql() according to typescript.

By the way, with velocity, lambda, and intentional schema design, I should think that you can do pretty much anything with your data.

sammartinez commented 4 years ago

@karrettgelley Can you provide a little more context on the package.json? I want to see which versions of Amplify you are using along with your dependencies to investigate this further.

stale[bot] commented 4 years ago

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.