Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
GraphQL print function doesn't escape string correctly

I am using the following code:

const GQL_ADD_JOB = gql`
    mutation AddJob {
        addJob(title: $title, company: $company, description: $description) {
            title
            company
            description
        }
    }
    `;

And then using axios, I attempt to POST using GraphQL's print function:

request.post(
  GQL_ENDPOINT,
  {
    query: print(GQL_ADD_JOB),
    variables: {
      title: job.title,
      company: job.company,
      description: job.description,
    },
  }
)

Where job.title, job.company, and job.description can all be strings containing any character, including newlines, quotes, etc. However, calling this results in a JSON parse error. The error shows that the request sent the following data:

'{"query":"mutation AddJob {\n addJob(title: $title, company: $company, description: $description) {\n title\n company\n description\n }\n}","variables":{"title":"Route Service Sales Representative - 3rd Shift","company":"Cintas","description":"test desc"}}'

When I use this data and manually perform a POST via curl, I get the following error:

{"message":"Syntax Error: Cannot parse the unexpected character "\\".","extensions":{"code":"GRAPHQL_PARSE_FAILED","exception":{"stacktrace":["GraphQLError: Syntax Error: Cannot parse the unexpected character "\\"."," at syntaxError (/home/user/repos/comp/node_modules/graphql/error/syntaxError.js:15:10)"," at readToken (/home/user/repos/comp/node_modules/graphql/language/lexer.js:360:40)"," at Lexer.lookahead (/home/user/repos/comp/node_modules/graphql/language/lexer.js:75:108)"," at Lexer.advance (/home/user/repos/comp/node_modules/graphql/language/lexer.js:58:35)"," at Parser.expectToken (/home/user/repos/comp/node_modules/graphql/language/parser.js:1408:19)"," at Parser.many (/home/user/repos/comp/node_modules/graphql/language/parser.js:1519:10)"," at Parser.parseSelectionSet (/home/user/repos/comp/node_modules/graphql/language/parser.js:271:24)"," at Parser.parseOperationDefinition (/home/user/repos/comp/node_modules/graphql/language/parser.js:199:26)"," at Parser.parseDefinition (/home/user/repos/comp/node_modules/graphql/language/parser.js:137:23)"," at Parser.many (/home/user/repos/comp/node_modules/graphql/language/parser.js:1523:26)"]}}}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I changed my mutation to this and now it works. I still don't fully understand why the other format didn't work.

mutation AddJob($title: String!, $company: String!, $description: String!) 
{
    addJob(title: $title, company: $company, description: $description)
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!