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

Create Cookies in Resolvers using AWS Lambda Integration #2554

Closed
Nosherwan opened this issue Mar 8, 2023 · 2 comments · Fixed by #2560
Closed

Create Cookies in Resolvers using AWS Lambda Integration #2554

Nosherwan opened this issue Mar 8, 2023 · 2 comments · Fixed by #2560

Comments

@Nosherwan
Copy link

Nosherwan commented Mar 8, 2023

Is your feature request related to a problem? Please describe.

Hi I am using the graphql-yoga integration with AWS Lambda sample from below link 👇🏼
https://the-guild.dev/graphql/yoga-server/docs/integrations/integration-with-aws-lambda

I am unable to access the response object in the context function. I would like to access it in the AWS Lambda Integration so it can be passed onto the resolvers.

Describe the solution you'd like

const yoga = createYoga<{
    event: APIGatewayEvent;
    lambdaContext: Context;
}>({
    // specify the path to the lambda function
    graphqlEndpoint: '/graphql',
    schema: createSchema({
        typeDefs,
        resolvers,
    }),
    context: async (params: any) => {
        const { request, response } = params;
        // response is not there 👆🏼
        const cookies = request.headers.get('Cookies');
        // response needs to be passed to resolvers,
        // so that a cookie can be created and attached to it in there
        return { request, response };
    },
});

I want to grab the response object in the context function, which I believe is available in node.js environment, but not sure if it is available in the lambda env or not.
If not is there a way to send cookies with the actual response after generating them from within a resolver?

Describe alternatives you've considered

I could potentially use the node.js version of graphql-yoga with lambda integration so that I have access to the response object in addition to the request. Is that possible?

@CarlosVilasAlvarez
Copy link

CarlosVilasAlvarez commented Mar 9, 2023

Maybe something like this? I have not tested it but you get the idea.

import { createYoga, createSchema } from 'graphql-yoga'
import { APIGatewayEvent, APIGatewayProxyResult, Context } from 'aws-lambda'

type ResponseHeaders = Record<string, string>

const yoga = createYoga<{
  event: APIGatewayEvent
  lambdaContext: Context
  responseHeaders: ResponseHeaders
}>({
  graphqlEndpoint: '/graphql',
  landingPage: false,
  schema: createSchema({
    typeDefs: /* GraphQL */ `
      type Query {
        greetings: String
      }
    `,
    resolvers: {
      Query: {
        greetings: (_, _args, { request, params, responseHeaders, event, lambdaContext }) => {
            responseHeaders['Cookies'] = 'test';
            return 'This is the `greetings` field of the root `Query` type';
        },
      },
    },
  }),
  context({ event, lambdaContext, request, responseHeaders }) {
    return {
        event, lambdaContext, request, responseHeaders
    }
  }
})

export async function handler(
  event: APIGatewayEvent,
  lambdaContext: Context,
): Promise<APIGatewayProxyResult> {
  const responseHeaders: ResponseHeaders = {};

  const response = await yoga.fetch(
    event.path +
      '?' +
      new URLSearchParams(
        (event.queryStringParameters as Record<string, string>) || {},
      ).toString(),
    {
      method: event.httpMethod,
      headers: event.headers as HeadersInit,
      body: event.body
        ? Buffer.from(event.body, event.isBase64Encoded ? 'base64' : 'utf8')
        : undefined,
    },
    {
      event,
      lambdaContext,
      responseHeaders,
    },
  )

  response.headers.forEach((value, name) => {
    responseHeaders[name] = value
  })

  return {
    statusCode: response.status,
    headers: responseHeaders,
    body: await response.text(),
    isBase64Encoded: false,
  }
}

@ardatan ardatan mentioned this issue Mar 12, 2023
@Nosherwan
Copy link
Author

@ardatan I am assuming @whatwg-node/server-plugin-cookies plugin is not available yet, but is ready to be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants