radek-bruha / nette-graphqlite
Nette Framework GraphQLite Integration Extension
1.1.0
2020-10-26 10:55 UTC
Requires
- php: ^7.4
- nette/caching: ^3.0
- nette/di: ^3.0
- psr/cache: ^1.0
- psr/container: ^1.0
- thecodingmachine/graphqlite: ^4.0
Requires (Dev)
- nette/application: ^3.0
- nette/bootstrap: ^3.0
- php-coveralls/php-coveralls: ^2.4
- radek-bruha/coding-standard: ^1.0
- tracy/tracy: ^2.7
This package is auto-updated.
Last update: 2024-10-26 20:32:36 UTC
README
Usage
composer require radek-bruha/nette-graphqlite
Nette Framework Usage
Config.neon
extensions: graphQLite: Bruha\NetteGraphQLite\DependencyInjection\GraphQLiteExtension graphQLite: namespaces: - App
GraphQLitePresenter.php
<?php declare(strict_types=1); namespace App\Presenters; use GraphQL\Error\Debug; use GraphQL\Error\Error; use GraphQL\GraphQL; use Nette\Application\UI\Presenter; use Nette\Http\IResponse; use Nette\Utils\Json; use TheCodingMachine\GraphQLite\Schema; use Tracy\Debugger; /** * Class GraphQLitePresenter * * @package App\Presenters */ final class GraphQLitePresenter extends Presenter { /** * @var Schema */ private Schema $schema; /** * GraphQLitePresenter constructor * * @param Schema $schema */ public function __construct(Schema $schema) { parent::__construct(); $this->schema = $schema; } /** * */ public function actionProcess(): void { $body = Json::decode( is_string($this->getHttpRequest()->getRawBody()) ? $this->getHttpRequest()->getRawBody() : '{}', Json::FORCE_ARRAY ); $this->sendJson( GraphQL::executeQuery( $this->schema, $body['query'] ?? '', NULL, NULL, $body['variables'] ?? [] )->setErrorsHandler( static function (array $errors, callable $formatter): array { if (Debugger::$productionMode) { /** @var Error $error */ foreach ($errors as $error) { Debugger::log( is_object($error->getPrevious()) ? $error->getPrevious() : $error, 'GraphQLite' ); } } return array_map($formatter, $errors); } )->setErrorFormatter( static function (Error $error): array { $exception = $error->getPrevious(); if (is_object($exception)) { return [ 'code' => $exception->getCode(), 'message' => $exception->getMessage(), ]; } return [ 'code' => IResponse::S400_BAD_REQUEST, 'message' => $error->getMessage(), ]; } )->toArray(Debugger::$productionMode ? FALSE : Debug::RETHROW_INTERNAL_EXCEPTIONS) ); } }
GraphQLite documentation