Open example project axtiva/example-integration/FlexibleGraphqlBundle
composer require axtiva/flexible-graphql-bundle
Create bundle config:
# content of config/packages/flexible_graphql.yaml
flexible_graphql:
namespace: App\GraphQL # namespace where store GraphQL models and resolvers
dir: '%kernel.project_dir%/src/GraphQL/' # path where it will be they save files
schema_type: graphql # type of schema generation. Default is `graphql` or optional is `federation` for apollo federation support
schema_files: '%kernel.project_dir%/config/graphql/*.graphql' # path to graphql schema SDL files
enable_preload: false # use Symfony preload if it true
default_resolver: flexible_graphql.default_resolver # default resolver if it does not defined
services:
Psr\Container\ContainerInterface: '@service_container'
bin/console cache:clear
Here we use symfony/psr-http-message-bridge for convert Symfony request to psr Request with nyholm/psr7
<?php
namespace App\Controller;
use App\GraphQL\TypeRegistry;
use GraphQL\GraphQL;
use GraphQL\Validator\Rules;
use GraphQL\Error\DebugFlag;
use GraphQL\Server\ServerConfig;
use GraphQL\Server\StandardServer;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class GraphqlController extends AbstractController
{
private TypeRegistry $typeRegistry;
private HttpMessageFactoryInterface $httpFactory;
public function __construct(
TypeRegistry $typeRegistry,
HttpMessageFactoryInterface $httpFactory
) {
$this->typeRegistry = $typeRegistry;
$this->httpFactory = $httpFactory;
}
/**
* @Route("/graphql", name="graphql")
*/
public function index(Request $request): Response
{
$typeRegistry = $this->typeRegistry;
$schema = new Schema([
'query' => $typeRegistry->getType('Query'),
'mutation' => $typeRegistry->getType('Mutation'),
'typeLoader' => static function (string $typeName) use ($typeRegistry): Type {
return $typeRegistry->getType($typeName);
}
]);
$validationRules = array_merge(
GraphQL::getStandardValidationRules(),
[
new Rules\QueryComplexity(PHP_INT_MAX),
]
);
$debugFlag = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS | DebugFlag::RETHROW_UNSAFE_EXCEPTIONS;
$psrRequest = $this->httpFactory->createRequest($request);
$config = ServerConfig::create()
->setContext(['user' => $this->getUser()])
->setSchema($schema)
->setValidationRules($validationRules)
->setQueryBatching(true)
->setDebugFlag($debugFlag)
;
$server = new StandardServer($config);
$psrResponse = $server->executePsrRequest($psrRequest);
return new JsonResponse($psrResponse);
}
}
And start querying data by route /graphql