Schema first lib for generate php code from graphql sdl to TypeRegistry whom support webonyx/graphql-php with simple integrate to any controller.
Can easy to integrate graphql to any project, all you need is controller. Fast start with example
- Schema/SDL first code generation (look at example dir)
- Support all features from webonyx/graphql-php
- Executable directives
- Apollo Federation/Federation2 support
- Amphp v3 support for async executions
- Popular framework integration:
- Symfony axtiva/flexible-graphql-bundle
Composer install:
composer require axtiva/flexible-graphql-php
- Generate models by types in Graphql Schema example/generate_type_models.php
- Generate custom scalar resolver by Graphql Schema example/generate_scalar_resolver.php
- Generate directive resolver by Graphql Schema example/generate_directive_resolver.php
- Generate type field resolver by Graphql Schema example/generate_field_resolver.php
- Create lazy loaded TypeRegistry example/generate_type_registry.php
- Setup graphql request handler with lazy loaded TypeRegistry example/start_graphql_server.php
Up Dev server for test http Graphql requests:
php -S localhost:8080 example/start_graphql_server.phpChange schema and run example commands
Update TypeRegistry and model classes from schema and resolvers map:
php example/generate_code.phpIf you need to make field resolver, then remove AutoGenerationInterface from model CodedCurrencyType
Example:
- final class CodedCurrencyType implements AutoGenerationInterface, NodeInterface, CurrencyInterface
+ final class CodedCurrencyType implements NodeInterface, CurrencyInterface
{
public string $id;
- public int $code;
}and run php example/generate_code.php, after this you will find in example/GraphQL/Resolver/CodedCurrency/CodeResolver.php.
this is your field resolver, define him in your psr container like PsrContainerExample in example/start_graphql_server.php:
$container = new PsrContainerExample([
\Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\Query\AccountResolver::class =>
new \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\Query\AccountResolver,
+ \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\CodedCurrency\CodeResolver::class =>
+ new \Axtiva\FlexibleGraphql\Example\GraphQL\Resolver\CodedCurrency\CodeResolver,
]);Run demo app php -S localhost:8080 example/start_graphql_server.php and try request CodedCurrency.code field in query
AmpFutureAdapter bridges Amphp v3 Future objects with the
webonyx/graphql-php promise system, enabling fully non-blocking GraphQL execution.
use Axtiva\FlexibleGraphql\Executor\AmpFutureAdapter;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use Amp\Future;
$config = ServerConfig::create()
// ... schema, root value, etc.
->setPromiseAdapter(new AmpFutureAdapter());
$server = new StandardServer($config);
$promise = $server->executePsrRequest($psrRequest);
$response = null;
if ($promise->adoptedPromise instanceof Future) {
$response = $promise->adoptedPromise->await()->toArray();
}
return new JsonResponse($response);- Every field resolver that returns an Amphp
Futureis automatically wrapped viaAmp\async()by the generatedTypeRegistrywhen you use theTypeRegistryGeneratorBuilderAmphpbuilder. amphp/ampis a suggested dependency; add it explicitly when async execution is needed:
composer require amphp/amp:^3
Run tests
php vendor/bin/phpunit
Run PHPStan
php vendor/bin/phpstan analyse -c phpstan.neon