Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions config/schema/graphql.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ graphql.graphql_servers.*:
batching:
type: boolean
label: 'Batching'
disable_introspection:
type: boolean
label: 'Disable Introspection'
query_depth:
type: integer
label: 'Max query depth'
query_complexity:
type: number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does number exist? should also be integer?

label: 'Max query complexity'
schema_configuration:
type: 'graphql.schema.[%parent.schema]'
persisted_queries_settings:
Expand Down
69 changes: 67 additions & 2 deletions src/Entity/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use GraphQL\Server\Helper;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Validator\DocumentValidator;
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;

/**
* The main GraphQL configuration and request entry point.
Expand Down Expand Up @@ -59,7 +62,10 @@
* "endpoint",
* "debug_flag",
* "caching",
* "batching"
* "batching",
* "disable_introspection",
* "query_depth",
* "query_complexity"

This comment was marked as resolved.

* },
* links = {
* "collection" = "/admin/config/graphql/servers",
Expand Down Expand Up @@ -498,10 +504,69 @@ protected function getValidationRules() {
return [];
}

return array_values(DocumentValidator::defaultRules());
$rules = array_values(DocumentValidator::defaultRules());
if ($this->getDisableIntrospection()) {
$rules[DisableIntrospection::class] = new DisableIntrospection();
}
if ($this->getQueryDepth()) {
$rules[QueryDepth::class] = new QueryDepth($this->query_depth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, we forgot to add the new properties to the class and document them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@klausi , my bad, I forgot to use the appropriate getter. Added it in a new commit.

}
if ($this->getQueryComplexity()) {
$rules[QueryComplexity::class] = new QueryComplexity($this->query_complexity);
}

return $rules;
};
}

/**
* {@inheritdoc}
*/
public function getDisableIntrospection() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add return type hints to the helpers, bool here

return (bool) $this->get('disable_introspection');
}

/**
* {@inheritdoc}
*/
public function setDisableIntrospection($introspection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool param type

$this->set('disable_introspection', $introspection);
return $this;
}

/**
* {@inheritdoc}
*/
public function getQueryDepth() {
return $this->get('query_depth');
}

/**
* {@inheritdoc}
*/
public function setQueryDepth($depth) {
$this->set('query_depth', $depth);
return $this;
}

/**
* Gets query complexity config.
*
* @return int|null
* The query complexity, NULL otherwise.
*/
public function getQueryComplexity() {
return $this->get('query_complexity');
}

/**
* {@inheritdoc}
*/
public function setQueryComplexity($complexity) {
$this->set('query_complexity', $complexity);
return $this;
}

/**
* {@inheritDoc}
*/
Expand Down
54 changes: 54 additions & 0 deletions src/Entity/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,58 @@ public function getPersistedQueryInstances();
*/
public function getSortedPersistedQueryInstances();

/**
* Gets disable introspection config.
*
* @return bool
* The disable introspection config, FALSE otherwise.
*/
public function getDisableIntrospection();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not change the interface at this point because graphql 4.0 has already been released.

Can we only do the changes on the Server class?


/**
* Sets disable introspection config.
*
* @param bool $introspection
* The value for the disable introspection config.
*
* @return $this
*/
public function setDisableIntrospection($introspection);

/**
* Gets query depth config.
*
* @return int|null
* The query depth, NULL otherwise.
*/
public function getQueryDepth();

/**
* Sets query depth config.
*
* @param int $depth
* The value for the query depth config.
*
* @return $this
*/
public function setQueryDepth($depth);

/**
* Gets query complexity config.
*
* @return int|null
* The query complexity, NULL otherwise.
*/
public function getQueryComplexity();

/**
* Sets query complexity config.
*
* @param int $complexity
* The value for the query complexity config.
*
* @return $this
*/
public function setQueryComplexity($complexity);

}
26 changes: 26 additions & 0 deletions src/Form/ServerForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,32 @@ public function form(array $form, FormStateInterface $formState): array {
'#description' => $this->t('Whether caching of queries and partial results is enabled.'),
];

$form['validation'] = [
'#title' => $this->t('Validation rules'),
'#type' => 'fieldset',
];

$form['validation']['disable_introspection'] = [
'#title' => $this->t('Disable introspection'),
'#type' => 'checkbox',
'#default_value' => $server->getDisableIntrospection(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, should we use the new methods here? they might not exist if somebody has swapped out the entity class and does not inherit them. Maybe an edge case and we don't care, but I think we should use $server->get() to be on the safe side.

'#description' => $this->t('Security rule: Whether introspection should be disabled.'),
];

$form['validation']['query_depth'] = [
'#title' => $this->t('Max query depth'),
'#type' => 'number',
'#default_value' => $server->getQueryDepth(),
'#description' => $this->t('Security rule: The maximum allowed depth of nested queries. Leave empty to set unlimited.'),
];

$form['validation']['query_complexity'] = [
'#title' => $this->t('Max query complexity'),
'#default_value' => $server->getQueryComplexity(),
'#type' => 'number',
'#description' => $this->t('Security rule: The maximum allowed complexity of a query. Leave empty to set unlimited.'),
];

$debug_flags = $server->get('debug_flag') ?? 0;
$form['debug_flag'] = [
'#title' => $this->t('Debug settings'),
Expand Down