Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda