Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

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

Open
sadeghhosseini opened this issue Oct 23, 2018 · 1 comment
Open

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

sadeghhosseini opened this issue Oct 23, 2018 · 1 comment

Comments

@sadeghhosseini
Copy link

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

@sadeghhosseini
Copy link
Author

sadeghhosseini commented Oct 24, 2018

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant