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

Update Lambda README to include more examples #330

Merged
merged 1 commit into from
Mar 22, 2017

Conversation

soda0289
Copy link
Contributor

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.

The lack of documentation has been brought up in the original PR and in this issue:
#315

TODO:

  • Update CHANGELOG.md with your change (include reference to issue & this PR)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass

@stubailo
Copy link
Contributor

Thanks!

output.headers['Access-Control-Allow-Origin'] = '*';
callback(error, output);
};
const handler = server.graphqlLambda({ schema: myGraphQLSchema });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my implementation

set CORS_ORIGINS in lambda environment variable

CORS_ORIGINS="http://localhost:8080,http://example.com,https://example.com"

var corsOrigins = process.env.CORS_ORIGINS
// ......
server.graphqlLambda({
    //...
})(event, context, function(err, output) {
    var matchedCORS = corsOrigins
        .split(",")
        .map(function(o) { return o.trim(); })
        .filter(function(o) { return o === event.headers.origin; });
    if (matchedCORS.length > 0) {
        output.headers = output.headers || {};
        output.headers['Access-Control-Allow-Credentials'] = 'true';
        output.headers['Access-Control-Allow-Origin'] = event.headers.origin;
    }
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this example and will use it in my project. It might be a little complex for the README but it does show how to validate domain names instead of just allowing everything. I think I will add one more example that checks the origin of the request. Thanks.

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
@DxCx
Copy link
Contributor

DxCx commented Mar 22, 2017

LGTM in general, but im less familiar with aws,
@NeoReyad can you give a short review as well?

@soda0289
Copy link
Contributor Author

@NeoReyad is just my work account, I use this account more often, but wrote the integration for work. I will probably have to add some more debugging information later on since several questions still come up.

@DxCx
Copy link
Contributor

DxCx commented Mar 22, 2017

oh cool 👍
can you open an issue with the open questions so people can take a look and review / respond?
maybe the experience of the others might help you as well ;)

@DxCx DxCx merged commit 93eb5ef into apollographql:master Mar 22, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants