apollo-server-integrations / apollo-server-integration-aws-lambda

An integration to use AWS Lambda as a hosting service with Apollo Server
MIT License
46 stars 9 forks source link

`context` option undocumented #97

Closed trevor-scheer closed 1 year ago

trevor-scheer commented 1 year ago

No API documentation or examples of usage in the README for the context option.

s10mcow commented 1 year ago

Ok so having a look at the code it seems that the third param will be our savior - options.

export const graphqlHandler = startServerAndCreateLambdaHandler(
  server,
  // We will be using the Proxy V2 handler
  handlers.createAPIGatewayProxyEventV2RequestHandler(),
  {
    context: async () => ({ prisma }),
  }
);

If you cmd + click on the startSeverAndCreateLambdaHandler in vscode it will take you to the method definition.

export function startServerAndCreateLambdaHandler<
  RH extends RequestHandler<any, any>,
>(
  server: ApolloServer<BaseContext>,
  handler: RH,
  options?: LambdaHandlerOptions<RH, BaseContext>,
): LambdaHandler<RH>;

as you can see here options are defined as so...

export interface LambdaHandlerOptions<
  RH extends RequestHandler<any, any>,
  TContext extends BaseContext,
> {
  middleware?: Array<MiddlewareFn<RH>>;
  context?: ContextFunction<[LambdaContextFunctionArgument<RH>], TContext>;
}
lizacodes commented 1 year ago

@s10mcow Thank you for setting me on the right path. It still took me longer than I'd like to admit to find the correct types for my use-case. I've raised a PR to update the README to include an example of using the context with types.