定制 xaddax/webonyx-psr15-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

xaddax/webonyx-psr15-middleware

最新稳定版本:v15.5.0

Composer 安装命令:

composer require xaddax/webonyx-psr15-middleware

包简介

PSR-15 middleware for webonyx/graphql-php

README 文档

README

A framework-agnostic PSR-15 middleware for handling GraphQL requests using webonyx/graphql-php.

codecov

Features

  • PSR-15 compliant middleware
  • Framework agnostic
  • Schema caching support
  • Request preprocessing capabilities
  • Flexible response handling
  • Operation-specific resolvers with dependency injection support
  • Resolver documentation

Documentation

Installation

composer require xaddax/webonyx-psr15-middleware

Requirements

  • PHP 8.3+
  • PSR-7 implementation (e.g., nyholm/psr7)
  • PSR-17 HTTP factories implementation
  • PSR-6 cache implementation (optional, e.g., symfony/cache)

Basic Usage

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

Schema Generation

The library includes support for generating schemas from .graphql files:

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

Framework Integration

Slim 4

use Slim\Factory\AppFactory; $app = AppFactory::create(); $app->add($middlewareFactory->createMiddleware());

Mezzio

use Mezzio\Application; $app = new Application(); $app->pipe($middlewareFactory->createMiddleware());

Laravel

use Illuminate\Support\ServiceProvider; class GraphQLServiceProvider extends ServiceProvider { public function boot() { $middleware = $this->app->make(GraphQLMiddlewareFactory::class) ->createMiddleware(); $this->app['router']->middleware('graphql', $middleware); } }

Request Preprocessing

You can add authentication/authorization by implementing the RequestPreprocessorInterface:

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

Development

Code Quality

The project uses PHP_CodeSniffer for coding standards and PHPStan for static analysis.

To check coding standards:

composer cs-check

To automatically fix coding standards:

composer cs-fix

To run static analysis:

composer stan

To run all checks (coding standards, static analysis, and tests):

composer check

Testing

To run the test suite:

composer test

Examples

Check the examples/ directory for complete working examples with different frameworks:

  • examples/slim/ - Slim 4 integration
  • examples/mezzio/ - Mezzio integration
  • examples/laravel/ - Laravel integration

License

MIT License. See LICENSE file for details.

==========================

To use the middleware in Laminas Mezzio, configure the factories

in config/autoload/dependencies.global.php

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

Add configuration in config/autoload/graphql.global.php

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, ], ], ];

see the WebOnyx Server Configuration Documentation for the full options for the server config.

You'll need to set up the route. In config/routes.php

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

Schema Definition Language

You can also use a Schema Definition Language as discussed in the webonyx documentation.

In config/autoload/graphql.global.php change the schema in the serverConfig to generatedSchema

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

Then inside of the graphQL config add the generatedSchema configuration

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', ], ], ], ];

See the documentation for parserOptions

The cached data is stored in data/cache/graphql.

统计信息

  • 总下载量: 1.22k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固