Automattic\WooCommerce\Vendor\GraphQL\Type\Definition

FieldDefinition::defineFieldMappublic staticWC 1.0

Method of the class: FieldDefinition{}

No Hooks.

Returns

Array. self|UnresolvedFieldDefinition>

Usage

$result = FieldDefinition::defineFieldMap( $parentType, $fields ): array;
$parentType(ObjectType|InterfaceType) (required)
.
$fields(callable|iterable) (required)
.

FieldDefinition::defineFieldMap() code WC 10.9.1

public static function defineFieldMap(Type $parentType, $fields): array
{
    if (is_callable($fields)) {
        $fields = $fields();
    }

    if (! is_iterable($fields)) {
        throw new InvariantViolation("{$parentType->name} fields must be an iterable or a callable which returns such an iterable.");
    }

    $map = [];
    foreach ($fields as $maybeName => $field) {
        if (is_array($field)) {
            if (! isset($field['name'])) {
                if (! is_string($maybeName)) {
                    throw new InvariantViolation("{$parentType->name} fields must be an associative array with field names as keys or a function which returns such an array.");
                }

                $field['name'] = $maybeName;
            }

            // @phpstan-ignore-next-line PHPStan won't let us define the whole type
            $fieldDef = new self($field);
        } elseif ($field instanceof self) {
            $fieldDef = $field;
        } elseif (is_callable($field)) {
            if (! is_string($maybeName)) {
                throw new InvariantViolation("{$parentType->name} lazy fields must be an associative array with field names as keys.");
            }

            $fieldDef = new UnresolvedFieldDefinition($maybeName, $field);
        } elseif ($field instanceof Type) {
            // @phpstan-ignore-next-line PHPStan won't let us define the whole type
            $fieldDef = new self([
                'name' => $maybeName,
                'type' => $field,
            ]);
        } else {
            $invalidFieldConfig = Utils::printSafe($field);
            throw new InvariantViolation("{$parentType->name}.{$maybeName} field config must be an array, but got: {$invalidFieldConfig}");
        }

        $map[$fieldDef->getName()] = $fieldDef;
    }

    return $map;
}