Hasura, Algolia, and Netlify Functions

Hello,

I am trying to figure out the best way to delete an object and update an object using netlify functions with hasura and algolia. I was able to get an add objects netlify function to work, but not sure how for updating and deleting with Algolia and Hasura event trigger payloads. Any thoughts on how to get this to work?

https://hasura.io/docs/1.0/graphql/manual/event-triggers/payload.html

Hi there,
Thanks for reaching out to us.

I personally haven’t used Hasura before, but if you managed to add objects then I guess updating and deleting would be similar.

If I understand correctly, when Hasura triggers events, then your Netlify function will be executed.
And inside your Netlify function, you can use Algolia JavaScript API Client to perform indexing-related methods.

Could you tell me how you implemented adding objects and how you’re having trouble with updating and deleting?

Hello, I made a gist with my netlify functions. I am using Algolia version3 instead of version4 at the moment. The addMessage code works, but the update and delete functions do not work. For the delete function, I get “AlgoliaSearchError: ObjectID must be a string” error message. For the update function, I get the “[object Object]” message.

Hi there,

Thanks for providing the details.

From your code

await index.deleteObject(request.event.data.old);

I’m not sure what old looks like, but our JavaScript API client asks for a string (objectID).

The link above is the documentation for v3.
Try to pass an objectID and let us know if it works for you.

I was using the event.data.old based on the event trigger payload in the Hasura docs. So I guess I am trying to reconcile the ObjectID in algolia with the event trigger payload in Hasura. Based on this example I found with Algolia and Firebase, the firebase object key is used as the objectID. I guess I need to figure out a key in Hasura.

function deleteIndexRecord({key}) {
// Get Algolia’s objectID from the Firebase object key
const objectID = key;
// Remove the object from Algolia
index
.deleteObject(objectID)
.then(() => {
console.log(‘Firebase object deleted from Algolia’, objectID);
})
.catch(error => {
console.error(‘Error when deleting contact from Algolia’, error);
process.exit(1);
});
}

You indeed need an objectID in order to be able to delete a record.
You will need to set it using an unique identifier if you can find one in request.event.data.old when creating the record in Algolia.

If you don’t provide one, Algolia will create one automatically for you, but you won’t be able to know it for update and deletion events.