在Graphql中传递多行字符串

在Graphql中传递多行字符串,graphql,Graphql,新手到GraphQL 我必须在变异中传递多行字符串(逐字)。我必须在一个单独的应用程序中读回文本,并将其保存为文本文件 这就是我要做的。我正在尝试使用GraphiQL客户端与GraphCool中的测试服务器进行对话 mutation { createMessage(fullName: "John K", message: "some very long text message that appears on multiple lines with line breaks."

新手到
GraphQL

我必须在
变异
中传递
多行
字符串(逐字)。我必须在一个单独的应用程序中读回文本,并将其保存为文本文件

这就是我要做的。我正在尝试使用
GraphiQL
客户端与
GraphCool
中的测试服务器进行对话

mutation {
  createMessage(fullName: "John K", 
    message: "some very long text
message that appears on multiple lines 
with line breaks."    
}
我得到了这个错误

{
"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            ^"
}
我可以通过将所有换行符替换为\n来解决此问题

mutation {
  createMessage(fullName: "John K", 
    message: "some very long text\nmessage that appears on multiple\nlines\nwith line breaks."    
}
但是,我不确定这是否是正确的方法,因为当我读回消息文本并将其视为文本文件时,不会出现换行符,而且我得到的只是\n


请帮忙

目前在graphql中不可能。不过也有


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

已尝试使用nodejs
“graphql”:“0.13.2”
,可以通过将多行字符串包装为
”“

(换行符仍应作为
\n
发送到graphql端点,只是graphiql可能会帮助您跳过\)

例如:

这将失败
消息:“一些很长的文本
显示在多行上的消息
带换行符。“

这应该传递
消息:“一些很长的文本
显示在多行上的消息

使用换行符。“”

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

GraphQL查询:

mutation($message: String) {
  createMessage(fullName: "John K", message: $message)
}
JS:

不确定您使用的是什么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>
  );
};
const CREATE_MESSAGE=gql`
变异($message:String){
createMessage(全名:“John K”,message:$message)
}
`;
const message=`一些很长的文本
显示在多行上的消息
换行;
const CreateMessage=()=>{
返回(
{(createMessage,{data})=>(
createMessage({变量:{message}}}>
创建消息
)}
);
};

在花了很多时间之后,这对我来说很有用

如果您使用的是GraphQL Apollo客户端
“${yourText}”
将帮助您

const yourText = "Lorem ipsum 
                  is simply dummy
                  standard dummy";
const input = {
                singleLine : "my name is kool",
                multiLine  : `""${yourText}""`
              }

谢谢你K00L;)

谢谢塞尔吉奥。如果没有标准选项,那么我想使用“\n”没有什么特别的好处。我的意思是,我可以使用任何我知道的字符,这些字符在回读后必须用换行符替换。对吗?顺便说一句,你的查询中似乎缺少了一个右括号。你救了我一天,T汉克斯
const yourText = "Lorem ipsum 
                  is simply dummy
                  standard dummy";
const input = {
                singleLine : "my name is kool",
                multiLine  : `""${yourText}""`
              }