Skip to content

Commit

Permalink
WIP FEATURE: Custom graphql adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebobo committed Dec 6, 2022
1 parent ac61342 commit ae85434
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
69 changes: 69 additions & 0 deletions Classes/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
* source code.
*/

use GraphQL\Error\SyntaxError;
use GraphQL\GraphQL;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Utils\BuildSchema;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\View\JsonView;
use Neos\Fusion\View\FusionView;
use Neos\Media\Domain\Model\Tag;
use Neos\Media\Domain\Repository\TagRepository;
use Neos\Neos\Controller\Module\AbstractModuleController;

/**
Expand All @@ -38,12 +47,72 @@ class MediaController extends AbstractModuleController
*/
protected $viewFormatToObjectNameMap = [
'html' => FusionView::class,
'json' => JsonView::class,
];

/**
* @Flow\InjectConfiguration(path="graphql.schema")
* @var string
*/
protected $schemaPath;

/**
* @Flow\Inject
* @var TagRepository
*/
protected $tagRepository;

/**
* Renders the media ui application
*/
public function indexAction(): void
{
}

/**
* @Flow\SkipCsrfProtection
* @throws SyntaxError|\JsonException
*/
public function queryAction(): void {
$input = $this->request->getHttpRequest()->getParsedBody();
$query = $input['query'] ?? null;
$variableValues = $input['variables'] ?? [];

// TODO: Make sure the file exists
$contents = file_get_contents($this->schemaPath);

// TODO: Add resolvers
$typeConfigDecorator = function (array $typeConfig, TypeDefinitionNode $typeDefinitionNode): array {
$name = $typeConfig['name'];
switch ($name) {
case 'Query':
$typeConfig['resolveField'] = function ($source, $args, $context, ResolveInfo $info) {
if ($info->fieldName === 'tags') {
return $this->tagRepository->findAll()->toArray();
// return array_map(static fn (Tag $tag) => [
// 'id' => $tag->getLabel(),
// 'label' => $tag->getLabel(),
// ], $this->tagRepository->findAll()->toArray());
}
if ($info->fieldName === 'tag') {
$tag = $this->tagRepository->findByIdentifier($args['id']);
return [
'id' => $tag->getLabel(),
'label' => $tag->getLabel(),
];
}
};
break;
}
return $typeConfig;
};

// // TODO: Add error handling
// // TODO: Add caching
$schema = BuildSchema::build($contents, $typeConfigDecorator);

$ast = Parser::parse($query);

$this->view->assign('value', GraphQL::executeQuery($schema, $ast));
}
}
9 changes: 9 additions & 0 deletions Configuration/Routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
package: 't3n.GraphQL'
variables:
'endpoint': 'media-assets'

- name: 'Media GraphQL API'
uriPattern: 'neos/graphql2'
defaults:
'@package': 'Flowpack.Media.Ui'
'@controller': 'Media'
'@format': 'json'
'@action': 'query'
httpMethods: ['GET', 'POST']
2 changes: 2 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Flowpack:
Media:
Ui:
maximumFileUploadLimit: 10
graphql:
schema: 'resource://Flowpack.Media.Ui/Private/GraphQL/schema.root.graphql'
16 changes: 16 additions & 0 deletions Resources/Private/GraphQL/.graphqlconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "Untitled GraphQL Schema",
"schemaPath": "schema.graphql",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "http://helzle.it.test/neos/graphql2",
"headers": {
"user-agent": "JS GraphQL",
"Cookie": "Neos_Session=KfhWNQJBRfO4keZGYpr7Tpf209gXXffT"
},
"introspect": false
}
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"neos/neos": "^5.2 || ~7.0 || ~8.0 || dev-master",
"neos/neos-ui": "^5.2 || ~7.0 || ~8.0 || dev-master",
"t3n/graphql": "^2.1 || ^3.0.2",
"t3n/graphql-upload": "^1.0 || ^2.0"
"t3n/graphql-upload": "^1.0 || ^2.0",
"webonyx/graphql-php": "*"
},
"suggest": {
"phpstan/phpstan": "For running code quality checks",
Expand Down

0 comments on commit ae85434

Please sign in to comment.