Apollo GraphQL "writeQuery" causing re-render to the app which is leading to an unnecessary network request - graphql

I'm working on a chat app, developed using Apollo GraphQL and React and I have the following issue:
I have a component called "Conversation" which is fetching an array of messages between two users (the logged-in user and other user) using the "useQuery" hook of Apollo which is invoked every time the component mounts.
When a new message is sent by the logged-in user, I'm updating the cached array of messages using the "writeQuery" function provided by Apollo.
By doing that, the "Conversation" component is getting re-rendered, which is leading to an unnecessary network request because the "useQuery" hook is triggered again.
I would love to hear if there is a way of preventing "writeQuery" from triggering a re-render to the relevant component.
Thanks a lot in advance!

Related

Best practice to solve Livewire UI Not updating until after API call

I am using Laravel Livewire, I currently have a form that captures a user's address information, then it needs to submit that to a 3rd party API (it's for billing information).
The issue that I am facing is that the UI doesnt update / livewire doesnt call render until after the entire method has run - which looks on the frontend like the UI is frozen / system isnt doing anything as the API is slow and takes a few seconds (15 - 20) to give a response.
I want to be able to trigger a browser event to display an alert saying the request is processing / show an alert banner on the page that it is processing before making the API request, process the API request, then display the results.
Currently I have identified the following ways to make this happen:
make the updates on the frontend, fire off an event with the data needed. Laravel event listener handles the API call, and emits a result event that the livewire component listens for, then the livewire component updates the page with the results.
make the updates on the frontend, dispatch a job to run in a queue worker, emit an event that the livewire component listens for, update the page with the results.
emit an event from the livewire component to itself, the livewire component then listens for that event and processes the API request, then updates the frontend with the results.
emit an event from the livewire component to itself to update the UI with processing info, while it still runs the API process in the original method call, and then updates the UI once the API gets a response
My thoughts are, what would be best practice here? All the above solutions require a different amount of work to get running, and solution 1 and 2 looks like complete overkill for such a simple process.
Does this come down to preference? Or is there actually a best practice way to do this in livewire?
And additionally, is there another way / simpler way to do this that I havnt mentioned above?
Your feedback is appreciated.
I would probably create a job and then return a response to the user. Then instigate Livewire polling that can check for the job being complete. If the API takes 15-20 seconds then polling once per second should be fine.
You might want to store the state in the database so that the job and the livewire component can communicate with each other. If you don't need to keep the data, using the cache to communicate between job and component would be a good alternative.

Apollo GraphQL Client - Modifying The Data From JS (React)

I'm developing a real time chat app using graphql and I want to modify fetched data instead of refetch it again - in order to reduce unnecessary network requests.
When my app first init I'm fetching a list of users (that are not the current logged-in user) to display on the sidebar of the app.
Each user contains a lastMessage object that represent the last message that he sent or received.
When a user clicks on a user to chat with, another query is submitted in order to get all the messages between the users.
Using subscriptions, I'm updating the messages state when a new message is sent (either by the logged-in user or by the other user).
Now, I want to update the lastMessage object of the related user displayed on the sidebar, but I don't want to refetch all the users again in order to do so, because I have all the updated data in the client, which I received by the subscription event. Instead, I want to update the graphql queried data using js.
Is there a way to do that?
Thanks.
Apollo cache has a 'modify' method that you can use to set the cache of user's lastMessage.
Apollo docs for cache modify

How to add a dynamic results section to Laravel

I made a laravel app which uses an index method that delivers a collection of the client's available appointments (think hairdresser) to the page (as an api), which I then display using vue/vuetify.
The client is saying they would like the appointments on the page to be dynamic/live eg if someone books an appointment, then all other logged in users will see that appointment disappear from the list on their screen.
I have no idea how I would do this, although I have had one idea - I somehow incorporate node/non-blocking on the server, like a chat room, but only for this part of the app.
Or is there a way to do this with laravel/nginx?
Thanks in advance - I don't know what to search for!
I believe you are looking for Broadcasting (Documentation Link). You would need to:
Configure your broadcasting driver (you could give pusher a try for quick setup and tinkering)
Configure your Laravel backend to dispatch a new event whenever a new appointment is made, (e.g. event(new AppointmentCreated($appointment)) where AppointmentCreated implements the ShouldBroadcast interface. You can combine this with Model Events
Update your frontend to receive your broadcast (Check Laravel Echo). Once you receive a broadcast, update the UI to mark this appointment as unavailable i.e, make it disappear

Symfony3.4 - Send a Server-Sent Event when something happens

I'm developing a project with Symfony 3.4.
I would like to send a message to all the clients currently viewing a page when something happens. I evaluated both Server-Sent Events and Websockets, and I decided to go with the former, because the communication is unidirectional (only server to client).
For this purpose, I'm using this library: https://packagist.org/packages/tonyhhyip/sse
It seems to work, but I need to specifically send a message when something happens in the whole system. I tried with the Symfony event system (by creating a custom event), but events seem to be dispatched and captured only within the same session (i.e., the same logged user). In other words, if an action performed by a user triggers an event, it is not captured by other users and therefore a message is not sent to the browser via SSE.
Any suggestion?
Thank you

How to sync my backbone.js app with the changes on server?

I have an app in backbone.js. The user can add items to the app which are added at the backend server and the collection is refreshed and the user sees the added data. How can I sync the app with any changes in the backend. Suppose if multiple users add the data at the same time each one should see the changes.
There are options like ajax polling where I can refresh the model after a certain time period but I hate to use it.
Can you suggest any event driven method where any change in the backend is reflected immediately in the frontend of my app.
You can add a kind of refresh event on your app, which will refresh every few minutes. Something like evernote desktop application does. In that event you would add collection sync event.
You can also use something like
collection.sync(method, collection, [options])
http://backbonejs.org/#Sync
This doesn't answer your question though, and I'm not sure how you would push changes from server to client.

Resources