I have searched high and low. But, I did found how to convertToRaw
and convertFromRaw
. That's great but, how to send the raw data with GraphQL?
Option 1:
First thing came to mind, just use JSON.stringify(..)
, create a GraphQLString
and send it. Boom! Done! Well...not so. If I went that route, GraphQL expects the value to be in double quotes and I got error about unexpected token '
etc.
Option 2:
Send it and with GraphQLInputObjectType
. That would work but...you have to write out all those keys in the blocks
array. Nope! My 6th sense tells me there is a better way with JSON.stringify(..)
, but...how?
Here's a visual representation of what the data looks like with used convertToRaw
:
String that was passed to GraphQLString
:
"{"entityMap":{},"blocks":[{"key":"7iq9s","text":"This is the type of text to pass to db via GraphQL","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}]}"
"Syntax Error GraphQL request (29:36) Expected :,
Without convertToRaw
:
{"entityMap":{},"blockMap":{"bk8ro":{"key":"bk8ro","type":"unstyled","text":"This is the type of text to pass to db via GraphQL","characterList":[{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null},{"style":[],"entity":null}],"depth":0,"data":{}}},"selectionBefore":{"anchorKey":"bk8ro","anchorOffset":0,"focusKey":"bk8ro","focusOffset":0,"isBackward":false,"hasFocus":true},"selectionAfter":{"anchorKey":"bk8ro","anchorOffset":50,"focusKey":"bk8ro","focusOffset":50,"isBackward":false,"hasFocus":true}}
I guess I can answer this question after some research. This answer uses atob() and btoa().
To pass a value to a GraphQLString's
args, ie, content
:
convertToRaw
That way you'll end up with a very long string: Q29udGVudFN0YXRlIHsgImVudGl0eU1hcCI6IFtvYmplY3QgT2JqZWN0...etc
To decode, use atob()
, JSON.parse()then
convertFromRaw`. There maybe an easier way but for now, this works. Im sure these steps can be reduced.