Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 3, 2024
1 parent ef4cbfe commit 4bf6d3b
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 215 deletions.
17 changes: 4 additions & 13 deletions _config/dev.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
---
Name: graphql-dev
---
SilverStripe\ORM\DatabaseAdmin:
SilverStripe\HybridExecution\Command\DbBuild:
extensions:
- SilverStripe\GraphQL\Extensions\DevBuildExtension
- SilverStripe\GraphQL\Extensions\DbBuildExtension

SilverStripe\Dev\DevelopmentAdmin:
registered_controllers:
graphql:
controller: SilverStripe\GraphQL\Dev\DevelopmentAdmin
links:
graphql: 'List GraphQL development tools'
SilverStripe\GraphQL\Dev\DevelopmentAdmin:
registered_controllers:
build:
controller: SilverStripe\GraphQL\Dev\Build
links:
build: Build the GraphQL schema
commands:
'graphql/build': 'SilverStripe\GraphQL\Dev\Build'
2 changes: 1 addition & 1 deletion _config/logging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ after: '#logging'
---

SilverStripe\Core\Injector\Injector:
# Omits the HTTPOutputHandler from the logger so errors won't appear in output
# Omits the ErrorOutputHandler from the logger so errors won't appear in output
Psr\Log\LoggerInterface.graphql-quiet:
type: singleton
class: Monolog\Logger
Expand Down
97 changes: 61 additions & 36 deletions src/Dev/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
namespace SilverStripe\GraphQL\Dev;

use Psr\Log\LoggerInterface;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\DebugView;
use SilverStripe\Dev\DevelopmentAdmin;
use SilverStripe\HybridExecution\Command\HybridCommand;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\GraphQL\Schema\DataObject\FieldAccessor;
use SilverStripe\GraphQL\Schema\Exception\EmptySchemaException;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
Expand All @@ -18,38 +17,41 @@
use SilverStripe\GraphQL\Schema\SchemaBuilder;
use SilverStripe\GraphQL\Schema\Storage\CodeGenerationStore;
use SilverStripe\ORM\Connect\NullDatabaseException;
use SilverStripe\Security\PermissionProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

class Build extends Controller
class Build extends HybridCommand implements PermissionProvider
{
private static $url_handlers = [
'' => 'build'
];
protected static string $commandName = 'graphql:build';

protected static string $description = 'Build the GraphQL schema(s)';

private static $allowed_actions = [
'build'
private static string|array|null $permissions_for_browser_execution = [
'ADMIN',
'ALL_DEV_ADMIN',
'CAN_DEV_GRAPHQL',
];

/**
* @throws SchemaBuilderException
* @throws SchemaNotFoundException
*/
public function build(HTTPRequest $request): void
public function getTitle(): string
{
$isBrowser = !Director::is_cli();
if ($isBrowser) {
$renderer = DebugView::create();
echo $renderer->renderHeader();
echo $renderer->renderInfo("GraphQL Schema Builder", Director::absoluteBaseURL());
echo "<div class=\"build\">";
}
$clear = true;

$this->buildSchema($request->getVar('schema'), $clear);
return 'GraphQL Schema Builder';
}

if ($isBrowser) {
echo "</div>";
echo $renderer->renderFooter();
public function run(InputInterface $input, HybridOutput $output): int
{
$originalLogger = Injector::inst()->get(LoggerInterface::class . '.graphql-build');
try {
$logger = Logger::singleton();
$logger->setOutput($output);
Injector::inst()->registerService($logger, LoggerInterface::class . '.graphql-build');
$this->buildSchema($input->getOption('schema'), true, $output);
} finally {
// Restore default logger back to its starting state
Injector::inst()->registerService($originalLogger, LoggerInterface::class . '.graphql-build');
}
return Command::SUCCESS;
}

/**
Expand Down Expand Up @@ -97,15 +99,15 @@ public function buildSchema(string $key = null, bool $clear = true): void
break;
}
}
$logger->warning("
Your schema configuration requires access to the database. This can happen
$logger->warning(
"Your schema configuration requires access to the database. This can happen
when you add fields that require type introspection (i.e. custom getters).
It is recommended that you specify an explicit type when adding custom getters
to your schema.");
to your schema."
);
if ($candidate) {
$logger->warning(sprintf(
"
This most likely happened when you tried to add the field '%s' to '%s'",
"This most likely happened when you tried to add the field '%s' to '%s'",
$candidate['args'][1],
get_class($candidate['args'][0])
));
Expand All @@ -114,9 +116,32 @@ public function buildSchema(string $key = null, bool $clear = true): void
throw $e;
}

$logger->info(
Benchmark::end('build-schema-' . $key, 'Built schema in %sms.')
);
$logger->info(Benchmark::end('build-schema-' . $key, 'Built schema in %sms.'));
}
}

public function getOptions(): array
{
return [
new InputOption(
'schema',
null,
InputOption::VALUE_REQUIRED,
'The name of the schema to be built. If not passed, all schemas will be built',
suggestedValues: array_keys(Schema::config()->get('schemas') ?? [])
)
];
}

public function providePermissions(): array
{
return [
'CAN_DEV_GRAPHQL' => [
'name' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_DESCRIPTION', 'Can view and execute /dev/graphql'),
'help' => _t(__CLASS__ . '.CAN_DEV_GRAPHQL_HELP', 'Can view and execute GraphQL development tools (/dev/graphql).'),
'category' => DevelopmentAdmin::permissionsCategory(),
'sort' => 80
],
];
}
}
146 changes: 0 additions & 146 deletions src/Dev/DevelopmentAdmin.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

use Psr\Log\LoggerInterface;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\HybridExecution\Command\DbBuild;
use SilverStripe\HybridExecution\HybridOutput;
use SilverStripe\GraphQL\Dev\Build;
use SilverStripe\GraphQL\Schema\Logger;
use SilverStripe\ORM\DatabaseAdmin;
use SilverStripe\Core\Extension;

/**
* @extends Extension<DatabaseAdmin>
* @extends Extension<DbBuild>
*/
class DevBuildExtension extends Extension
class DbBuildExtension extends Extension
{
use Configurable;

Expand All @@ -23,7 +24,7 @@ class DevBuildExtension extends Extension
*/
private static bool $enabled = true;

protected function onAfterBuild(): void
protected function onAfterBuild(HybridOutput $output): void
{
if (!static::config()->get('enabled')) {
return;
Expand All @@ -35,7 +36,7 @@ protected function onAfterBuild(): void
try {
// Define custom logger
$logger = Logger::singleton();
$logger->setVerbosity(Logger::INFO);
$logger->setOutput($output);
Injector::inst()->registerService($logger, LoggerInterface::class . '.graphql-build');

Build::singleton()->buildSchema();
Expand Down
Loading

0 comments on commit 4bf6d3b

Please sign in to comment.