Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

UniqueOperationTypes::getSDLVisitorpublicWC 1.0

Method of the class: UniqueOperationTypes{}

No Hooks.

Returns

null. Nothing (null).

Usage

$UniqueOperationTypes = new UniqueOperationTypes();
$UniqueOperationTypes->getSDLVisitor( $context ): array;
$context(SDLValidationContext) (required)
.

UniqueOperationTypes::getSDLVisitor() code WC 10.9.1

public function getSDLVisitor(SDLValidationContext $context): array
{
    $schema = $context->getSchema();
    $definedOperationTypes = [];
    $existingOperationTypes = $schema !== null
        ? [
            'query' => $schema->getQueryType(),
            'mutation' => $schema->getMutationType(),
            'subscription' => $schema->getSubscriptionType(),
        ]
        : [];

    /**
     * @param SchemaDefinitionNode|SchemaExtensionNode $node
     */
    $checkOperationTypes = static function ($node) use ($context, &$definedOperationTypes, $existingOperationTypes): VisitorOperation {
        foreach ($node->operationTypes as $operationType) {
            $operation = $operationType->operation;
            $alreadyDefinedOperationType = $definedOperationTypes[$operation] ?? null;

            if (isset($existingOperationTypes[$operation])) {
                $context->reportError(
                    new Error(
                        "Type for {$operation} already defined in the schema. It cannot be redefined.",
                        $operationType,
                    ),
                );
            } elseif ($alreadyDefinedOperationType !== null) {
                $context->reportError(
                    new Error(
                        "There can be only one {$operation} type in schema.",
                        [$alreadyDefinedOperationType, $operationType],
                    ),
                );
            } else {
                $definedOperationTypes[$operation] = $operationType;
            }
        }

        return Visitor::skipNode();
    };

    return [
        NodeKind::SCHEMA_DEFINITION => $checkOperationTypes,
        NodeKind::SCHEMA_EXTENSION => $checkOperationTypes,
    ];
}