0

I would like to add something like this to all my queries and mutations

type Query @middleware(checks: ["moduleCheck", "moduleScope"]){
  //
}

Is there a way to run middleware globally?

I can add it to the $middleware variable in Http\Kernal.php but then my middleware won't respond with the required queries set to null, which throws a different error.

Edward Louth
  • 1,010
  • 2
  • 9
  • 19
  • Could you tell what you are trying to achieve with the global middleware? Lighthouse works by adding a route to Laravel routes, you can disable the default and register your own, by doing this, you can add all route middlewares you want. However it's a different story if you want to add the middlewares from lighthouse to all methods. – Oliver Nybroe Dec 10 '19 at 16:21
  • I would like to do the second, for example if I add the auth middleware to the route then you won't get graphql compliant error messages whereas if you add it as lighthouse middleware it is caught and converted. I would like to do the same with my middleware, being able to throw exceptions that are caught by lighthouse and converted to graphql compliant errors. – Edward Louth Dec 12 '19 at 00:29

1 Answers1

0

You can try to put it inside middleware config in lighthouse.php like so

'middleware' => [

        \Nuwave\Lighthouse\Support\Http\Middleware\AcceptJson::class,

        // Logs in a user if they are authenticated. In contrast to Laravel's 'auth'
        // middleware, this delegates auth and permission checks to the field level.
        \Nuwave\Lighthouse\Support\Http\Middleware\AttemptAuthentication::class,

         \App\Http\Middleware\YourMiddlewareGoesHere::class,
    ],
  • I am looking to add a middleware to all of my queries, the same way as using the `@middleware` directive. It is somewhat confusing that these are also called middleware and have the same signature but they are run at a different point to the lighthouse.php middleware. The critical difference is to do with whether errors are caught and graphQL errors returned or http errors. – Edward Louth Sep 11 '20 at 06:27