Skip to content
Snippets Groups Projects
Commit 1c29b1a0 authored by Al Munnings's avatar Al Munnings
Browse files

[Task] Fix tests to work with 4.7, fix schema config, remove expect...

[Task] Fix tests to work with 4.7, fix schema config, remove expect deprecation, move to use alterable schema as base.
parent fa1dc9cc
Branches
No related tags found
No related merge requests found
Showing
with 121 additions and 160 deletions
......@@ -12,7 +12,7 @@
},
"require": {
"php": "^8.1",
"drupal/graphql": "^4.6",
"drupal/graphql": "^4.7",
"doctrine/inflector": "^2",
"symfony/string": "^6"
},
......
langcode: en
status: true
dependencies: {}
dependencies: { }
name: graphql_compose_server
label: "GraphQL Compose - Server"
endpoint: /graphql
......@@ -11,5 +11,7 @@ batching: true
disable_introspection: false
query_depth: null
query_complexity: null
schema_configuration: {}
persisted_queries_settings: {}
schema_configuration:
graphql_compose:
enabled: true
persisted_queries_settings: { }
......@@ -121,3 +121,15 @@ graphql_compose.field.*.*.*:
type: string
label: "Schema field name"
nullable: true
graphql.schema.graphql_compose:
type: mapping
label: 'GraphQL Compose schema'
mapping:
graphql_compose:
type: mapping
label: 'GraphQL Compose'
mapping:
enabled:
label: Enabled
type: boolean
......@@ -140,7 +140,6 @@ function graphql_compose_preprocess_links__dropbutton__operations(array &$variab
* Implements hook_form_FORM_ID_alter().
*/
function graphql_compose_form_graphql_server_create_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$form['schema']['#element_validate'][] = '_graphql_compose_schema_validate';
_graphql_compose_schema_options($form, $form_state);
}
......@@ -148,28 +147,9 @@ function graphql_compose_form_graphql_server_create_form_alter(array &$form, For
* Implements hook_form_FORM_ID_alter().
*/
function graphql_compose_form_graphql_server_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$form['schema']['#element_validate'][] = '_graphql_compose_schema_validate';
_graphql_compose_schema_options($form, $form_state);
}
/**
* Callback from hook_form_FORM_ID_alter().
*
* Fix up a bug in the GraphQL module where the debug flag is not set
* properly on ajax callbacks from ajaxSchemaConfigurationForm.
*/
function _graphql_compose_schema_validate(array &$form, FormStateInterface $form_state): void {
/** @var \Drupal\graphql\Form\ServerForm $server_form */
$server_form = $form_state->getFormObject();
/** @var \Drupal\graphql\Entity\ServerInterface $server */
$server = $server_form->getEntity();
$debug_flag = $server->get('debug_flag') ?: 0;
if (is_array($debug_flag)) {
$server->set('debug_flag', array_sum($debug_flag));
}
}
/**
* Callback from hook_form_FORM_ID_alter().
*
......
......@@ -65,3 +65,11 @@ services:
- "@config.factory"
- "@graphql_compose.entity_type_manager"
- "%graphql_compose.config%"
# Subscribe to alter the schema
graphql_compose.alter_subscriber:
class: Drupal\graphql_compose\EventSubscriber\AlterSchemaSubscriber
arguments:
- "@graphql_compose.schema_type_manager"
tags:
- { name: "event_subscriber" }
<?php
declare(strict_types=1);
namespace Drupal\graphql_compose\EventSubscriber;
use Drupal\graphql\Event\AlterSchemaDataEvent;
use Drupal\graphql\Event\AlterSchemaExtensionDataEvent;
use Drupal\graphql_compose\Plugin\GraphQLComposeSchemaTypeManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Extend the schema with GraphQL Compose.
*/
class AlterSchemaSubscriber implements EventSubscriberInterface {
/**
* Constructs a new ConfigEventsSubscriber object.
*
* @param \Drupal\graphql_gql_schema\GraphQLSchemaTypeManager $gqlSchemaTypeManager
* The GraphQL schema type manager.
*/
public function __construct(
protected GraphQLComposeSchemaTypeManager $gqlSchemaTypeManager
) {}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
AlterSchemaExtensionDataEvent::EVENT_NAME => ['alterExtensions'],
AlterSchemaDataEvent::EVENT_NAME => ['alterSchema'],
];
}
/**
* Alter the schema data.
*
* @param \Drupal\graphql\Event\AlterSchemaExtensionDataEvent $event
* The alter schema data event.
*/
public function alterExtensions(AlterSchemaExtensionDataEvent $event) {
$schema = $event->getSchemaExtensionData();
$schema[] = $this->gqlSchemaTypeManager->printExtensions();
// Fix an issue with empty extensions. Always add something.
// @see GraphQLComposeSchema::getExtensionDefinition()
$schema[] = 'scalar _';
$event->setSchemaExtensionData($schema);
}
/**
* Alter the schema data.
*
* @param \Drupal\graphql\Event\AlterSchemaDataEvent $event
* The alter schema data event.
*/
public function alterSchema(AlterSchemaDataEvent $event) {
$schema = $event->getSchemaData();
$schema[] = $this->gqlSchemaTypeManager->printTypes();
$event->setSchemaData($schema);
}
}
......@@ -4,21 +4,12 @@ declare(strict_types=1);
namespace Drupal\graphql_compose\Plugin\GraphQL\Schema;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\ResolverRegistry;
use Drupal\graphql\Plugin\GraphQL\Schema\AlterableComposableSchema;
use Drupal\graphql\Plugin\GraphQL\Schema\SdlSchemaPluginBase;
use Drupal\graphql\Plugin\SchemaExtensionPluginInterface;
use Drupal\graphql_compose\Plugin\GraphQLComposeSchemaTypeManager;
use GraphQL\Language\Parser;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The provider of the schema base for the GraphQL Compose GraphQL API.
......@@ -32,144 +23,42 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* name = "GraphQL Compose Schema",
* )
*/
class GraphQLComposeSchema extends SdlSchemaPluginBase implements ConfigurableInterface, PluginFormInterface, ContainerFactoryPluginInterface {
class GraphQLComposeSchema extends AlterableComposableSchema {
use StringTranslationTrait;
use MessengerTrait;
/**
* Field type plugin manager.
* {@inheritDoc}
*
* @var \Drupal\graphql_compose\Plugin\GraphQLComposeSchemaTypeManager
* - Load all extensions that are tagged with graphql_compose.
*/
protected GraphQLComposeSchemaTypeManager $gqlSchemaTypeManager;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected MessengerInterface $messenger;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$instance->gqlSchemaTypeManager = $container->get('graphql_compose.schema_type_manager');
$instance->messenger = $container->get('messenger');
return $instance;
}
/**
* {@inheritdoc}
*/
public function getResolverRegistry() {
return new ResolverRegistry();
}
/**
* {@inheritdoc}
*
* Inject extensions into the schema after all extensions loaded.
*/
protected function getSchemaDocument(array $extensions = []) {
// Only use caching of the parsed document if we aren't in development mode.
$cid = "schema:{$this->getPluginId()}";
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
return $cache->data;
}
$extensions = array_filter(array_map(function (SchemaExtensionPluginInterface $extension) {
return $extension->getBaseDefinition();
}, $extensions), function ($definition) {
return !empty($definition);
public function defaultConfiguration() {
$extensions = $this->extensionManager->getDefinitions();
$extensions = array_filter($extensions, function ($definition) {
return $definition['schema'] === 'graphql_compose';
});
$schema = array_filter(array_merge(
[$this->gqlSchemaTypeManager->printTypes()],
[$this->getSchemaDefinition()],
$extensions
));
$options = ['noLocation' => TRUE];
$ast = !empty($schema) ? Parser::parse(implode(PHP_EOL . PHP_EOL, $schema), $options) : NULL;
if (empty($this->inDevelopment)) {
$this->astCache->set($cid, $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']);
}
return $ast;
return ['extensions' => array_keys($extensions)];
}
/**
* {@inheritdoc}
*
* Inject extensions into the schema after all extensions loaded.
*/
protected function getExtensionDocument(array $extensions = []) {
// Only use caching of the parsed document if we aren't in development mode.
$cid = "extension:{$this->getPluginId()}";
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
return $cache->data;
}
$extensions = array_filter(array_map(function (SchemaExtensionPluginInterface $extension) {
return $extension->getExtensionDefinition();
}, $extensions), function ($definition) {
return !empty($definition);
});
$schema = array_filter(array_merge(
[$this->gqlSchemaTypeManager->printExtensions()],
$extensions
));
$options = ['noLocation' => TRUE];
$ast = !empty($schema) ? Parser::parse(implode(PHP_EOL . PHP_EOL, $schema), $options) : NULL;
if (empty($this->inDevelopment)) {
$this->astCache->set($cid, $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']);
}
return $ast;
}
/**
* {@inheritdoc}
* - Load the .graphql file from the module.
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration): void {
$this->configuration = NestedArray::mergeDeep($this->defaultConfiguration(), $configuration);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
protected function getSchemaDefinition() {
return SdlSchemaPluginBase::getSchemaDefinition();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$info = $form_state->getBuildInfo();
if ($info['form_id'] === 'graphql_server_create_form') {
$this->messenger->addStatus('GraphQL Compose is ready to use.');
$this->messenger()->addStatus('GraphQL Compose is ready to use.');
$form['settings_good'] = [
'#type' => 'status_messages',
'#display' => 'status',
......@@ -186,6 +75,11 @@ class GraphQLComposeSchema extends SdlSchemaPluginBase implements ConfigurableIn
];
}
$form['enabled'] = [
'#type' => 'hidden',
'#value' => TRUE,
];
return $form;
}
......@@ -200,7 +94,7 @@ class GraphQLComposeSchema extends SdlSchemaPluginBase implements ConfigurableIn
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
// Satisfy interface. Nothing to do here.
// Do nothing. We autoload all extensions.
}
}
......@@ -35,7 +35,6 @@ class GraphQLComposeTypeSchemaExtension extends ResolverOnlySchemaExtensionPlugi
* {@inheritdoc}
*/
public function getBaseDefinition() {
// Add entity types and fields to schema.
foreach ($this->gqlEntityTypeManager->getPluginInstances() as $entity_type) {
$entity_type->registerTypes();
......
......@@ -106,7 +106,8 @@ abstract class ResolverOnlySchemaExtensionPluginBase extends PluginBase implemen
* @internal
*/
public function getExtensionDefinition() {
return NULL;
// Return "something" so AlterableComposableSchema can be built.
return PHP_EOL;
}
}
......@@ -27,14 +27,14 @@ class GraphQLComposeFieldTypeManager extends DefaultPluginManager {
/**
* Private field plugin storage.
*
* @var \Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeInterface[]
* @var array
*/
private array $fields = [];
/**
* Private field plugin storage.
*
* @var \Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeInterface[]
* @var array
*/
private array $interfaceFields = [];
......
......@@ -36,8 +36,6 @@ class AddressTest extends GraphQLComposeBrowserTestBase {
protected function setUp(): void {
parent::setUp();
$this->expectDeprecation('Using a translatable string as a category for field type is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3364271');
$this->createContentType([
'type' => 'test',
'name' => 'Test node type',
......
......@@ -38,8 +38,6 @@ class BlockFieldTest extends GraphQLComposeBrowserTestBase {
protected function setUp(): void {
parent::setUp();
$this->expectDeprecation('Using a translatable string as a category for field type is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. See https://www.drupal.org/node/3364271');
$this->createContentType([
'type' => 'test',
'name' => 'Test node type',
......
......@@ -645,7 +645,7 @@ class LayoutBuilderTest extends GraphQLComposeBrowserTestBase {
}
$this->assertStringContainsStringIgnoringCase(
'Unknown type "BlockFieldNodeTestBody"',
'"BlockFieldNodeTestBody" not found in document',
$content['errors'][0]['message'] ?? NULL
);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment