Automattic\WooCommerce\Vendor\GraphQL\Type

SchemaValidationContext::validateTypeImplementsInterfaceprivateWC 1.0

Method of the class: SchemaValidationContext{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->validateTypeImplementsInterface( $type, $iface ): void;
$type(ObjectType|InterfaceType) (required)
.
$iface(InterfaceType) (required)
.

SchemaValidationContext::validateTypeImplementsInterface() code WC 10.9.1

private function validateTypeImplementsInterface(ImplementingType $type, InterfaceType $iface): void
{
    $typeFieldMap = $type->getFields();
    $ifaceFieldMap = $iface->getFields();

    foreach ($ifaceFieldMap as $fieldName => $ifaceField) {
        $typeField = $typeFieldMap[$fieldName] ?? null;

        if ($typeField === null) {
            $this->reportError(
                "Interface field {$iface->name}.{$fieldName} expected but {$type->name} does not provide it.",
                array_merge(
                    [$this->getFieldNode($iface, $fieldName)],
                    $this->getAllNodes($type)
                )
            );
            continue;
        }

        $typeFieldType = $typeField->getType();
        $ifaceFieldType = $ifaceField->getType();
        if (! TypeComparators::isTypeSubTypeOf($this->schema, $typeFieldType, $ifaceFieldType)) {
            $this->reportError(
                "Interface field {$iface->name}.{$fieldName} expects type {$ifaceFieldType} but {$type->name}.{$fieldName} is type {$typeFieldType}.",
                [
                    $this->getFieldTypeNode($iface, $fieldName),
                    $this->getFieldTypeNode($type, $fieldName),
                ]
            );
        }

        foreach ($ifaceField->args as $ifaceArg) {
            $argName = $ifaceArg->name;
            $typeArg = $typeField->getArg($argName);

            if ($typeArg === null) {
                $this->reportError(
                    "Interface field argument {$iface->name}.{$fieldName}({$argName}:) expected but {$type->name}.{$fieldName} does not provide it.",
                    [
                        $this->getFieldArgNode($iface, $fieldName, $argName),
                        $this->getFieldNode($type, $fieldName),
                    ]
                );
                continue;
            }

            $ifaceArgType = $ifaceArg->getType();
            $typeArgType = $typeArg->getType();
            if (! TypeComparators::isEqualType($ifaceArgType, $typeArgType)) {
                $this->reportError(
                    "Interface field argument {$iface->name}.{$fieldName}({$argName}:) expects type {$ifaceArgType} but {$type->name}.{$fieldName}({$argName}:) is type {$typeArgType}.",
                    [
                        $this->getFieldArgTypeNode($iface, $fieldName, $argName),
                        $this->getFieldArgTypeNode($type, $fieldName, $argName),
                    ]
                );
            }

            // TODO: validate default values?
        }

        foreach ($typeField->args as $typeArg) {
            $argName = $typeArg->name;
            $ifaceArg = $ifaceField->getArg($argName);

            if ($typeArg->isRequired() && $ifaceArg === null) {
                $this->reportError(
                    "Object field {$type->name}.{$fieldName} includes required argument {$argName} that is missing from the Interface field {$iface->name}.{$fieldName}.",
                    [
                        $this->getFieldArgNode($type, $fieldName, $argName),
                        $this->getFieldNode($iface, $fieldName),
                    ]
                );
            }
        }
    }
}