javascriptpostgresqlgraphqlspecial-charactersdirectus

How to mutate via GraphQL special characters and HTML with JavaScript?


I'm fetching emails from IMAP and want to store them in PostgreSQL via Hasura GraphQL API. As HTML and special characters can't be in the valid GraphQL query, I need to get rid of them. Remove or replace with something acceptable. That ends up with ugly and unformatted content in the DB. Googled but can't really find a good or at least near-perfect solution for getting the most readable (ideally lossless) content in PostgreSQL.

I have to create an admin panel where emails can be assigned to users and have other relations and different rights (eg. one role won't see email addresses and possible phone numbers and other contacts removed from the email content). But they can reply from the system. Everything is easy except the question of how to get the modified HTML email to the DB.

Maybe I shouldn't use GraphQL. Maybe not JavaScript. I can code in PHP, too if that's easier but I prefer JavaScript as I use N8N.io workflow and I can easily use JavaScript there. Option I was also considering is to use PostgreSQL compression but then I can not use the Directus.io admin panel (that is used) for viewing, editing and adding relations to the emails.


Solution

  • I can see from your variables.json that you have line breaks in your JSON. After removing line breaks the JSON becomes valid. JSON does not allow real line breaks so you must use \n instead. Because you have HTML and use <br> for line breaks you can just remove the real line breaks for example like this:

    someText = someText.replace(/(\r\n|\n|\r)/gm, "");
    

    More about removing line breaks with JavaScript here: https://www.textfixer.com/tutorials/javascript-line-breaks.php

    You may copy your JSON to VSCode for example and you will see if your JSON is valid. In the code, you can also check with JavaScript if the string is valid a valid JSON.