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...
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.
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.
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).
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."""
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>
);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With