Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

PossibleFragmentSpreads::doTypesOverlapprotectedWC 1.0

Method of the class: PossibleFragmentSpreads{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->doTypesOverlap( $schema, $fragType, $parentType ): bool;
$schema(Schema) (required)
.
$fragType(CompositeType&Type) (required)
.
$parentType(CompositeType&Type) (required)
.

PossibleFragmentSpreads::doTypesOverlap() code WC 10.9.1

protected function doTypesOverlap(Schema $schema, CompositeType $fragType, CompositeType $parentType): bool
{
    // Checking in the order of the most frequently used scenarios:
    // Parent type === fragment type
    if ($parentType === $fragType) {
        return true;
    }

    // Parent type is interface or union, fragment type is object type
    if ($parentType instanceof AbstractType && $fragType instanceof ObjectType) {
        return $schema->isSubType($parentType, $fragType);
    }

    // Parent type is object type, fragment type is interface (or rather rare - union)
    if ($parentType instanceof ObjectType && $fragType instanceof AbstractType) {
        return $schema->isSubType($fragType, $parentType);
    }

    // Both are object types:
    if ($parentType instanceof ObjectType && $fragType instanceof ObjectType) {
        return $parentType === $fragType;
    }

    // Both are interfaces
    // This case may be assumed valid only when implementations of two interfaces intersect
    // But we don't have information about all implementations at runtime
    // (getting this information via $schema->getPossibleTypes() requires scanning through whole schema
    // which is very costly to do at each request due to PHP "shared nothing" architecture)
    //
    // So in this case we just make it pass - invalid fragment spreads will be simply ignored during execution
    // See also https://github.com/webonyx/graphql-php/issues/69#issuecomment-283954602
    if ($parentType instanceof InterfaceType && $fragType instanceof InterfaceType) {
        return true;

        // Note that there is one case when we do have information about all implementations:
        // When schema descriptor is defined ($schema->hasDescriptor())
        // BUT we must avoid situation when some query that worked in development had suddenly stopped
        // working in production. So staying consistent and always validate.
    }

    // Interface within union
    if ($parentType instanceof UnionType && $fragType instanceof InterfaceType) {
        foreach ($parentType->getTypes() as $type) {
            if ($type->implementsInterface($fragType)) {
                return true;
            }
        }
    }

    if ($parentType instanceof InterfaceType && $fragType instanceof UnionType) {
        foreach ($fragType->getTypes() as $type) {
            if ($type->implementsInterface($parentType)) {
                return true;
            }
        }
    }

    if ($parentType instanceof UnionType && $fragType instanceof UnionType) {
        foreach ($fragType->getTypes() as $type) {
            if ($parentType->isPossibleType($type)) {
                return true;
            }
        }
    }

    return false;
}