I'm looking at this line specifically:
https://github.com/apollographql/apollo-server/blame/f6cdc94f291a202344453027c6c8735841fc8eb6/packages/apollo-server-lambda/src/lambdaApollo.ts#L46
When I try to test my Lambda locally using this command:
SLS_DEBUG=* serverless invoke local --function graphql --data '{ "httpMethod": "POST", "body": { "query": "{ posts{ id description } }" } }'
I get this error:
SyntaxError: Unexpected token o in JSON at position 1
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at graphqlHandler (/Users/geoff/.../node_modules/apollo-server-lambda/dist/lambdaApollo.js:28:20)
If I modify that line to accept the event.body without Parsing, it works just fine. This even fails when writing a test in the Lambda management panel. So I thought I'd ask why is this the case? In what case would someone send JSON as a string when it seems like invoking with a body is always done as JSON.
@gyfchong I'm getting the same error. How did you get it to accept the body without parsing?
@gyfchong I'm getting the same error. How did you get it to accept the body without parsing?
Haven't touched this part of my code lately, but I essentially just modified it in my node_modules for the time being...
Hi,
I ran into the same error earlier when invoking a GraphQL function locally. I eventually got local invoke to work with:
SLS_DEBUG=* serverless invoke local --function graphql --data '{ "httpMethod": "POST", "body": "{ \"query\": \"{ posts{ id description } }\" }" }'
The key difference is: surrounding the value of body with quotation to make it a valid JSON.
Hope it helps!
This is not only an issue for local invocation of the lambda. If the graphql server is given malformed JSON that is, for example, missing a comma, the production endpoint will simply respond with
{
"errors": [
{
"message": "Internal server error"
}
]
}
Which does not easily help the client-side developer find their error.
When I do the same test on serverless-offline I do see the error logged:
SyntaxError: Unexpected string in JSON at position 309 ApiGateway.js:991
and it does show up in on the serverless-offline endpoint as
{
"errorMessage": "Uncaught error in your 'graphQL' handler",
"errorType": "SyntaxError"
}
Is there a way to catch that error and indicate that it is a client error in the response?
Most helpful comment
This is not only an issue for local invocation of the lambda. If the graphql server is given malformed JSON that is, for example, missing a comma, the production endpoint will simply respond with
Which does not easily help the client-side developer find their error.
When I do the same test on
serverless-offlineI do see the error logged:and it does show up in on the serverless-offline endpoint as
Is there a way to catch that error and indicate that it is a client error in the response?