From e5de3d87099f8aa86590c491c209c2de5fd6c326 Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Mon, 20 Mar 2017 21:23:24 -0500 Subject: [PATCH] Update Lambda README to include more examples This commit adds examples for modifying the response headers, to enable CORS, and how to read the event and context variables using an options function --- packages/graphql-server-lambda/README.md | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/packages/graphql-server-lambda/README.md b/packages/graphql-server-lambda/README.md index 3be14106..79d0a13c 100755 --- a/packages/graphql-server-lambda/README.md +++ b/packages/graphql-server-lambda/README.md @@ -83,3 +83,64 @@ aws cloudformation deploy \ --stack-name prod \ --capabilities CAPABILITY_IAM ``` + +## Access or Modify Lambda options +### Read API Gateway event and Lambda Context +To read information about the current request (HTTP headers, HTTP method, body, path, ...) or the current Lambda Context (Function Name, Function Version, awsRequestId, time remaning, ...) use the options function. This way they can be passed to your schema resolvers using the context option. +```js +var server = require("graphql-server-lambda"), + myGraphQLSchema = require("./schema"); + +exports.graphqlHandler = server.graphqlLambda((event, context) => { + const headers = event.headers, + functionName = context.functionName; + + return { + schema: myGraphQLSchema, + context: { + headers, + functionName, + event, + context + } + + }; +}); +``` +### Modify the Lambda Response (Enable CORS) +To enable CORS the response HTTP headers need to be modified. To accomplish this pass in a callback filter to the generated handler of graphqlLambda. +```js +var server = require("graphql-server-lambda"), + myGraphQLSchema = require("./schema"); + +exports.graphqlHandler = function(event, context, callback) { + const callbackFilter = function(error, output) { + output.headers['Access-Control-Allow-Origin'] = '*'; + callback(error, output); + }; + const handler = server.graphqlLambda({ schema: myGraphQLSchema }); + + return handler(event, context, callbackFilter); +}; +``` +To enable CORS response for requests with credentials (cookies, http authentication) the allow origin header must equal the request origin and the allow credential header must be set to true. +```js +const CORS_ORIGIN = "https://example.com"; + +var server = require("graphql-server-lambda"), + myGraphQLSchema = require("./schema"); + +exports.graphqlHandler = function(event, context, callback) { + const requestOrigin = event.headers.origin, + callbackFilter = function(error, output) { + if (requestOrigin === CORS_ORIGIN) { + output.headers['Access-Control-Allow-Origin'] = CORS_ORIGIN; + output.headers['Access-Control-Allow-Credentials'] = 'true'; + } + callback(error, output); + }; + const handler = server.graphqlLambda({ schema: myGraphQLSchema }); + + return handler(event, context, callbackFilter); +}; +```