1

I'm trying to convert my serverless nodejs graphql api to use typescript but serverless throws an error stating that the graphql handler is not a function.

Error message:

Error: Serverless-offline: handler for 'hello' is not a function
    at Object.createHandler (/home/savnik/serverless-webpack-typescript-apollo/node_modules/serverless-offline/src/functionHelper.js:221:11)
    at handler (/home/savnik/serverless-webpack-typescript-apollo/node_modules/serverless-offline/src/ApiGateway.js:485:40)
    at module.exports.internals.Manager.execute (/home/savnik/serverless-webpack-typescript-apollo/node_modules/@hapi/hapi/lib/toolkit.js:41:33)
    at Object.internals.handler (/home/savnik/serverless-webpack-typescript-apollo/node_modules/@hapi/hapi/lib/handler.js:46:48)
    at exports.execute (/home/savnik/serverless-webpack-typescript-apollo/node_modules/@hapi/hapi/lib/handler.js:31:36)
    at Request._lifecycle (/home/savnik/serverless-webpack-typescript-apollo/node_modules/@hapi/hapi/lib/request.js:312:68)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Request._execute (/home/savnik/serverless-webpack-typescript-apollo/node_modules/@hapi/hapi/lib/request.js:221:9)

The objective is to create an apollo graphql api which uses typescript and can be used offline for development purpose.

I have reproduced the issue here: https://github.com/savnik/serverless-webpack-typescript-apollo

Any thoughts on what is the root cause for this issue?

Peter Savnik
  • 763
  • 1
  • 8
  • 27
  • Did you ever find an answer for this? I'm having the same issue... – Edy Bourne Dec 28 '20 at 01:26
  • Yes the problem is a stated in the comment of pd76 a issue with `exports.graphqlHandler = server.createHandler();` – Peter Savnik Dec 30 '20 at 06:59
  • 1
    Oh, OK.. for me that wasn't the issue. I was exporting an async function call, so the exported thing was not a function but a promise instead. Thanks anyways! – Edy Bourne Dec 30 '20 at 15:32

1 Answers1

2

This is because you are still using 'export' syntax for a js module.

In src/handler.ts, change the last line from exports.graphqlHandler = server.createHandler() to export const graphqlHandler = server.createHandler()

pd76
  • 36
  • 2