To cut back on query costs in graphql, I was thinking about storing data in a single metafield. All the data is related, and I often need to get it all at once. According to the documents on metafield types, the JSON string type is depreciated so I guess I would have to store the data as a long string with type single_line_text_field. So I would have to make the JSON object, stringify it, then add it to the metafield. However, JSON Objects and the metafields require double quotes so I would have to escape all the quotes when turning the object into a string (Unsure of this - JSON.stringify() and/or graphql may take care of that for me?).
var data_obj = {"key1": "value1", "key2": "value2"};
{
"metafield": {
"value": "{\"key1\": \"value1\", \"key2\": \"value2\"}"
}
}
I could then use JSON.parse() on the value to turn it back into an object when I need to read/edit a certain key.
It will get a little more complicated when I want an array of objects as I will have to figure out how to go from a string to an array without messing up the objects (probably using some regular expressions).
Will this work or is it a bad idea? The docs say that the character limit for a string is 5 million, so I don’t need to worry about that. But I’m worried that having to go back and forth between a string and an array of objects will cause some errors. It would be nice to just use the JSON string metafield type, but I’m assuming I shouldn’t use something that is depreciated.