0

我想从灯塔库扩展指令@modelClass。我正在研究模块化架构,但我没有 Eloquent 模型,我有几个,而且我正在扩展我的第一个模型版本,这就是为什么我使用接口来绑定我需要的最后一个模型。我需要做的是使用接口而不是模型类来解析我的类型对象。

使用指令 @modelClass 应该如下所示:

type User @modelClass(class: "App\\Models\\versionC\\User") {
  id: Int!
  username: String!
}

因为我有这个绑定:

$this->app->bind(UserInterface::class, User::class)

我应该有类似的东西:

type User @modelClass(interface: "App\\Interfaces\\UserInterface") {
  id: Int!
  username: String!
}

但我不能覆盖或扩展 @modelClass 指令。

4

1 回答 1

0

我发现它的解决方案是操纵架构

type User @entity(interface: "App\\Interfaces\\UserInterface") {
  id: Int!
  username: String!
}
class EntityDirective extends BaseDirective implements TypeManipulator
{
    public static function definition(): string
    {
        return /** @lang GraphQL */ <<<'SDL'
"""
Map an interface model class to an object type.
This can be used when an eloquent model it is bind to an interface.
"""
directive @entity(
    """
    The interface class name of the corresponding model binding.
    """
    interface: String!
) on OBJECT
SDL;
    }

    public function manipulateTypeDefinition(DocumentAST &$documentAST, TypeDefinitionNode &$objectType)
    {
        $modelClass = $this->directiveArgValue('interface');
        if(!$modelClass) {
            throw new DefinitionException(
                "An `interface` argument must be assigned to the '{$this->name()}'directive on '{$this->nodeName()}"
            );
        }

        $modelClass = get_class(resolve($modelClass));
        // If the type already exists, we use that instead
        if (isset($documentAST->types[$objectType->name->value])) {
            $objectType = $documentAST->types[$objectType->name->value];
        }

        $objectType->directives = ASTHelper::mergeNodeList(
            $objectType->directives,
            [PartialParser::directive('@modelClass(class: "'.addslashes($modelClass).'")')]
        );

        $documentAST->setTypeDefinition($objectType);
    }
}

为了记录,我正在使用灯塔 v4.10

于 2020-03-06T02:25:06.313 回答