如何使用 GraphQL 保存 Draft.js 数据
How to save Draft.js data using GraphQL
我搜索了高低。但是,我确实找到了 convertToRaw
和 convertFromRaw
的方法。这很好,但是,如何使用 GraphQL 发送原始数据?
选项 1:
想到的第一件事就是使用JSON.stringify(..)
,创建一个GraphQLString
并发送。繁荣!完毕!好吧……不是这样。如果我走那条路,GraphQL 期望值用双引号引起来,我得到关于 unexpected token '
等的错误
选项 2:
发送并与 GraphQLInputObjectType
一起发送。那行得通,但是...您必须在 blocks
数组中写出所有这些键。没有!我的第六感告诉我 JSON.stringify(..)
有更好的方法,但是...如何?
这是使用过的数据的直观表示 convertToRaw
:
传递给 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 :,
没有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}}
经过一番研究,我想我可以回答这个问题。这个答案使用 .
将值传递给 GraphQLString's
args,即 content
:
- 将Draft.js转换为
convertToRaw
- JSON.stringify() 结果
- 使用 btoa()
这样你会得到一个很长的字符串:Q29udGVudFN0YXRlIHsgImVudGl0eU1hcCI6IFtvYmplY3QgT2JqZWN0...etc
要解码,请使用 atob()
、JSON.parse()then
convertFromRaw`。也许有更简单的方法,但就目前而言,这是可行的。我确信可以减少这些步骤。
我搜索了高低。但是,我确实找到了 convertToRaw
和 convertFromRaw
的方法。这很好,但是,如何使用 GraphQL 发送原始数据?
选项 1:
想到的第一件事就是使用JSON.stringify(..)
,创建一个GraphQLString
并发送。繁荣!完毕!好吧……不是这样。如果我走那条路,GraphQL 期望值用双引号引起来,我得到关于 unexpected token '
等的错误
选项 2:
发送并与 GraphQLInputObjectType
一起发送。那行得通,但是...您必须在 blocks
数组中写出所有这些键。没有!我的第六感告诉我 JSON.stringify(..)
有更好的方法,但是...如何?
这是使用过的数据的直观表示 convertToRaw
:
传递给 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 :,
没有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}}
经过一番研究,我想我可以回答这个问题。这个答案使用
将值传递给 GraphQLString's
args,即 content
:
- 将Draft.js转换为
convertToRaw
- JSON.stringify() 结果
- 使用 btoa()
这样你会得到一个很长的字符串:Q29udGVudFN0YXRlIHsgImVudGl0eU1hcCI6IFtvYmplY3QgT2JqZWN0...etc
要解码,请使用 atob()
、JSON.parse()then
convertFromRaw`。也许有更简单的方法,但就目前而言,这是可行的。我确信可以减少这些步骤。