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 GraphQL\Middleware\Config\DefaultConfiguration;
use GraphQL\Middleware\Factory\GeneratedSchemaFactory;

// Configure schema generation
$schemaConfig = new DefaultConfiguration([
    'schema' => [
        'directories' => [__DIR__ . '/schema'],
        'parser_options' => [],
    ],
    'cache' => [
        'enabled' => true,
        'directory' => __DIR__ . '/cache',
    ],
]);

// Create schema factory
$schemaFactory = new GeneratedSchemaFactory($schemaConfig);
$schema = $schemaFactory->createSchema();

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()
);

return [
    'dependencies' => [
        'factories'  => [
            \GraphQL\Server\StandardServer::class => \Xaddax\GraphQL\Factory\StandardServerFactory::class,
            \GraphQL\Middleware\GraphQLMiddleware::class => \Xaddax\GraphQL\Factory\GraphQLMiddlewareFactory::class,
        ],
    ],
];

return [
    'graphQL' => [
        'middleware' => [
            'allowedHeaders' => [
                'application/graphql',
                'application/json',
            ],
        ],
        'schema' => \Path\To\Schema::class, // optional, defaults to webonyx Schema
        'schemaConfig' => [], // optional, if not configured expected in Schema class constructor
        'server' => \Path\To\Server::class, // not yet implemented, defaults to webonyx StandardServer
        'serverConfig' => [
            'context' => \GraphQL\Context\TokenContext::class
            'schema' => \Path\To\Your\Schema::class, 
        ],
    ],
];

return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void {
    $app->post('/graphql', \GraphQL\Middleware\GraphQLMiddleware::class, 'graphql');
};

return [
    'graphQL' => [
        'serverConfig' => [
            'schema' => 'generatedSchema',
        ],
    ],
];

return [
    'graphQL' => [
        'generatedSchema' => [
            'parserOptions' => [
                'experimentalFragmentVariables' => true, // to parse fragments
                'noLocation' => false, // default, set true for development
            ],
            'cache' => [
                'alwaysEnabled' => false, // default, set to true to cache when the system cache is not enabled
                'directoryChangeFilename' => 'directory-change-cache.php', // default
                'schemaCacheFilename' => 'schema-cache.php', // default 
            ],
            'schemaDirectories' => [
                '/full/path/to/schema-directory-1',
                '/full/path/to/schema-directory-2',
            ],
        ],
    ],
];