PHP code example of xaddax / webonyx-psr15-middleware
1. Go to this page and download the library: Download xaddax/webonyx-psr15-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xaddax / webonyx-psr15-middleware example snippets
use GraphQL\Server\ServerConfig;
use GraphQL\Type\Schema;
use Nyholm\Psr7\Factory\Psr17Factory;
use GraphQL\Middleware\Factory\GraphQLMiddlewareFactory;
// Create PSR-17 factories
$psr17Factory = new Psr17Factory();
// Configure your GraphQL schema and server
$serverConfig = new ServerConfig();
$serverConfig->setSchema($schema);
// Create the middleware factory
$middlewareFactory = new GraphQLMiddlewareFactory(
serverConfig: $serverConfig,
responseFactory: $psr17Factory,
streamFactory: $psr17Factory
);
// Create and add the middleware to your application
$middleware = $middlewareFactory->createMiddleware();
use Slim\Factory\AppFactory;
$app = AppFactory::create();
$app->add($middlewareFactory->createMiddleware());
use Mezzio\Application;
$app = new Application();
$app->pipe($middlewareFactory->createMiddleware());
use Illuminate\Support\ServiceProvider;
class GraphQLServiceProvider extends ServiceProvider
{
public function boot()
{
$middleware = $this->app->make(GraphQLMiddlewareFactory::class)
->createMiddleware();
$this->app['router']->middleware('graphql', $middleware);
}
}
use GraphQL\Middleware\RequestPreprocessorInterface;
class AuthPreprocessor implements RequestPreprocessorInterface
{
public function process(ServerRequestInterface $request): ServerRequestInterface
{
$token = $request->getHeaderLine('Authorization');
if (!$this->isValidToken($token)) {
throw new UnauthorizedException('Invalid token');
}
return $request;
}
}
// Add to middleware factory
$middlewareFactory = new GraphQLMiddlewareFactory(
serverConfig: $serverConfig,
responseFactory: $psr17Factory,
streamFactory: $psr17Factory,
requestPreprocessor: new AuthPreprocessor()
);