### Install Flexible Graphql Bundle using Composer Source: https://github.com/axtiva/flexible-graphql-bundle/blob/master/README.md This command installs the Axtiva Flexible Graphql Bundle into your Symfony project using Composer. It fetches the package and its dependencies. ```shell composer require axtiva/flexible-graphql-bundle ``` -------------------------------- ### Generated Field Resolver Example (PHP) Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt This PHP snippet shows an example of a generated field resolver for the 'users' query. It implements the ResolverInterface and includes a placeholder for business logic, such as fetching data from a database. It demonstrates how to access arguments like 'limit'. ```php 1, 'name' => 'Alice', 'email' => 'alice@example.com'], ['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com'], ]; } } ``` -------------------------------- ### Symfony Service Configuration for Resolvers Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Example Symfony `services.yaml` configuration that enables autowiring and autoconfiguration for resolver services. It explicitly tags services within the `App\GraphQL\Resolver\` namespace with `flexible_graphql.resolver`, although this tag is often auto-detected. ```yaml # services.yaml - Resolvers are autowired automatically services: _defaults: autowire: true autoconfigure: true App\GraphQL\Resolver\: resource: '../src/GraphQL/Resolver/*' tags: - { name: 'flexible_graphql.resolver' } # Optional: auto-detected ``` -------------------------------- ### Federation Service with Entity Extension Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Defines GraphQL types for User and Product, extending existing types with federation directives. This allows for schema merging in a federated GraphQL setup, specifying fields for entity resolution and external data. ```graphql extend type Query { me: User } type User @key(fields: "id") { id: ID! username: String! email: String! } extend type Product @key(fields: "id") { id: ID! @external reviews: [Review] } type Review { id: ID! body: String! rating: Int! author: User! product: Product! } ``` -------------------------------- ### Clear Symfony Cache Source: https://github.com/axtiva/flexible-graphql-bundle/blob/master/README.md This command clears the Symfony application cache. It's often necessary after installing or configuring new bundles to ensure changes are recognized. ```shell bin/console cache:clear ``` -------------------------------- ### List Flexible Graphql Commands Source: https://github.com/axtiva/flexible-graphql-bundle/blob/master/README.md This command lists all available console commands provided by the Flexible Graphql Bundle. This is useful for discovering and understanding the bundle's functionalities. ```shell bin/console list flexible_graphql ``` -------------------------------- ### Configure Flexible Graphql Bundle Source: https://github.com/axtiva/flexible-graphql-bundle/blob/master/README.md This YAML configuration sets up the Axtiva Flexible Graphql Bundle. It defines the namespace for GraphQL models, the directory for generated files, schema type (graphql or federation), path to schema definition files, preload enablement, and the default resolver. ```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 ``` -------------------------------- ### Opcache Preload Configuration for Performance Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Configuration snippet for enabling Opcache preload within the Axtiva Flexible GraphQL Bundle. This optional setting (`enable_preload: true`) helps improve performance by preloading the `TypeRegistry` into Opcache, reducing load times. ```yaml # Opcache preload configuration (optional) # config/packages/flexible_graphql.yaml flexible_graphql: enable_preload: true # This generates entries for var/cache/[env]/preload.php # Improves performance by preloading TypeRegistry into opcache ``` -------------------------------- ### Symfony Bundle Configuration for GraphQL Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt This YAML configuration sets up the Axtiva Flexible GraphQL Bundle within a Symfony application. It defines namespaces, directories for generated code, schema types (standard GraphQL or Apollo Federation), the location of SDL files, opcache preload settings, default resolver service, template language version, and the execution mode (synchronous or asynchronous). ```yaml flexible_graphql: namespace: App\GraphQL # Root namespace for generated code dir: '%kernel.project_dir%/src/GraphQL/' # Directory where files will be saved schema_type: graphql # 'graphql' or 'federation' for Apollo Federation schema_files: '%kernel.project_dir%/config/graphql/*.graphql' # Path to SDL files (supports glob patterns) enable_preload: false # Enable Symfony opcache preload default_resolver: flexible_graphql.default_resolver # Service ID for default field resolver template_language_version: '7.4' # PHP version for generated code templates executor: sync # 'sync', 'amphp_v2', or 'amphp_v3' ``` -------------------------------- ### Generate Type Registry and All Types (Bash) Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt This command-line instruction clears the cache and manually generates the TypeRegistry and all associated GraphQL types. It reads schema SDL files and creates PHP files for types and the registry. It's typically run automatically during cache clearing. ```bash # Automatically runs during cache:clear bin/console cache:clear # Or manually generate the TypeRegistry bin/console flexible_graphql:generate-type-registry ``` -------------------------------- ### Create GraphQL Controller (PHP) Source: https://github.com/axtiva/flexible-graphql-bundle/blob/master/docs/index.md This PHP controller handles GraphQL requests for a Symfony application. It utilizes `symfony/psr-http-message-bridge` and `nyholm/psr7` to convert Symfony requests to PSR-7 compliant requests. It sets up the GraphQL schema, validation rules, and debugging flags before executing the request and returning a JSON response. ```php 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); } } ``` -------------------------------- ### Automatic Resolver Service Tagging Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Demonstrates how GraphQL resolvers implementing `ResolverInterface` are automatically tagged by the Symfony service container. This allows the bundle to discover and register resolvers without manual configuration. ```php productRepository = $productRepository; } public function __invoke($rootValue, $args, $context, ResolveInfo $info) { $limit = $args['limit'] ?? 20; $offset = $args['offset'] ?? 0; return $this->productRepository->findBy( [], ['createdAt' => 'DESC'], $limit, $offset ); } } ``` -------------------------------- ### Create GraphQL HTTP Endpoint (PHP) Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt This snippet demonstrates how to create a GraphQL HTTP endpoint in a Symfony controller. It configures the GraphQL schema, validation rules, debug flags, and integrates with PSR-7 HTTP messages. Dependencies include Symfony's HttpFoundation and PSR-7 bridges. ```php typeRegistry = $typeRegistry; $this->httpFactory = $httpFactory; } /** * @Route("/graphql", name="graphql", methods={"GET", "POST"}) */ public function index(Request $request): Response { $typeRegistry = $this->typeRegistry; // Build schema with lazy type loading $schema = new Schema([ 'query' => $typeRegistry->getType('Query'), 'mutation' => $typeRegistry->getType('Mutation'), 'typeLoader' => static function (string $typeName) use ($typeRegistry): Type { return $typeRegistry->getType($typeName); } ]); // Configure validation rules including query complexity $validationRules = array_merge( GraphQL::getStandardValidationRules(), [ new Rules\QueryComplexity(PHP_INT_MAX), ] ); // Set debug flags (adjust for production) $debugFlag = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS | DebugFlag::RETHROW_UNSAFE_EXCEPTIONS; // Convert Symfony request to PSR-7 and execute $psrRequest = $this->httpFactory->createRequest($request); $config = ServerConfig::create() ->setContext(['user' => $this->getUser()]) // Add authentication context ->setSchema($schema) ->setValidationRules($validationRules) ->setQueryBatching(true) // Enable batched queries ->setDebugFlag($debugFlag); $server = new StandardServer($config); $psrResponse = $server->executePsrRequest($psrRequest); return new JsonResponse( json_decode($psrResponse->getBody()->getContents(), true), $psrResponse->getStatusCode() ); } } ``` -------------------------------- ### Cache Warmer Integration for Schema Generation Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Illustrates the automatic code generation process during Symfony's cache warming phase. The `SchemaCacheWarmer` generates essential GraphQL type classes and the `TypeRegistry`, ensuring the schema is ready for use. ```php propertyAccessor = $propertyAccessor; } public function __invoke($rootValue, $args, $context, ResolveInfo $info) { // Try standard GraphQL resolution first $property = Executor::defaultFieldResolver($rootValue, $args, $context, $info); // Fallback to Symfony PropertyAccessor for getter methods and array access if ($property === null && $rootValue !== null) { try { return $this->propertyAccessor->getValue($rootValue, $info->fieldName); } catch (\Exception $e) { return null; } } return $property; } } ``` ```yaml # Register custom default resolver in services.yaml services: app.graphql.custom_default_resolver: class: App\GraphQL\Resolver\CustomDefaultResolver arguments: - '@property_accessor' # Reference in flexible_graphql.yaml flexible_graphql: default_resolver: app.graphql.custom_default_default_resolver ``` -------------------------------- ### Generate Field Resolver (Bash) Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt This command generates a PHP resolver class for a specific GraphQL field, such as 'users' on the 'Query' type. It takes the parent type and field name as arguments and creates a boilerplate resolver file that you can then implement with your business logic. ```bash # Generate a resolver for a specific field bin/console flexible_graphql:generate-field-resolver Query users # Example: For this SDL definition # type Query { # users(limit: Int): [User] # } # # Generates: src/GraphQL/Resolver/Query/UsersResolver.php ``` -------------------------------- ### Federation User Entity Resolver Implementation Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Implements the `FederationRepresentationResolverInterface` to resolve User entities in a federated GraphQL service. This PHP resolver fetches user data based on the provided representation (e.g., user ID) and returns it in a structured format. ```php $userId, 'username' => 'john_doe', 'email' => 'john@example.com', ]; } public function getTypeName(): string { return 'User'; } } ``` -------------------------------- ### Generate Directive Resolver Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Generates a PHP class for a directive resolver, typically used for field-level authorization or other cross-cutting concerns. The resolver implements the DirectiveResolverInterface and receives arguments defined in the directive schema. It allows you to intercept field resolutions and perform actions before or after the actual resolver. ```bash # For an executable directive: directive @auth(role: String!) on FIELD_DEFINITION bin/console flexible_graphql:generate-directive-resolver auth ``` ```php hasRole($requiredRole)) { throw new Error('Unauthorized access'); } // Continue to the actual field resolver return $next($rootValue, $args, $context, $info); } } ``` -------------------------------- ### Generate Custom Scalar Resolver Source: https://context7.com/axtiva/flexible-graphql-bundle/llms.txt Generates a basic PHP class for a custom scalar resolver. You need to implement the logic for serializing, parsing values, and parsing literals based on your scalar's requirements. This is useful for handling specific data types like DateTime. ```bash bin/console flexible_graphql:generate-scalar-resolver DateTime ``` ```php format(\DateTime::ISO8601); } throw new Error('DateTime cannot represent non DateTime value: ' . json_encode($value)); } public function parseValue($value) { // Convert external input to internal representation try { return new \DateTime($value); } catch (\Exception $e) { throw new Error('DateTime cannot represent value: ' . $value); } } public function parseLiteral(Node $valueNode, ?array $variables = null) { // Parse literal AST value if ($valueNode instanceof \GraphQL\Language\AST\StringValueNode) { return $this->parseValue($valueNode->value); } throw new Error('DateTime cannot represent non-string value'); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.