I'm wondering if anyone has successfully run apollo-server-lambda on zeit now v2.
When I copy and paste the simple hello world example from apollo-server-lambda's docs I'm getting this error:
TypeError: "listener" argument must be a function
I then see it's specifically for vanilla lambdas on AWS and has a exports.handler = ... rather than module.exports = ... as is common on zeit now. However, as you'd expect switching to the latter doesn't work either and produces:
UnhandledPromiseRejectionWarning: TypeError: callback is not a function
Does anyone have any thoughts on getting this to run?
Hi! Sorry this wasn't looked at before now. For anyone else curious, we have an example here: https://github.com/zeit/now-examples/tree/master/apollo
Worth noting that the docs at docs/source/deployment/now.md still refer to Now v1
+1 for needing docs for Now v2
I'd hazard a guess since apollo-server-lambda takes in a different signature to what Zeit do under the hood with their fancy Node.js helpers, that package might not work.
Zeit seem to have removed all GraphQL examples from their guides and GitHub, which I assume can only mean they don't recommend people using Zeit Now for Apollo Server.
Here is a small example using apollo-server-micro which works:
const { ApolloServer } = require('apollo-server-micro')
const server = new ApolloServer({
typeDefs, resolvers
})
const withCors = handler => (req, res, ...args) => {
if (req.method === 'OPTIONS') return res.end()
return handler(req, res, ...args)
}
module.exports = withCors(server.createHandler())
Hi @notrab noticing your above solution, though it’s not working for me when I deploy to Zeit’s Now. Any chance you might be able to assist me? (Been at this for over 2 weeks and at my wits’ end.)
@heymartinadams I've gone ahead and created a quick example using Zeit you can find here. View demo
Essentially you need to disable the bodyParser helper with the new Now API routes.
Also apollo-server-micro should really be replaced by something similar to apollo-server-lambda but I've ran into many issues using that library instead, so this weird workaround with micro seems to be the only thing that works.
Thanks to your assistance, @notrab, was able to get it to work with CORS. Afterwards, in a moment of utter ‘duh’-ness, I realized I didn’t have to use CORS in the first place if I have both the client and server in the same project using Now. 😂 (I had been so indoctrinated to keep those two parts separate, it didn’t even occur to me to integrate them into the same project.)
Adding this to the general knowledge base: https://medium.com/ecstatic-engineering/how-to-deploy-a-graphql-server-with-prisma-to-a-serverless-platform-49c573e9b26e
Most helpful comment
Worth noting that the docs at
docs/source/deployment/now.mdstill refer to Now v1