CustomValidationRule.php
2 months ago
DisableIntrospection.php
2 months ago
ExecutableDefinitions.php
2 months ago
FieldsOnCorrectType.php
2 months ago
FragmentsOnCompositeTypes.php
2 months ago
KnownArgumentNames.php
2 months ago
KnownArgumentNamesOnDirectives.php
2 months ago
KnownDirectives.php
2 months ago
KnownFragmentNames.php
2 months ago
KnownTypeNames.php
2 months ago
LoneAnonymousOperation.php
2 months ago
LoneSchemaDefinition.php
2 months ago
NoFragmentCycles.php
2 months ago
NoUndefinedVariables.php
2 months ago
NoUnusedFragments.php
2 months ago
NoUnusedVariables.php
2 months ago
OneOfInputObjectsRule.php
2 months ago
OverlappingFieldsCanBeMerged.php
2 months ago
PossibleFragmentSpreads.php
2 months ago
PossibleTypeExtensions.php
2 months ago
ProvidedRequiredArguments.php
2 months ago
ProvidedRequiredArgumentsOnDirectives.php
2 months ago
QueryComplexity.php
2 months ago
QueryDepth.php
2 months ago
QuerySecurityRule.php
2 months ago
ScalarLeafs.php
2 months ago
SingleFieldSubscription.php
2 months ago
UniqueArgumentDefinitionNames.php
2 months ago
UniqueArgumentNames.php
2 months ago
UniqueDirectiveNames.php
2 months ago
UniqueDirectivesPerLocation.php
2 months ago
UniqueEnumValueNames.php
2 months ago
UniqueFieldDefinitionNames.php
2 months ago
UniqueFragmentNames.php
2 months ago
UniqueInputFieldNames.php
2 months ago
UniqueOperationNames.php
2 months ago
UniqueOperationTypes.php
2 months ago
UniqueTypeNames.php
2 months ago
UniqueVariableNames.php
2 months ago
ValidationRule.php
2 months ago
ValuesOfCorrectType.php
2 months ago
VariablesAreInputTypes.php
2 months ago
VariablesInAllowedPosition.php
2 months ago
LoneSchemaDefinition.php
58 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\NodeKind; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\SchemaDefinitionNode; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\SDLValidationContext; |
| 9 | |
| 10 | /** |
| 11 | * Lone schema definition. |
| 12 | * |
| 13 | * A Automattic\WooCommerce\Vendor\GraphQL document is only valid if it contains only one schema definition. |
| 14 | */ |
| 15 | class LoneSchemaDefinition extends ValidationRule |
| 16 | { |
| 17 | public static function schemaDefinitionNotAloneMessage(): string |
| 18 | { |
| 19 | return 'Must provide only one schema definition.'; |
| 20 | } |
| 21 | |
| 22 | public static function canNotDefineSchemaWithinExtensionMessage(): string |
| 23 | { |
| 24 | return 'Cannot define a new schema within a schema extension.'; |
| 25 | } |
| 26 | |
| 27 | public function getSDLVisitor(SDLValidationContext $context): array |
| 28 | { |
| 29 | $oldSchema = $context->getSchema(); |
| 30 | $alreadyDefined = $oldSchema === null |
| 31 | ? false |
| 32 | : ( |
| 33 | $oldSchema->astNode !== null |
| 34 | || $oldSchema->getQueryType() !== null |
| 35 | || $oldSchema->getMutationType() !== null |
| 36 | || $oldSchema->getSubscriptionType() !== null |
| 37 | ); |
| 38 | |
| 39 | $schemaDefinitionsCount = 0; |
| 40 | |
| 41 | return [ |
| 42 | NodeKind::SCHEMA_DEFINITION => static function (SchemaDefinitionNode $node) use ($alreadyDefined, $context, &$schemaDefinitionsCount): void { |
| 43 | if ($alreadyDefined) { |
| 44 | $context->reportError(new Error(static::canNotDefineSchemaWithinExtensionMessage(), $node)); |
| 45 | |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if ($schemaDefinitionsCount > 0) { |
| 50 | $context->reportError(new Error(static::schemaDefinitionNotAloneMessage(), $node)); |
| 51 | } |
| 52 | |
| 53 | ++$schemaDefinitionsCount; |
| 54 | }, |
| 55 | ]; |
| 56 | } |
| 57 | } |
| 58 |