Forum Discussion

wdrain's avatar
wdrain
Icon for Helper I rankHelper I
2 years ago
Solved

How would do you escape a variable name with a Hypen in a GraphQL query string?

I am able to connect to GraphQL restapi with a variable name with space using a GraphQL query string by prefixing and suffixing the variable name with a \ (backslash) and 2 double quotes "". 

name: "Custom Tag"
i.e.   name: \""Custom Tags\""  => works successfuly escaping with the \ backslash and 2 double quotes ""

However a variable name GraphQL Query containing  a  "-" hypen or dash will not work and I need help troubleshooting.

 

name:"Custom - Tag2"
i.e. name:\""Custom - Tag2\"" => Does not work, because of the hypen and I don't know how to escape this?



  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi wdrain ,
    According to your description, in GraphQL, variable names cannot contain hyphens or dashes because they are not valid characters for identifiers.1 This is due to possible ambiguity in parsing, as hyphens may be interpreted as subtraction operators in many programming languages. If you need to keep hyphens in the value, you can try to do so, but the variable name itself should follow valid naming conventions.

    {
      customTag2: "Custom - Tag2"
    }
    

     

    Queries and Mutations | GraphQL
    [question] why names cannot contain dash '-' · Issue #479 · graphql/graphql-spec · GitHub

     

    Best regards,
    Albert He


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi wdrain ,
    According to your description, in GraphQL, variable names cannot contain hyphens or dashes because they are not valid characters for identifiers.1 This is due to possible ambiguity in parsing, as hyphens may be interpreted as subtraction operators in many programming languages. If you need to keep hyphens in the value, you can try to do so, but the variable name itself should follow valid naming conventions.

    {
      customTag2: "Custom - Tag2"
    }
    

     

    Queries and Mutations | GraphQL
    [question] why names cannot contain dash '-' · Issue #479 · graphql/graphql-spec · GitHub

     

    Best regards,
    Albert He


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly