diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 1f0a227dbba..9899c20dd8c 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -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 diff --git a/src/console/controllers/GraphqlController.php b/src/console/controllers/GraphqlController.php new file mode 100644 index 00000000000..ac418e3fa3b --- /dev/null +++ b/src/console/controllers/GraphqlController.php @@ -0,0 +1,131 @@ + + * @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; + } +}