Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
62 / 62 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| GraphqlSchemaFactory | |
100.00% |
62 / 62 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSchema | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| createMutations | |
100.00% |
28 / 28 |
|
100.00% |
1 / 1 |
7 | |||
| createQuery | |
100.00% |
29 / 29 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Graphql\Factories; |
| 3 | |
| 4 | use Apie\Common\ActionDefinitionProvider; |
| 5 | use Apie\Common\ActionDefinitions\CreateResourceActionDefinition; |
| 6 | use Apie\Common\ActionDefinitions\DownloadFilesActionDefinition; |
| 7 | use Apie\Common\ActionDefinitions\GetResourceActionDefinition; |
| 8 | use Apie\Common\ActionDefinitions\GetResourceListActionDefinition; |
| 9 | use Apie\Common\ActionDefinitions\ReplaceResourceActionDefinition; |
| 10 | use Apie\Common\Actions\CreateObjectAction; |
| 11 | use Apie\Core\ApieLib; |
| 12 | use Apie\Core\BoundedContext\BoundedContext; |
| 13 | use Apie\Core\Context\ApieContext; |
| 14 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 15 | use Apie\Graphql\TypeResolvers\ApieCallTypeResolver; |
| 16 | use Apie\Graphql\TypeResolvers\SearchObjectTypeResolver; |
| 17 | use Apie\Graphql\Types; |
| 18 | use Apie\Graphql\Types\SearchObjectType; |
| 19 | use GraphQL\Type\Definition\ObjectType; |
| 20 | use GraphQL\Type\Definition\Type; |
| 21 | use GraphQL\Type\Schema; |
| 22 | |
| 23 | class GraphqlSchemaFactory |
| 24 | { |
| 25 | public function __construct( |
| 26 | private readonly ActionDefinitionProvider $actionDefinitionProvider, |
| 27 | ) { |
| 28 | } |
| 29 | public function createSchema(ApieContext $apieContext): Schema |
| 30 | { |
| 31 | return new Schema([ |
| 32 | 'query' => $this->createQuery($apieContext), |
| 33 | 'mutation' => $this->createMutations($apieContext), |
| 34 | ]); |
| 35 | } |
| 36 | |
| 37 | private function createMutations(ApieContext $apieContext): ObjectType |
| 38 | { |
| 39 | $fields = [ |
| 40 | ]; |
| 41 | $boundedContext = $apieContext->getContext(BoundedContext::class); |
| 42 | foreach ($this->actionDefinitionProvider->provideActionDefinitions($boundedContext, $apieContext) as $actionDefinition) { |
| 43 | if ($actionDefinition instanceof GetResourceListActionDefinition || $actionDefinition instanceof GetResourceActionDefinition || $actionDefinition instanceof DownloadFilesActionDefinition) { |
| 44 | continue; |
| 45 | } |
| 46 | if ($actionDefinition instanceof CreateResourceActionDefinition || $actionDefinition instanceof ReplaceResourceActionDefinition) { |
| 47 | $typeInput = Types::createMeta($actionDefinition->getResourceName()); |
| 48 | $type = Types::displayMeta($actionDefinition->getResourceName()); |
| 49 | $fields['create' . $actionDefinition->getResourceName()->getShortName()] = [ |
| 50 | 'name' => 'create' . $actionDefinition->getResourceName()->getShortName(), |
| 51 | 'type' => $type, |
| 52 | 'args' => [ |
| 53 | 'input' => [ |
| 54 | 'type' => $typeInput, |
| 55 | ], |
| 56 | ], |
| 57 | 'description' => $type->description, |
| 58 | 'resolve' => new ApieCallTypeResolver( |
| 59 | CreateObjectAction::class, |
| 60 | $actionDefinition->getBoundedContextId(), |
| 61 | $actionDefinition->getResourceName(), |
| 62 | ), |
| 63 | ]; |
| 64 | } |
| 65 | } |
| 66 | return new ObjectType([ |
| 67 | 'name' => 'Mutation', |
| 68 | 'fields' => $fields, |
| 69 | ]); |
| 70 | } |
| 71 | |
| 72 | private function createQuery(ApieContext $apieContext): ObjectType |
| 73 | { |
| 74 | $fields = [ |
| 75 | 'apie_version' => [ |
| 76 | 'type' => Type::string(), |
| 77 | 'args' => [], |
| 78 | 'description' => 'Get the current version of the Apie library', |
| 79 | 'resolve' => fn (ApieContext $rootValue, array $args): string => ApieLib::VERSION, |
| 80 | ], |
| 81 | ]; |
| 82 | $boundedContext = $apieContext->getContext(BoundedContext::class); |
| 83 | foreach ($this->actionDefinitionProvider->provideActionDefinitions($boundedContext, $apieContext) as $actionDefinition) { |
| 84 | if ($actionDefinition instanceof GetResourceListActionDefinition) { |
| 85 | $type = new SearchObjectType($actionDefinition->getResourceName()); |
| 86 | $fields[$type->name] = [ |
| 87 | 'type' => $type, |
| 88 | 'args' => [ |
| 89 | 'id' => [ |
| 90 | 'type' => Types::fromId($actionDefinition->getResourceName()), |
| 91 | ], |
| 92 | 'filter' => [ |
| 93 | 'type' => Types::createMeta(new \ReflectionClass(QuerySearch::class)), |
| 94 | ], |
| 95 | ], |
| 96 | 'description' => $type->description, |
| 97 | 'resolve' => new SearchObjectTypeResolver($actionDefinition->getResourceName()->name), |
| 98 | ]; |
| 99 | } |
| 100 | } |
| 101 | return new ObjectType([ |
| 102 | 'name' => 'Query', |
| 103 | 'fields' => $fields, |
| 104 | ]); |
| 105 | } |
| 106 | } |