Skip to content

Commit

Permalink
Update Lambda README to include more examples
Browse files Browse the repository at this point in the history
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
  • Loading branch information
soda0289 committed Mar 21, 2017
1 parent 37ae74c commit e5de3d8
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions packages/graphql-server-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
```

0 comments on commit e5de3d8

Please sign in to comment.