Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apollo Server #56

Closed
1 of 4 tasks
H4ad opened this issue Nov 17, 2022 · 6 comments · Fixed by #65
Closed
1 of 4 tasks

Apollo Server #56

H4ad opened this issue Nov 17, 2022 · 6 comments · Fixed by #65
Assignees
Labels
enhancement New feature or request released

Comments

@H4ad
Copy link
Owner

H4ad commented Nov 17, 2022

Feature request

Inspired apollo-server-integrations/apollo-server-integration-aws-lambda#38.

Is your feature request related to an issue? Please describe.
Added support for Apollo Server as Framework/Handler.

Describe the solution you would like

In theory, we already have support for Apollo Server if we use express as middleware.
But maybe we can do better, the solutions are:

  • Add support for Apollo Server as Framework.
  • Add support for Apollo Server as Handler, so you don't need to create another Request/Response.

In both ways, I don't exactly know how to deal with cors, maybe I can create a CorsFramework to wrap around any other framework to add cors features.

Are you willing to resolve this issue by submitting a pull request?

  • Yes, I have time and I know how to start.
  • Yes, I have time, but I don't know how to start. I would need guidance.
  • No, I don't have time, although I believe I could do it if I had time...
  • No, I don't have time and I wouldn't even know how to start.
@H4ad H4ad added the enhancement New feature or request label Nov 17, 2022
@H4ad H4ad self-assigned this Nov 17, 2022
@H4ad
Copy link
Owner Author

H4ad commented Nov 17, 2022

I did a proof of concept using the express implementation inside Apollo Server and I create apollo-server.framework.ts.

If you want to give it a try, just create the apollo-server.framework.ts file with the contents of the above file and use it like:

import { IncomingMessage, ServerResponse } from 'http';
import { ApolloServer } from '@apollo/server';
import { ServerlessAdapter, getCurrentInvoke } from '@h4ad/serverless-adapter';
import { DefaultHandler } from '@h4ad/serverless-adapter/lib/handlers/default';
import { PromiseResolver } from '@h4ad/serverless-adapter/lib/resolvers/promise';
import {
  ApiGatewayV1Adapter,
  ApiGatewayV2Adapter,
} from '@h4ad/serverless-adapter/lib/adapters/aws';
import { ApolloServerFramework } from '@h4ad/serverless-adapter/lib/frameworks/apollo-server/apollo-server';

type CustomContext = {
  request: IncomingMessage;
  response: ServerResponse;
  lambdaEvent: unknown;
  lambdaContext: unknown;
};

const server = new ApolloServer<CustomContext>({
  typeDefs: 'type Query { x: ID }',
  resolvers: { Query: { x: () => 'hi!' } },
});

const framework = new ApolloServerFramework<CustomContext>({
  context: ({ request, response }) => {
    const { event, context } = getCurrentInvoke();

    return Promise.resolve({
      request: request,
      response: response,
      lambdaEvent: event,
      lambdaContext: context,
    });
  },
});

server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();

// this handler add support for API Gateway v1 and v2, to add another event source, just call addAdapter.
export const handler = ServerlessAdapter.new(server)
  .setHandler(new DefaultHandler())
  .setFramework(framework)
  .setResolver(new PromiseResolver())
  .addAdapter(new ApiGatewayV1Adapter())
  .addAdapter(new ApiGatewayV2Adapter())
  .build();

@H4ad
Copy link
Owner Author

H4ad commented Nov 17, 2022

I still need to figure out how to handle cors.

In theory, if I recommend the user to configure within API Gateway is not a problem, but we also have to add support for other clouds, this is not an option.

@H4ad
Copy link
Owner Author

H4ad commented Nov 19, 2022

The problem with cors was solved with CorsFramework

@H4ad
Copy link
Owner Author

H4ad commented Nov 19, 2022

I'll wait until I have more time to work on #62, after I finish that integration I'll see how it works with Apollo.

For those who want to use Apollo and don't want to wait, just copy the code from apollo-server.framework.ts, install the latest version of the library and be happy.

@H4ad
Copy link
Owner Author

H4ad commented Nov 26, 2022

Once the body parser frameworks have been completed, I will now work on completing the Apollo server integration.

@H4ad H4ad mentioned this issue Nov 26, 2022
8 tasks
@H4ad H4ad closed this as completed in #65 Dec 3, 2022
@github-actions
Copy link

github-actions bot commented Dec 3, 2022

🎉 This issue has been resolved in version 2.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant