Lighthouse: Lighthouse 2.5 - 2.6 breaks laravel-cors

Created on 27 Nov 2018  路  13Comments  路  Source: nuwave/lighthouse

Describe the bug

Any query I do with laravel-cors I get

Argument 2 passed to Barryvdh\Cors\HandleCors::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\Pagination\LengthAwarePaginator given

It works fine in graphql-playground but not on a real request from apollo graphql.

Expected behavior

For the query to work & not get this error.

Schema

Any query or mutation gets this error.

Output/Logs

Click to expand

Argument 2 passed to Barryvdh\Cors\HandleCors::addHeaders() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Illuminate\Pagination\LengthAwarePaginator given


Environment

Lighthouse Version: 2.6
Laravel Version: 5.7
PHP Version: PHP 7.2

Additional context

Upgraded from 2.4 to 2.6 & got this error.

I went back to 2.5 & it's broke on that version as well.

question

Most helpful comment

This goes in lighthouse.php:

    /*
    |--------------------------------------------------------------------------
    | Route configuration
    |--------------------------------------------------------------------------
    |
    | Additional configuration for the route group.
    | Check options here https://lumen.laravel.com/docs/routing#route-groups
    |
    | Beware that middleware defined here runs before the GraphQL execution phase.
    | This means that errors will cause the whole query to abort and return a
    | response that is not spec-compliant. It is preferable to use directives
    | to add middleware to single fields in the schema.
    | Read more about this in the docs https://lighthouse-php.netlify.com/docs/auth.html#apply-auth-middleware
    |
    */
    'route' => [
        'prefix' => '',
        'middleware' => [
            \Barryvdh\Cors\HandleCors::class,
        ],
    ],

All 13 comments

Where did you define the middleware for your schema? This could occur when you use
schema-level/field-level middlware, which works different from global middleware defined in the config.

@spawnia I defined it in the @group directive

@group(namespace: "App\\Http\\GraphQL\\Queries", middleware: ["cors"]) {

Read https://lighthouse-php.com/2.6/api-reference/directives.html#middleware closely.

In your case you can just define middleware through the config file.

@spawnia Thanks for the response back. I guess I'm using it the old way or the way I found back then. So the @group directive doesn't really work anymore? The new docs are awesome! I'll look at trying to update it again soon.

It does work differently, however i do not recommend it. Reusing Laravel middleware is really just a crutch and can almost always be done better by a custom directive.

@spawnia Just wanted to let you know that I'm all updated. I switched out all the groups for @middleware(checks: ["auth:api"]). Using cors middleware in the config. Thanks for explaining the new way for 2.5+.

@dragonfire1119 can you post your solution with laravel-cors? I always get same error like you when I request from the graphql server.

@gstrobl Go to your Lighthouse config > under route key add 'middleware' => ['cors'],

This goes in lighthouse.php:

    /*
    |--------------------------------------------------------------------------
    | Route configuration
    |--------------------------------------------------------------------------
    |
    | Additional configuration for the route group.
    | Check options here https://lumen.laravel.com/docs/routing#route-groups
    |
    | Beware that middleware defined here runs before the GraphQL execution phase.
    | This means that errors will cause the whole query to abort and return a
    | response that is not spec-compliant. It is preferable to use directives
    | to add middleware to single fields in the schema.
    | Read more about this in the docs https://lighthouse-php.netlify.com/docs/auth.html#apply-auth-middleware
    |
    */
    'route' => [
        'prefix' => '',
        'middleware' => [
            \Barryvdh\Cors\HandleCors::class,
        ],
    ],

@dragonfire1119 @spawnia thank you guys, it works.

@dragonfire1119 @spawnia thank u its work for me on umi react framework.

@spawnia it looks that currently if I follow your proposal and add \Fruitcake\Cors\HandleCors::class to the middlewares within config/lighthouse, then it is not working for HTTP OPTIONS method. But if I swtich back to the app/Http/Kernel middlewares, everything works correctly.

I think this is not wanted behaviour.

If this is a bug within Lighthouse and it is still present within the newest version of Lighthouse, you can file a bug report.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

spawnia picture spawnia  路  4Comments

spawnia picture spawnia  路  4Comments

souljacker picture souljacker  路  3Comments

m1guelpf picture m1guelpf  路  3Comments

mikeerickson picture mikeerickson  路  3Comments