aws-mobile-appsync-sdk-js: Mutations fail using aws-appsync with @apollo/react-hooks 3.0.0
Do you want to request a feature or report a bug? Bug
What is the current behavior? When performing a mutation whilst online, the promise never resolves, and the mutation just hangs.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.
We are starting to use @apollo/react-hooks 3.0.0 since the previous hook library we were using has been deprecated and has a bug. This has a dependency on apollo-client 2.6.3, which is not compatible with aws-appsync.
To fix this, we are forcing resolutions in npm to fix the version across libraries:
"resolutions": {
"apollo-client": "2.6.3"
}
The app compiles and runs ok. Queries work fine, but the mutations hang.
To make sure the problem doesn’t lie with react-hooks lib, i tried mutating using the client directly:
console.log('mutating...')
const result = await client.mutate({
mutation: MUTATION,
variables: {
forename: 'test value',
},
})
console.log('result', result)
What is the expected behavior?
Mutations should work as normal
Which versions and which environment (browser, react-native, nodejs) / OS are affected by this issue? Did this work in previous versions?
@apollo/react-hooks 3.0.0 aws-appsync 1.7.2 apollo-client 2.6.3
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 51
- Comments: 51 (3 by maintainers)
here is my temporary solution, switching to
ApolloProviderand setting it up to use the link from AppSyncSupport for @apollo/react-hooks 3.0.1 or greater would be greatly appreciated as the hooks simplify a lot of boiler plate code. Is there any ETA on when this will be supported?
Hello everyone - I want to reply and assure you that we have not abandoned support of this project. Per my earlier comment, this is a complex process and to be completely transparent there are things in the Apollo library that are out of our control and there have also been breaking changes in Apollo versions which we’re trying to account for, which is also why we pinned the version. We’ve been spending many hours trying to account for this but it’s not simple to solve. After putting more thought into this we’ve got some initial solutions that I hope will unblock many of you. What we’ve done is packaged two of the “Links” which are part of the SDK - the Auth and Subscription Links - and published them to NPM. This will allow you to include them in the newer Apollo versions if you wish to use the newer features. This does not include the “Offline Link” for offline support which is more complex to solve and we’re looking into for the future.
Installation of the links:
Usage: https://github.com/awslabs/aws-mobile-appsync-sdk-js#using-authorization-and-subscription-links-with-apollo-client-no-offline-support
With is I would then give the following recommendations for choosing your client strategy:
Please let us know if you have any trouble using these links or if there is a scenario which we haven’t accounted for here.
My example at https://github.com/wolfeidau/appsync-apollo-links is now includes support for Cognito Pools rather than just API key.
Everything is working 👍 which is great!
happy anniversary, thanks to bugs like these I have moved / actively don’t recommend Appsync
I think for a paid solution like AppSync, should not need so much hacking. I have tried all the above solutions but still facing issues. I am reverting apollo to a compatible version.
I wonder if there’s any ETA for solution of this issue?
Any Update ?
I also have this problem. It’s quite disappointing to see it failing even though react-apollo v3 was in public beta for about two months now… Likely, the issue is related to offline functionality since passing
disableOffline = truein client’s options and providing custom Rehydrate makes them work againHi @undefobj,
Is there any sort of roadmap for fixes to this?
I’ve just spent several days trying to upgrade to Expo36 (which includes RN0.61 and React 16.9), but I’m ready to give up as I can’t get it working 😦
Our constraint is that we need the offline capability for our app plus we need the very neat AppSync feature that allows us to upload images, so, as per your comment above, we’re stuck with using apollo-client@2.4.6. But this isn’t compatible with React 16.9 as it uses
componentWillReceiveProps, so we get warnings when we start up the app.In fact in order to make it work (and to fix an issue where you have pending mutations that include images as Blobs), we’ve had to create our own versions of AWSAppSyncClient and the store and
offline-link. We’ve actually got everything working with Expo locally, just with warnings about the lifecycle methods and RNCNetInfo. But when I build an APK and try that it crashes on startup with an error sayingExpo encountered a fatal error: RNCNetInfo.getCurrentState got 3 arguments, expected 2. We’re not using RNCNetInfo at all, but it’s used inRehydrate, so we’ve created our own version of that, and we still get the error.We seem to be completely stuck! As far as I can tell, it’s just NOT possible to use AWS AppSync with offline and image upload capability with RN0.61 and React 16.9.
In case it’s useful to anyone, here is the set of packages that we’ve managed to get working together.
Please give an ETA for this.
Hello everyone, I have just published an article on how to set up AWS AppSync with react apollo 3.x, react hooks and offline support. Hope it helps!
We have now explored this issue further. The reason to use AppSync over just the ApolloClient is if you need the offline capability. We wrote our own Rehydrated and got around the issues there but then when posting a mutation, it got enqueued in redux-offline but never processed. We debugged the flow and found the issues to be in the offline-link created redux-offline effect.
AppSync is using a method called
client.queryManager.buildOperationForLinkthat no longer exists to create the observable and hence nothing happens when redux-offline calls the effect.We refactored this to the following construct using the same approach as Apollo does in the mutations:
There were a few other minor issues that we have been changing to make the framework work for our specific use case but this is the key one the core framework falls over on.
We now have this working nicely with hooks and our adapted version of AppSync for our use case and I just wanted to share in case anyone else are looking to do the same or if this could inspire the core team to update the framework as it is a hinderance for adaptation.
I am working on adding Cognito pool authentication to the example as it is the more common requirement.
I am planning to add this using
@amplify/authandreact-amplify.Hopefully that will round it out as a more generally usable sample as well!
@isocra it should - it is doing what AppSync is building within it’s own client class but skipping Rehydrate and the observable there which is causing issues but I haven’t tested it
You can build the
ApolloClientclass in whichever way would suit such as apollo-cache-persist withAsyncStoragesupport for Native from herefrom the example:
even with this issue fixed I believe we’re going to stick to using AppSync in this way because we’ve had too many issues with the layer of abstraction ontop of Apollo and benefiting from new releases
@wolfeidau oh my god. the only thing I was missing was
Auth.configure(awsconfig);@nc9, is this solution supports AppSync’s offline and subscription capabilities?
@wolfeidau can you confirm your solution works with complex objects (ie single mutation that adds records to dynamo DB and uploads the file to s3)?
For what it’s worth these packages worked for me. I’m using the apollo hooks library primarily.
Implementation:
I put together an example application of my own https://github.com/wolfeidau/appsync-apollo-links which demonstrates realtime websocket connections.
Has anyone worked out a solution for SSR? I am using AppSync with Next.js. A solution found here https://github.com/dabit3/next-apollo-appsync works with Apollo render props, NOT with hooks, however 😦.
Here is how I am trying to initialize
ApolloClient(it works only on the client-side, the server stucks atloading: truestate):I am also facing the same problem as @stewartduffy so if anyone finds a solution, please post as I need this rectified urgently!!!
Temporary hack: https://github.com/apollographql/react-apollo/issues/3148#issuecomment-511622210
Here’s step by step:
Force appsync to resolve to apollo-client 2.6.3 or greater (2.6.4 works as of this writing). If you’re using Yarn, add in your
package.jsonaresolutionskey and addapollo-client:2.6.4.Then run
yarn install. (If that didn’t work, try deleting thenode_modulesfolder first).If you’re using npm, then you will have to patch the package-lock.json. You can do this with
npm-force-resolutions.Add in
resolutions"apollo-client": "2.6.4"into package.jsonDelete the entire
node_modulesfolder, and then run:npx npm-force-resolutionsThis will force all the dependencies inside package.json to reference the apollo-client package correctly.
And then:
npm installI’m not using Rehydrated because we’re
disableOffline: trueThis was the aim of my complete example, over fragments.
Great to hear it has helped!
@undefobj @dabit3 @elorzafe @manueliglesias Any news for this issue? Should we switch to Amplify Datastore (as it is low low low priority in the backlog) or it’s still going to be resolved?
@isocra if you require offline per the comments above and the documentation you can use 2.4.6 with the standard client and don’t need a link.
@undefobj, I need the offline capability as I’m building a mobile app. Can you confirm which versions of this SDK, Apollo and any others I can safely use that will work well together?
Thanks
@tgjorgoski, I am sorry for not replying earlier. I can confirm that we use a rewritten version of the AppSync client with Apollo 3 offline using the approach described above. We have converted to use the Apollo hooks pattern and it works well in our use case. We have not changed the local state management. I assume @undefobj has referred to when saying that they need full context of the state in the middleware. This compromise works for us at the moment as we use Redux for our AppState. We use the local state management in Apollo for metadata on mutations and it works fine with the current approach. I look forward to see where AWS is heading with this. We are unfortunately very far from the standard code in a fair few areas making a pull request difficult and maybe not a good idea if AWS is working on a solution. @undefobj, I would be happy to collaborate and contribute if you want someone to work with you on these issues.
@nc9 Great solution! This saved the day for us. Do you happen to know if there is any additional configuration needed to get this working with subscriptions?
Not to stir the pot, but seeing as how we need to decide on a solution within the next 24 hours, we are deciding if we want to revert 2 days of work and just eat that time, or replace appsync sdk with plain apollo client. Seeing as how we’re not using the react appsync package at all, the biggest thing would be what we would be giving up if we did.
Most offline things seem to be able to be handled with relative ease with other packages, and we can handle auth manually. Does anyone know what else we might be giving up by removing this dependency from our react app to get around this issue?
EDIT: Nevermind, I see now that appsync uses MQTT instead of the websockets that apollo supports. Guess we’ll need to start reverting all changes.
Just ran into this after spending 2 days migrating to the new version, a fix or workaround would be much appreciated as this is preventing us from updating our production app.