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

时间:2017-08-30 08:04:46

标签: graphql

新手multiline

我必须在mutation内传递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            ^"
}

我收到此错误

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

我可以用\ n。

替换所有换行符来解决问题
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        try
        {
            var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

            var user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null || !user.IsActive)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            var oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
               OAuthDefaults.AuthenticationType);
            var cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                CookieAuthenticationDefaults.AuthenticationType);
            var properties = CreateProperties(user.UserName);
            var ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);

        }catch(Exception ex){
            throw ex;
        }

    }

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

请帮忙......

4 个答案:

答案 0 :(得分:7)

尝试使用nodejs "graphql": "0.13.2",可以通过使用"""包装多行字符串来完成此操作:

(换行符应该仍然作为\n发送到graphql端点,只是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."""

答案 1 :(得分:1)

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

GraphQL查询:

module/state

JS:

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

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

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

答案 2 :(得分:1)

花了很多时间后,对我来说很重要

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

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

谢谢K00L;)

答案 3 :(得分:0)

目前在graphql中不可能。但是有some RFC

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