Apollo-server-lambda with additional endpoints

I’d like to use apollo-server-lambda but also serve additional routes like this

export const handler = apolloServer.createHandler({
  expressAppFromMiddleware: (middleware) => {
    const app = express();
    app.get('/my-path', (req, res) => {
      res
        .status(200)
        .send('This is an additional endpoint.')
        .end();
    });
    app.use(middleware);
    return app;
  }
});

Is this possible?

This worked for me.

const { ApolloServer } = require("apollo-server-lambda");
const express = require("express");

const typeDefs = require("./graphql/typedefs");
const resolvers = require("./graphql/resolvers");

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ event }) => ({ req: { headers: event.headers } }),
});

exports.graphqlHandler = server.createHandler({
  expressGetMiddlewareOptions: {
    cors: {
      origin: "*",
      credentials: true,
    },
  },
  expressAppFromMiddleware(middleware) {
    const app = express();

    app.get("/upload", (req, res, next) => {
      console.log("Test root!");
      res.send("Hello world REST!");
      next();
    });
    app.use(middleware);

    return app;
  },
});

How to access this endpoint? Do you have to write something in the SAM template?

I use serverless, so under events in the yml you’d add upload as a path with the method post, and then you have the other endpoint graphql with the method post. How ever you do another endpoint in whatever your using would be the way to go.

Hi eldrien,

I very interested by your experience with middleware, createdHandler and serverless.

When you say :

I use serverless, so under events in the yml you’d add upload as a path with the method post, and then you have the other endpoint graphql with the method post. How ever you do another endpoint in whatever your using would be the way to go.

Did you describe a similaire code that it , for your yml file?

upload-path-graphql:
    handler: ./src/functions/upload-path-graphql.handler
    description: test upload path appolo server
    events:
      - http:
          path: /upload
          method: post
          cors: true
      - http:
          path: /graphql
          method: post
          cors: true

Have you got an experience in Subscription graphql when you use a similar approach ?