Skip to content

Commit

Permalink
Merge pull request apollographql#330 from soda0289/patch-1
Browse files Browse the repository at this point in the history
Update Lambda README to include more examples
  • Loading branch information
DxCx authored Mar 22, 2017
2 parents 37ae74c + e5de3d8 commit 93eb5ef
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 93eb5ef

Please sign in to comment.