player259 / graphql-bundle
Lightweight Symfony GraphQL bundle over webonyx/graphql-php with DI and autowiring
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 1
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.1.3|^8.0
- ext-json: *
- nyholm/psr7: ^1.2
- psr/log: ^1|^2
- symfony/config: ^4.4|^5.0
- symfony/dependency-injection: ^4.4|^5.0
- symfony/http-kernel: ^4.4|^5.0
- symfony/property-access: ^4.4|^5.0
- symfony/psr-http-message-bridge: ^2.0
- webonyx/graphql-php: ^0.12.6|^0.13.0|^14.0
Requires (Dev)
- phpunit/phpunit: ^7.5.20|^8.0|^9.0
- slevomat/coding-standard: ^6.4
- squizlabs/php_codesniffer: ^3.5
- symfony/browser-kit: ^4.4|^5.0
- symfony/framework-bundle: ^4.4|^5.0
This package is auto-updated.
Last update: 2025-04-29 01:10:48 UTC
README
About
The Player259GraphQLBundle integrates webonyx/graphql-php library into symfony applications.
Features
- Types-as-services with Dependency Injection
- Controller-like resolvers with Autowiring
- Type definition and resolvers in the same class, see Basic usage
- No extra configuration files
- No static calls
- Native webonyx/graphql-php type system with all its features and great documentation
- Integrated Type Registry
- Possible Code splitting for Query and Mutation types
- Simplified Deferred resolving with integrated buffer
Installation
The Player259GraphQLBundle requires PHP 7.1+ and Symfony 4.4+.
You can install the bundle using Symfony Flex:
$ composer require player259/graphql-bundle
If you're not using Flex, then add the bundle to your config/bundles.php
:
// config/bundles.php return [ // ... Player259\GraphQLBundle\Player259GraphQLBundle::class => ['all' => true], ];
Import routing file:
# in app/config/routes.yaml player259_graphql: resource: '@Player259GraphQLBundle/Resources/config/routing.xml' prefix: /
Or assign endpoint to specific url:
# in app/config/routes.yaml player259_graphql_index: path: /graphql controller: Player259\GraphQLBundle\Controller\GraphQLController
By default bundle registers /graphql
endpoint.
Configuration
Default configuration in config/packages/player259_graphql.yaml
.
# in app/config/packages/player259_graphql.yml player259_graphql: debug: '%kernel.debug%' logger: '?logger'
With debug
option set to true
response errors will contain debugMessage
and trace
.
logger
parameter is a service name to log exceptions.
If it's prefixed with ?
it will not throw exception if no such service exists.
Example
To create your first GraphQL API (with default Symfony 5 installation and no configuration):
- Create class which extends webonyx
ObjectType
with nameQuery
- Add at least one field and resolver
- Make a request to
/graphql
url
<?php namespace App\GraphQL; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use Symfony\Component\Security\Core\Security; class QueryType extends ObjectType { public function __construct() { $config = [ 'name' => 'Query', 'fields' => [ 'username' => [ 'type' => Type::string(), 'description' => 'Current User username', ], ], ]; parent::__construct($config); } public function resolveUsername(Security $security): ?string { return $security->getUser() ? $security->getUser()->getUsername() : null; } }
Documentation
Usage examples can be found in documentation.
Not yet implemented
Pass execution rules, disabling introspection, query depth and complexity.
Dispatching events to override server parameters such as promiseAdapter, error formatters and handlers.
Allow to merge non-root types to get more flexibility.
Maybe custom type config property resolveMethod
to call specific method or another service.
Another option is annotations, something like @GraphQL\Resolve("App\GraphQL\QueryType", "users")
so it could be attached to any service with public method.
There will be no autowiring but it can be useful in some cases.
License
Released under the MIT License, see LICENSE.