-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1.0.0 | ||
|
||
Improve ux for generated code | ||
|
||
- Upgrade base lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# axtiva/flexible-graphql-bundle | ||
|
||
# Get started | ||
|
||
## Example project | ||
|
||
Open example project [axtiva/example-integration/FlexibleGraphqlBundle](https://github.com/axtiva/example-integration/tree/master/FlexibleGraphqlBundle) | ||
|
||
## Composer install in your Symfony app | ||
|
||
``` | ||
composer require axtiva/flexible-graphql-bundle | ||
``` | ||
|
||
## Add bundle config | ||
|
||
Create bundle config: | ||
|
||
```yaml | ||
# 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 | ||
``` | ||
## Run type registry code generation | ||
``` | ||
bin/console flexible_graphql:generate-type-registry | ||
``` | ||
|
||
## Create GraphQL Controller | ||
|
||
Here we use [symfony/psr-http-message-bridge](https://github.com/symfony/psr-http-message-bridge) | ||
for convert Symfony request to psr Request with [nyholm/psr7](https://github.com/Nyholm/psr7) | ||
|
||
```php | ||
<?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); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters