I followed the directions at Apollo Server for deploying as AWS lambda. https://www.apollographql.com/docs/apollo-server/deployment/lambda/ used the serverless framework and it is working fine in region east-2.
I extended the example to use a PostGres DB for queries (I used the npm sequalize package). The same code is used fine when I run as an ApolloServer and a local postresql DB. I made it so that it can also switch to an apollo-server-lambda. I have an if statement that changes the connection to the DB depending on if it is a lambda or not.
The issue I have is that queries that do not hit the DB work fine. But graphql queries to the DB return:
{
"error": {
"message": "Internal server error"
}
}
OK, So now how do I debug a nodejs lambda function?
The lambda management console does have a test operation. I redefiend the hello world test to use this as the test:
{
"operationName": null,
"variables": {},
"query": "{users {id firstName lastName addressNumber streetName city email createdAt updatedAt }}"
}
But that does not seem to be the right way to invoke the lambda function. Because the log returns:
{
"body": "Apollo Server supports only GET/POST requests.",
"statusCode": 405,
"headers": {
"Allow": "GET, POST"
}
}
server.js
const { ApolloServer } = require('apollo-server')
const { ApolloServer: ApolloServerLambda } = require('apollo-server-lambda')
const { typeDefs, resolvers, connect } = require('./schema.js')
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
async function setup(server) {
let { url } = await server.listen()
console.log(`???? Server ready at ${url}`)
await connect("local")
}
async function awsSetup() {
await connect("aws")
}
if (process.env.USERNAME == 'ysg4206') {
const server = new ApolloServer({ typeDefs, resolvers })
setup(server)
} else {
const server = new ApolloServerLambda({ typeDefs, resolvers })
//awsSetup()
exports.graphqlHandler = server.createHandler({
playground: true,
introspection: true,
cors: {
origin: '*',
credentials: true,
},
context: ({ event, context }) => { return (
{
headers: event.headers,
functionName: context.functionName,
event,
context
})
}
})
}
schema.js
const { gql } = require('apollo-server')
const { DB } = require('./db')
const { GraphQLDateTime } = require('graphql-iso-date')
exports.typeDefs = gql`
scalar DateTime
type User {
id: Int
"English First Name"
firstName: String
lastName: String
addressNumber: Int
streetName: String
city: String
email: String
createdAt: DateTime
updatedAt: DateTime
}
input UserType {
"Hebrew First Name"
firstName: String
lastName: String
addressNumber: Int
streetName: String
city: String
email: String
}
type Query {
users: [User]
findUser(firstName: String): User
hello(reply: String): String
}
type Mutation {
addUser(user: UserType): User!
}
type Subscription {
newUser: User!
}
`
exports.resolvers = {
Query: {
// users: async () => {
// let users = await DB.findAll()
// return users
// },
users: () => DB.findAll(),
findUser: async (_, { firstName }) => {
let who = await DB.findFirst(firstName)
return who
},
hello: (_, { reply }, context) => {
console.log(`hello with reply ${reply}`)
console.log(`context : ${JSON.stringify(reply, null, 4)}`)
return reply
}
},
Mutation: {
addUser: async (_, args) => {
let who = await DB.addUser(args.user)
return who
}
}
}
exports.connect = async function connect(where) {
await DB.dbSetup(where)
await DB.populate()
let users = await DB.findAll()
console.log(users)
}