Sending double quotes and backslash in Graphql request
Asked Answered
O

4

13

I want to send double quote(") and back slash(\ ) in Graphql request and want to get it back in response.

Please check bellow example:

mutation M { 
    signUp
    (
        name: "Harsha Vardhan"
        username: "Harsha143", 
        email: "[email protected]",
        description: "Cool "boy" \n And good looking."
    ) 
    {
        _id,
        name
        username,
        email,
        description
}

In above Mutation I want to send double quotes and back slash in description. Please guide me to overcome this.

Thanks in advance.

Ornate answered 8/6, 2016 at 17:35 Comment(2)
Did you try escaping those characters? "Cool \"boy\" \\n And good looking."Agminate
It doesn't help if you try to escape these characters like this \"boy\" !Beaton
M
7

You should try to escape those characters like so

mutation M { 
    signUp
    (
        name: "Harsha Vardhan"
        username: "Harsha143", 
        email: "[email protected]",
        description: "Cool \"boy\" \\n And good looking."
    ) 
    {
        _id,
        name
        username,
        email,
        description
}

Hope that helps!

Mettle answered 12/6, 2016 at 22:49 Comment(2)
how to handle if there is a "(double quotes) after first escape ( may be within the word - boy"s )? <- this scenario might break and throw syntax errorStellastellar
It doesn't work if you try to escape those double quotes with backslash like this \"boy\" . I tried it and it didn't help.Beaton
M
1

Note that if you are sending in through something else that deals with quotes and backslashes, you may need more backslashes than you are expecting.

    description: "Cool \\\"Boy\\\" and good looking."

I came to the same question with the same issue where \" didn't work, but then I worked through the stack of transformations and found some java code doing this.

  query = queryInput.replaceAll("\n", "\\\n");

which I needed to change to (this awful construct).

  query = queryInput.replaceAll("\\\\","\\\\\\\\")
      .replaceAll("\"", "\\\"");

and then further downstream in some Rust code.

  stage2 = stage1.replace("\\","\\\\").replace("\"", "\\\"")
``

Welcome to "backslash hell", where you end up doubling backslashes more that you imagine.
Mosa answered 6/2, 2023 at 14:42 Comment(0)
S
0

You have to surround the description's value with triple quotes like this:

mutation M { 
    signUp
    (
        name: "Harsha Vardhan"
        username: "Harsha143", 
        email: "[email protected]",
        description: """Cool "boy" \n And good looking."""
    ) 
    {
        _id,
        name
        username,
        email,
        description
}

This is how your value will be stored in the database:

"""Cool \""boy\"" \\n And good looking."""

When you fetch it back it will look like you expect it to be.

I tested this using a NestJS GraphQL endpoint with PostgresDB.

Spermatium answered 23/2, 2023 at 16:3 Comment(0)
B
0

Not recommended, but there are two simple solutions.

  1. Transform string to base64 (btoa() and atob() in js)
  2. Replace those characters with some unused character of set of characters.

Then when you get the data back, do the opposite, to transform back to the original string.

Benoit answered 22/3, 2023 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.