typescriptwebpackapollo-serverserverless-offline

Serverless-offline does not recognize graphql handler as a function


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?


Solution

  • 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()