国内 / webonxy-middleware
Webonxy GraphQL 中间件
v1.0.0
2024-07-24 20:16 UTC
Requires
- php: ^8.2
- firebase/php-jwt: ^6.1.0
- laminas/laminas-cache: ^4.0
- laminas/laminas-cache-storage-adapter-filesystem: ^2.4
- laminas/laminas-servicemanager: ^4.0.0-rc2
- nesbot/carbon: ^2.16
- psr/container: ^1.0 || ^2.0
- psr/http-server-middleware: ^1.0
- psr/log: ^2.0 || ^3.0
- webonyx/graphql-php: ^15.0.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.4
- phpspec/prophecy: ~1.0
- phpunit/phpunit: ^10.4@dev
README
要在 Laminas Mezzio 中使用中间件,请配置 factories
在 config/autoload/dependencies.global.php
return [ 'dependencies' => [ 'factories' => [ \GraphQL\Server\StandardServer::class => \Xaddax\GraphQL\Factory\StandardServerFactory::class, \Zestic\GraphQL\Middleware\GraphQLMiddleware::class => \Xaddax\GraphQL\Factory\GraphQLMiddlewareFactory::class, ], ], ];
在 config/autoload/graphql.global.php
中添加配置
return [ 'graphQL' => [ 'middleware' => [ 'allowedHeaders' => [ 'application/graphql', 'application/json', ], ], 'schema' => \Path\To\Schema::class, // optional, defaults to webonxy Schema 'schemaConfig' => [], // optional, if not configured expected in Schema class constructor 'server' => \Path\To\Server::class, // not yet implemented, defaults to webonxy StandardServer 'serverConfig' => [ 'context' => \Zestic\GraphQL\Context\TokenContext::class 'schema' => \Path\To\Your\Schema::class, ], ], ];
有关服务器配置的完整选项,请参阅WebOnyx 服务器配置文档。
您需要设置路由。在 config/routes.php
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container) : void { $app->post('/graphql', \Zestic\GraphQL\Middleware\GraphQLMiddleware::class, 'graphql'); };
模式定义语言
您还可以使用WebOnxy 文档中讨论的模式定义语言。
在 config/autoload/graphql.global.php
中,将 serverConfig
中的模式更改为 generatedSchema
return [ 'graphQL' => [ 'serverConfig' => [ 'schema' => 'generatedSchema', ], ], ];
然后在 graphQL
配置中添加 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', ], ], ], ];
有关 parserOptions
,请参阅文档。
缓存数据存储在 data/cache/graphql
。