-
Notifications
You must be signed in to change notification settings - Fork 2k
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
graphql-server-lambda does not able to config CORS origin #315
Comments
The graphql-server does not create CORS requests for any integration you have to handle them yourself. For API gateway you should be able to enable CORS following the instructions here: I have not tested this myself so I'm not 100% sure if it will append the correct header on output. I think it will create the OPTIONS method endpoint. If you want to change the headers that are sent from graphql-server-lambda you can do so like this: exports.graphqlHandler = function(event, context, callback) {
const callbackFilter = function(error, output) {
output.headers['Access-Control-Allow-Origin'] = '*';
callback(error, output);
};
server.graphqlLambda({ schema: myGraphQLSchema })(event, context, callbackFilter);
}; Let me know if you have any problems. |
@soda0289 However, for the actual request to lambda, the corresponding CORS header should be set by the lambda function. the code block provided by you works for me. And I think this solution is good to be documented in I could help on this in the coming weekend. |
I will look into updating the documentation. There have been a couple questions on how to read and modify headers from the handler. We might want to add CORS as a feature of server itself but that could be out of the scope of this project. |
@soda0289 Thanks for linking me over -- I missed this issue in my initial search for information. I think your sample above is a good solution as it keeps the signature as simple as possible for those not worried about custom headers. Before I posted in the other issue, I read through most of the developer documentation and saw only a few indirect references to CORS support in the current versions of Particularly with Lambda, with which setting up a Gateway on the same domain as the web server is a practical nightmare, I think there is room for improvement in the docs. Can I be of assistance in updating the documentation with your above code and a brief explanation of CORS setup? |
@jangerhofer Sure if you have some free time I'd be happy to review an update to the documentation. Are you thinking to update the graphql-sever-lambda README or the https://github.com/apollographql/tools-docs ? |
I finally got around to updating the AWS Lambda README. Let me know if you think we need more details. We still might need some more documentation on the tools website but I don't have time this week to do an update. |
Since in Node 8.X runtime the callback parameter is removed; how the output headers can be modified? |
graphql-server-lambda uses the expressjs CORS options ie: export const graphQl = server.createHandler({
cors: {
origin: '*',
methods: 'POST',
allowedHeaders: [
'Content-Type',
'Origin',
'Accept'
]
}
}); Hope this helps! |
While serving GraphQL on AWS Lambda via API Gateway,
the graphql-server-lambda not able to response with
access-control-allow-origin
in headerThis makes the
graphql-server-lambda
unable to serve CORS.The text was updated successfully, but these errors were encountered: