在Graphql变异中传递多行字符串

Joh*_*n K 6 graphql

新手来GraphQL.

我必须在一个multiline字符串中传递一个字符串(逐字)mutation.我必须在单独的应用程序中回读文本并将其另存为文本文件.

这是我想要做的.我正在尝试使用GraphiQL客户端与测试服务器通信GraphCool.

mutation {
  createMessage(fullName: "John K", 
    message: "some very long text
message that appears on multiple lines 
with line breaks."    
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个错误

{
"error": "Syntax error while parsing GraphQL query. Invalid input \"\"some very long text\\n\", expected StringValue, BooleanValue, NullValue, Variable, Comments, ObjectValue, EnumValue, NumberValue or ListValue (line 6, column 13):\n    message: \"some very long text\n            ^"
}
Run Code Online (Sandbox Code Playgroud)

我可以用\n替换所有换行符来解决问题.

mutation {
  createMessage(fullName: "John K", 
    message: "some very long text\nmessage that appears on multiple\nlines\nwith line breaks."    
}
Run Code Online (Sandbox Code Playgroud)

但是,我不确定它是否是正确的方法,因为当我回读消息文本并将其视为文本文件时,换行符不会出现,而我得到的只是\n.

请帮忙...

Ano*_*ous 12

尝试过nodejs "graphql": "0.13.2",可以通过包装多行字符串来实现""":

(换行符应该仍然发送到graphql端点\n,只是graphiql可能帮你逃脱了\)

例:

这会失败 message: "some very long text message that appears on multiple lines with line breaks."

这应该通过 message: """some very long text message that appears on multiple lines with line breaks."""


Mik*_*ank 6

您可以将字符串作为变量传递:

GraphQL 查询:

mutation($message: String) {
  createMessage(fullName: "John K", message: $message)
}
Run Code Online (Sandbox Code Playgroud)

JS:

const message = `some very long text
message that appears on multiple lines 
with line breaks.`;
Run Code Online (Sandbox Code Playgroud)

不确定您使用的是什么 GraphQL 客户端,但从这里您应该能够将变量传递给客户端。使用 Apollo (react-apollo),它看起来像这样:

const CREATE_MESSAGE = gql`
  mutation($message: String) {
    createMessage(fullName: "John K", message: $message)
  }
`;

const message = `some very long text
message that appears on multiple lines 
with line breaks.`;

const CreateMessage = () => {    
  return (
    <Mutation mutation={CREATE_MESSAGE}>
      {(createMessage, { data }) => (
        <button onClick={() => createMessage({ variables: { message } })}>
          Create Message
        </button>
      )}
    </Mutation>
  );
};
Run Code Online (Sandbox Code Playgroud)


小智 5

花了很多时间后这对我来说很有效

如果您使用 GraphQL Apollo 客户端 """${yourText}"""将为您提供帮助。

const yourText = "Lorem ipsum 
                  is simply dummy
                  standard dummy";
const input = {
                singleLine : "my name is kool",
                multiLine  : `""${yourText}""`
              }
Run Code Online (Sandbox Code Playgroud)

谢谢你,K00L;)


Ser*_*sev 0

目前在 graphql 中不可能。不过,有一些 RFC

所以,我猜,你最好的选择是处理你的输入并替换文字字符\n换行符。根据您输入的内容,这可能会产生一些误报......