Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Graphql console controller #4834

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes for Craft CMS 3.x

## Unreleased

### Added
- Added the `graphql/dump-schema` and `graphql/print-schema` console commands. ([#4834](https://github.com/craftcms/cms/pull/4834))

## 3.3.1.1 - 2019-09-06

### Changed
Expand Down
131 changes: 131 additions & 0 deletions src/console/controllers/GraphqlController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\console\controllers;

use Craft;
use craft\errors\GqlException;
use craft\helpers\Console;
use craft\console\Controller;

use yii\helpers\Inflector;
use yii\base\InvalidArgumentException;
use yii\console\ExitCode;
use yii\web\BadRequestHttpException;

use GraphQL\Utils\SchemaPrinter;

/**
* Allows you to manage GraphQL schemas.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.3.2
*/
class GraphqlController extends Controller
{
// Constants
// =========================================================================

const GQL_SCHEMA_EXTENSION = ".graphql";

// Public Properties
// =========================================================================

/**
* @var string The token to look up to determine the appropriate GraphQL schema
*/
public $token = null;

// Public Methods
// =========================================================================

/**
* @inheritdoc
*/
public function options($actionID)
{
$options = parent::options($actionID);
$options[] = 'token';

return $options;
}

/**
* Print out a given GraphQL schema
*
* @return int
*/
public function actionPrintSchema(): int
{
$gqlService = Craft::$app->getGql();
$schema = $this->getGqlSchema();
if ($schema !== null) {
$schemaDef = $gqlService->getSchemaDef($schema, true);
// Output the schema
echo SchemaPrinter::doPrint($schemaDef);
}

return ExitCode::OK;
}

/**
* Dump out a given GraphQL schema to a file
*
* @return int
*/
public function actionDumpSchema(): int
{
$gqlService = Craft::$app->getGql();
$schema = $this->getGqlSchema();
if ($schema !== null) {
$schemaDef = $gqlService->getSchemaDef($schema, true);
// Output the schema
$filename = Inflector::slug($schema->name, '_').self::GQL_SCHEMA_EXTENSION;
$schemaDump = SchemaPrinter::doPrint($schemaDef);;
$result = file_put_contents($filename, $schemaDump);
$this->stdout('Dumping GraphQL schema to file: ', Console::FG_YELLOW);
$this->stdout($filename.PHP_EOL);
}

return ExitCode::OK;
}

// Protected Methods
// =========================================================================

/**
* @return \craft\models\GqlSchema|null
* @throws BadRequestHttpException
* @throws \yii\base\Exception
*/
protected function getGqlSchema()
{
$schema = null;
$gqlService = Craft::$app->getGql();
// First try to get the schema from the passed in token
if ($this->token !== null) {
try {
$schema = $gqlService->getSchemaByAccessToken($this->token);
} catch (InvalidArgumentException $e) {
$this->stdout('Invalid authorization token: ', Console::FG_RED);
$this->stdout($this->token.PHP_EOL, Console::FG_YELLOW);
return null;
}
}
// Next look up the active schema
if ($schema === null) {
try {
$schema = $gqlService->getActiveSchema();
} catch (GqlException $exception) {
// Well, go for the public schema then.
$schema = $gqlService->getPublicSchema();
}
}

return $schema;
}
}