folkloreinc/laravel-graphql

What is the format for config/graphql.php middleware array?

sadeghhosseini opened this issue · 1 comments

I have written a middleware for authentication, How can I set the middleware to intercept a graphql schema?

This is How I solved my problem.

in config/graphql.php I defined a new array:

    'sadegh_schema_middlewares' => [
        'default' => ['theAuth']
    ]

Then I added this code to the top of query function in my implementation of GraphQLController:

if (!$schema) {
$schema = config('graphql.schema');
}

    //sadegh - applying middlware
    $kernel = resolve(Kernel::class);
    $sadeghSchemaMiddlewares = config('graphql.sadegh_schema_middlewares');
    if (isset($sadeghSchemaMiddlewares[$schema])) {
        $middlewares = $sadeghSchemaMiddlewares[$schema];
        foreach ($middlewares as $middlewareName) {
            if (isset($kernel->getRouteMiddlewares()[$middlewareName])) {
                $routeMiddleware = $kernel->getRouteMiddlewares()[$middlewareName];
                $mdw = new $routeMiddleware();
                $mdw->handle($request, function($request) {});
            }
        }
    }
}
    //sadegh - applying middlware

Please if anybody has any opinion concerning this way being wrong and insecure. Let me know.