Skip to content

Conversation

sheikalthaf
Copy link

CORS options added

/**
* graphql routes
*/
$app->post('/graphql', GraphQLController::class .':process');
Copy link
Author

Choose a reason for hiding this comment

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

Instead of string mentioning the actual class will help to navigate easily

* Please change the `origin` header according to requirement
*/
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you need this?

Copy link
Author

Choose a reason for hiding this comment

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

If I try to access from angular application then I'm getting cors error

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, now I see where is the problem :)


$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
Copy link
Owner

Choose a reason for hiding this comment

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

Here is the problem. You should not return the response, but call the next callback and return it.

$response = $response->withHeader('Access-Control-Allow-Origin', '*')
  ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
  ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');

return $next($request, $response);

If you will do it like this, you can remove $app->options('/{routes:.+}', function ...)

@juffalow juffalow added the enhancement New feature or request label Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants