Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules

FragmentsOnCompositeTypes{}WC 1.0└─ ValidationRule

No Hooks.

Usage

$FragmentsOnCompositeTypes = new FragmentsOnCompositeTypes();
// use class methods

Methods

  1. public static fragmentOnNonCompositeErrorMessage(string $fragName, string $type)
  2. public getVisitor(QueryValidationContext $context)
  3. public static inlineFragmentOnNonCompositeErrorMessage(string $type)

FragmentsOnCompositeTypes{} code WC 10.9.1

class FragmentsOnCompositeTypes extends ValidationRule
{
    public function getVisitor(QueryValidationContext $context): array
    {
        return [
            NodeKind::INLINE_FRAGMENT => static function (InlineFragmentNode $node) use ($context): void {
                if ($node->typeCondition === null) {
                    return;
                }

                $type = AST::typeFromAST([$context->getSchema(), 'getType'], $node->typeCondition);
                if ($type === null || Type::isCompositeType($type)) {
                    return;
                }

                $context->reportError(new Error(
                    static::inlineFragmentOnNonCompositeErrorMessage($type->toString()),
                    [$node->typeCondition]
                ));
            },
            NodeKind::FRAGMENT_DEFINITION => static function (FragmentDefinitionNode $node) use ($context): void {
                $type = AST::typeFromAST([$context->getSchema(), 'getType'], $node->typeCondition);

                if ($type === null || Type::isCompositeType($type)) {
                    return;
                }

                $context->reportError(new Error(
                    static::fragmentOnNonCompositeErrorMessage(
                        $node->name->value,
                        Printer::doPrint($node->typeCondition)
                    ),
                    [$node->typeCondition]
                ));
            },
        ];
    }

    public static function inlineFragmentOnNonCompositeErrorMessage(string $type): string
    {
        return "Fragment cannot condition on non composite type \"{$type}\".";
    }

    public static function fragmentOnNonCompositeErrorMessage(string $fragName, string $type): string
    {
        return "Fragment \"{$fragName}\" cannot condition on non composite type \"{$type}\".";
    }
}