Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multi line string in a Graphql mutation

Tags:

graphql

Newbie to GraphQL.

I have to pass a multiline string (verbatim) inside a mutation. I have to read back the text in a seperate application and save it as a text file.

Here is what I am trying to do. I am trying this using GraphiQL client talking to a testing server in GraphCool.

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

And I get this error

{
"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            ^"
}

I can resolve the issue by replacing all line breaks with \n.

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

However, I am not sure though if it is the right approach because when I read back the message text and view it as a text file line breaks do not appear and all I get are \n.

Please help...

like image 847
John K Avatar asked Aug 30 '17 08:08

John K


People also ask

How do you pass two arguments in GraphQL query?

Multiple arguments can be used together in the same query. For example, you can use the where argument to filter the results and then use the order_by argument to sort them.

How do you pass parameters in GraphQL query?

When you're passing arguments in code, it's generally better to avoid constructing the whole query string yourself. Instead, you can use $ syntax to define variables in your query, and pass the variables as a separate map. . then(data => console.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).


2 Answers

Have tried with nodejs "graphql": "0.13.2", it is possible to do it by wrapping your multiline string with """:

(the line break should be still send to the graphql endpoint as \n, just graphiql probably help you escaped the \)

example:

this will fail message: "some very long text message that appears on multiple lines with line breaks."

this should pass message: """some very long text message that appears on multiple lines with line breaks."""

like image 126
Anonymous Avatar answered Sep 22 '22 23:09

Anonymous


You could pass the string as a variable:

GraphQL query:

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

JS:

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

Not sure what GraphQL client you're using but from here you should be able to pass the variables to the client. With Apollo (react-apollo) it would look like this:

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>
  );
};
like image 25
Mikael Lirbank Avatar answered Sep 22 '22 23:09

Mikael Lirbank