Skip to content

Commit

Permalink
update main lib
Browse files Browse the repository at this point in the history
  • Loading branch information
bpteam committed Mar 27, 2022
1 parent 8d89c32 commit 606e63c
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Symfony bundle for Flexible Graphql PHP

- SDL first code generation
- Fast integration to any project without breaking changes
- Lazy loading on schema definition
- Support symfony native opcache preload file generation

# Setup
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
}
},
"require": {
"php": ">=7.4",
"axtiva/flexible-graphql-php": "^0.2",
"axtiva/flexible-graphql-federation": "^0.2",
"axtiva/graphql-federation-extension": "^0.1",
"php": "^7.4 | ^8.0",
"axtiva/flexible-graphql-php": "^1.0",
"axtiva/flexible-graphql-federation": "^0.3",
"axtiva/graphql-federation-extension": "^1.0",
"symfony/config": "^4.4 | ^5.0 | ^6.0",
"symfony/dependency-injection": "^4.4 | ^5.0 | ^6.0",
"symfony/framework-bundle": "^4.4 | ^5.0 | ^6.0",
"symfony/property-access": "^4.4 | ^5.0 | ^6.0"
},
"require-dev": {
"symfony/console": "^4.4 | ^5.0 | ^6.0"
"symfony/console": "^4.4 | ^5.0 | ^6.0",
"phpunit/phpunit": "^9.5"
}
}
109 changes: 109 additions & 0 deletions docs/index.md
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);
}
}
```
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function schemaFile(): ScalarNodeDefinition
$node = $treeBuilder->getRootNode();

$node
->info('Full path to schema sdl files like schema.graphql or glob template')
->info('Full path to schema sdl files like /path/to/schema.graphql or glob template')
->defaultValue('%kernel.project_dir%/config/graphql/*.graphql')
->isRequired()
->end();
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/flexible_graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ flexible_graphql:
namespace: App\GraphQL
dir: '%kernel.project_dir%/src/GraphQL/'
schema_type: graphql
schema_file: '%kernel.project_dir%/config/graphql/*.graphql'
schema_files: '%kernel.project_dir%/config/graphql/*.graphql'
enable_preload: false
default_resolver: flexible_graphql.default_resolver

0 comments on commit 606e63c

Please sign in to comment.