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)"]}}}
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)
}