Automattic\WooCommerce\Vendor\GraphQL\Utils

SchemaExtender::doExtendprotectedWC 1.0

Метод класса: SchemaExtender{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->doExtend( $schema, $documentAST, $options, ?callable $typeConfigDecorator, ?callable $fieldConfigDecorator ): Schema;
$schema(Schema) (обязательный)
.
$documentAST(DocumentNode) (обязательный)
.
$options(массив)
.
По умолчанию: []
?callable $typeConfigDecorator
.
По умолчанию: null
?callable $fieldConfigDecorator
.
По умолчанию: null

Код SchemaExtender::doExtend() WC 11.0.1

protected function doExtend(
    Schema $schema,
    DocumentNode $documentAST,
    array $options = [],
    ?callable $typeConfigDecorator = null,
    ?callable $fieldConfigDecorator = null
): Schema {
    if (
        ! ($options['assumeValid'] ?? false)
        && ! ($options['assumeValidSDL'] ?? false)
    ) {
        DocumentValidator::assertValidSDLExtension($documentAST, $schema);
    }

    /** @var array<string, Node&TypeDefinitionNode> $typeDefinitionMap */
    $typeDefinitionMap = [];

    /** @var array<int, DirectiveDefinitionNode> $directiveDefinitions */
    $directiveDefinitions = [];

    /** @var SchemaDefinitionNode|null $schemaDef */
    $schemaDef = null;

    /** @var array<int, SchemaExtensionNode> $schemaExtensions */
    $schemaExtensions = [];

    foreach ($documentAST->definitions as $def) {
        if ($def instanceof SchemaDefinitionNode) {
            $schemaDef = $def;
        } elseif ($def instanceof SchemaExtensionNode) {
            $schemaExtensions[] = $def;
        } elseif ($def instanceof TypeDefinitionNode) {
            $name = $def->getName()->value;
            $typeDefinitionMap[$name] = $def;
        } elseif ($def instanceof TypeExtensionNode) {
            $name = $def->getName()->value;
            $this->typeExtensionsMap[$name][] = $def;
        } elseif ($def instanceof DirectiveDefinitionNode) {
            $directiveDefinitions[] = $def;
        }
    }

    if (
        $this->typeExtensionsMap === []
        && $typeDefinitionMap === []
        && $directiveDefinitions === []
        && $schemaExtensions === []
        && $schemaDef === null
    ) {
        return $schema;
    }

    $this->astBuilder = new ASTDefinitionBuilder(
        $typeDefinitionMap,
        [],
        function (string $typeName) use ($schema): Type {
            $existingType = $schema->getType($typeName);
            if ($existingType === null) {
                throw new InvariantViolation("Unknown type: \"{$typeName}\".");
            }

            return $this->extendNamedType($existingType);
        },
        $typeConfigDecorator,
        $fieldConfigDecorator
    );

    $this->extendTypeCache = [];

    $types = [];

    // Iterate through all types, getting the type definition for each, ensuring
    // that any type not directly referenced by a field will get created.
    foreach ($schema->getTypeMap() as $type) {
        $types[] = $this->extendNamedType($type);
    }

    // Do the same with new types.
    foreach ($typeDefinitionMap as $type) {
        $types[] = $this->astBuilder->buildType($type);
    }

    $operationTypes = [
        'query' => $this->extendMaybeNamedType($schema->getQueryType()),
        'mutation' => $this->extendMaybeNamedType($schema->getMutationType()),
        'subscription' => $this->extendMaybeNamedType($schema->getSubscriptionType()),
    ];

    if ($schemaDef !== null) {
        foreach ($schemaDef->operationTypes as $operationType) {
            $operationTypes[$operationType->operation] = $this->astBuilder->buildType($operationType->type);
        }
    }

    foreach ($schemaExtensions as $schemaExtension) {
        foreach ($schemaExtension->operationTypes as $operationType) {
            $operationTypes[$operationType->operation] = $this->astBuilder->buildType($operationType->type);
        }
    }

    $schemaConfig = (new SchemaConfig())
        ->setDescription($schemaDef->description->value ?? $schema->description ?? null)
        // @phpstan-ignore-next-line the root types may be invalid, but just passing them leads to more actionable errors
        ->setQuery($operationTypes['query'])
        // @phpstan-ignore-next-line the root types may be invalid, but just passing them leads to more actionable errors
        ->setMutation($operationTypes['mutation'])
        // @phpstan-ignore-next-line the root types may be invalid, but just passing them leads to more actionable errors
        ->setSubscription($operationTypes['subscription'])
        ->setTypes($types)
        ->setDirectives($this->getMergedDirectives($schema, $directiveDefinitions))
        ->setAstNode($schema->astNode ?? $schemaDef)
        ->setExtensionASTNodes([...$schema->extensionASTNodes, ...$schemaExtensions]);

    return new Schema($schemaConfig);
}