Submit
Path:
~
/
/
opt
/
psa
/
admin
/
plib
/
vendor
/
webonyx
/
graphql-php
/
src
/
Validator
/
Rules
/
File Content:
VariablesInAllowedPosition.php
<?php declare(strict_types=1); namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; use GraphQL\Error\InvariantViolation; use GraphQL\Language\AST\NodeKind; use GraphQL\Language\AST\NullValueNode; use GraphQL\Language\AST\OperationDefinitionNode; use GraphQL\Language\AST\ValueNode; use GraphQL\Language\AST\VariableDefinitionNode; use GraphQL\Type\Definition\NonNull; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; use GraphQL\Utils\AST; use GraphQL\Utils\TypeComparators; use GraphQL\Utils\Utils; use GraphQL\Validator\QueryValidationContext; class VariablesInAllowedPosition extends ValidationRule { /** * A map from variable names to their definition nodes. * * @var array<string, VariableDefinitionNode> */ protected array $varDefMap; public function getVisitor(QueryValidationContext $context): array { return [ NodeKind::OPERATION_DEFINITION => [ 'enter' => function (): void { $this->varDefMap = []; }, 'leave' => function (OperationDefinitionNode $operation) use ($context): void { $usages = $context->getRecursiveVariableUsages($operation); foreach ($usages as $usage) { $node = $usage['node']; $type = $usage['type']; $defaultValue = $usage['defaultValue']; $varName = $node->name->value; $varDef = $this->varDefMap[$varName] ?? null; if ($varDef === null || $type === null) { continue; } // A var type is allowed if it is the same or more strict (e.g. is // a subtype of) than the expected type. It can be more strict if // the variable type is non-null when the expected type is nullable. // If both are list types, the variable item type can be more strict // than the expected item type (contravariant). $schema = $context->getSchema(); $varType = AST::typeFromAST([$schema, 'getType'], $varDef->type); if ($varType !== null && ! $this->allowedVariableUsage($schema, $varType, $varDef->defaultValue, $type, $defaultValue)) { $context->reportError(new Error( static::badVarPosMessage($varName, $varType->toString(), $type->toString()), [$varDef, $node] )); } } }, ], NodeKind::VARIABLE_DEFINITION => function (VariableDefinitionNode $varDefNode): void { $this->varDefMap[$varDefNode->variable->name->value] = $varDefNode; }, ]; } /** * A var type is allowed if it is the same or more strict than the expected * type. It can be more strict if the variable type is non-null when the * expected type is nullable. If both are list types, the variable item type can * be more strict than the expected item type. */ public static function badVarPosMessage(string $varName, string $varType, string $expectedType): string { return "Variable \"\${$varName}\" of type \"{$varType}\" used in position expecting type \"{$expectedType}\"."; } /** * Returns true if the variable is allowed in the location it was found, * which includes considering if default values exist for either the variable * or the location at which it is located. * * @param ValueNode|null $varDefaultValue * @param mixed $locationDefaultValue * * @throws InvariantViolation */ protected function allowedVariableUsage(Schema $schema, Type $varType, $varDefaultValue, Type $locationType, $locationDefaultValue): bool { if ($locationType instanceof NonNull && ! $varType instanceof NonNull) { $hasNonNullVariableDefaultValue = $varDefaultValue !== null && ! $varDefaultValue instanceof NullValueNode; $hasLocationDefaultValue = Utils::undefined() !== $locationDefaultValue; if (! $hasNonNullVariableDefaultValue && ! $hasLocationDefaultValue) { return false; } $nullableLocationType = $locationType->getWrappedType(); return TypeComparators::isTypeSubTypeOf($schema, $varType, $nullableLocationType); } return TypeComparators::isTypeSubTypeOf($schema, $varType, $locationType); } }
Submit
FILE
FOLDER
INFO
Name
Size
Permission
Action
CustomValidationRule.php
982 bytes
0644
DisableIntrospection.php
1428 bytes
0644
ExecutableDefinitions.php
2093 bytes
0644
FieldsOnCorrectType.php
5239 bytes
0644
FragmentsOnCompositeTypes.php
2212 bytes
0644
KnownArgumentNames.php
2471 bytes
0644
KnownArgumentNamesOnDirectives.php
3669 bytes
0644
KnownDirectives.php
7939 bytes
0644
KnownFragmentNames.php
1023 bytes
0644
KnownTypeNames.php
3455 bytes
0644
LoneAnonymousOperation.php
1591 bytes
0644
LoneSchemaDefinition.php
1793 bytes
0644
NoFragmentCycles.php
3359 bytes
0644
NoUndefinedVariables.php
2351 bytes
0644
NoUnusedFragments.php
2229 bytes
0644
NoUnusedVariables.php
2211 bytes
0644
OverlappingFieldsCanBeMerged.php
31858 bytes
0644
PossibleFragmentSpreads.php
6390 bytes
0644
PossibleTypeExtensions.php
6340 bytes
0644
ProvidedRequiredArguments.php
2024 bytes
0644
ProvidedRequiredArgumentsOnDirectives.php
4287 bytes
0644
QueryComplexity.php
9211 bytes
0644
QueryDepth.php
4144 bytes
0644
QuerySecurityRule.php
6556 bytes
0644
ScalarLeafs.php
1687 bytes
0644
SingleFieldSubscription.php
1506 bytes
0644
UniqueArgumentDefinitionNames.php
2857 bytes
0644
UniqueArgumentNames.php
2003 bytes
0644
UniqueDirectiveNames.php
1971 bytes
0644
UniqueDirectivesPerLocation.php
3165 bytes
0644
UniqueEnumValueNames.php
2450 bytes
0644
UniqueFieldDefinitionNames.php
3831 bytes
0644
UniqueFragmentNames.php
1541 bytes
0644
UniqueInputFieldNames.php
2446 bytes
0644
UniqueOperationNames.php
1692 bytes
0644
UniqueOperationTypes.php
2420 bytes
0644
UniqueTypeNames.php
2172 bytes
0644
UniqueVariableNames.php
1473 bytes
0644
ValidationRule.php
870 bytes
0644
ValuesOfCorrectType.php
7329 bytes
0644
VariablesAreInputTypes.php
1361 bytes
0644
VariablesInAllowedPosition.php
4705 bytes
0644
N4ST4R_ID | Naxtarrr