apollographql / apollo-server

šŸŒ Ā Spec-compliant and production ready JavaScript GraphQL server that lets you develop in a schema-first way. Built for Express, Connect, Hapi, Koa, and more.
https://www.apollographql.com/docs/apollo-server/
MIT License
13.76k stars 2.03k forks source link

serverless lambda deployment fail with this error `Unable to determine event source based on event` #6072

Closed Mhm0ud closed 2 years ago

Mhm0ud commented 2 years ago

I have created a simple app to deploy the apollo server to lambda function using serverless framework, I followed this instruction My app structure as the following: image

index.js content:

const { ApolloServer, gql } = require('apollo-server-lambda');
const { ApolloServerPluginLandingPageGraphQLPlayground } = require('apollo-server-core');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,

  // By default, the GraphQL Playground interface and GraphQL introspection
  // is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`).
  //
  // If you'd like to have GraphQL Playground and introspection enabled in production,
  // install the Playground plugin and set the `introspection` option explicitly to `true`.
  introspection: true,
  plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
});

exports.handler = server.createHandler();

serverless.yml content:

# serverless.yml

service: apollo-lambda
provider:
  name: aws
  runtime: nodejs14.x
functions:
  graphql:
    # this is formatted as <FILENAME>.<HANDLER>
    handler: index.handler
    events:
    - http:
        path: /
        method: post
        cors: true
    - http:
        path: /
        method: get
        cors: true

run the following command: npm i serverless invoke local --function graphql

I get this error:

{
    "errorMessage": "Unable to determine event source based on event.",
    "errorType": "Error",
    "stackTrace": [
        "Error: Unable to determine event source based on event.",
        "    at getEventSourceNameBasedOnEvent (C:\\Users\\MHMAl\\Documents\\GitHub\\federation-gateway2\\node_modules\\@vendia\\serverless-express\\src\\event-sources\\utils.js:88:9)",
        "    at proxy (C:\\Users\\MHMAl\\Documents\\GitHub\\federation-gateway2\\node_modules\\@vendia\\serverless-express\\src\\configure.js:38:51)",
        "    at handler (C:\\Users\\MHMAl\\Documents\\GitHub\\federation-gateway2\\node_modules\\@vendia\\serverless-express\\src\\configure.js:99:12)",
        "    at C:\\Users\\MHMAl\\Documents\\GitHub\\federation-gateway2\\node_modules\\apollo-server-lambda\\dist\\ApolloServer.js:51:27"
    ]
}

it's a simple app, but it's not working.

serverless --version

Framework Core: 3.0.1
Plugin: 6.0.0
SDK: 4.3.0
abhishek199-dhn commented 2 years ago

I think since this lambda is a graphql serverless service it requires an event payload that of the HTTP request. If you try to invoke this lambda using a simple object in payload it will result in the same error. Try to hit this lambda via AWS api-gateway by setting up the trigger. It should work

glasser commented 2 years ago

That's correct; apollo-server-lambda is designed for particular AWS HTTP interfaces. For direct invocation you need to make your object match one of those formats

https://github.com/apollographql/apollo-server/pull/5789 improves the docs to explain what to do.