Subscription Typescript Error
See original GitHub issueDescribe 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
-
Create a blank amplify react app
-
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.
- OS: MacOS Catalina 10.15.3
- Browser: Brave
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.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (1 by maintainers)
Top GitHub Comments
@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 ofAPI.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.
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.