Lighthouse 找不到我的 Union 自定义类型

你好吗?


我正在与联合斗争,我想对基于自定义类型的联合类型的自定义解析器进行一些澄清。Mi 当前架构是product.graphql 在内部导入的schema.graphql


type ProductServing {

    name: String

    price: Float

}


type ProductDish {

    price: Float

    calories: Int

    servings: [ProductServing]

}


type ProductDrink {

    price: Float

    servings: [ProductServing]

}


union ProductProperties = ProductDish | ProductDrink


type Product {

    id: ID!

    user: User! @belongsTo

    media: Media

    blocks: [Block]! @belongsToMany

    slug: String!

    name: String!

    description: String

    properties: ProductProperties!

    activated_at: DateTime

    created_at: DateTime!

    updated_at: DateTime!

当然,仅此模式是行不通的,因为 Lighthouse 无法理解自定义类型。我为类型创建了两个类:


// App\GraphQL\Types

class ProductDish extends ObjectType

{

    public function __construct()

    {

        $config = [

            "name" => "ProductDish",

            "fields" => [

                "__type" => Type:string(),

                "price" => Type::float(),

                "calories" => Type::int(),

            ],

        ];

        parent::__construct($config);

    }

}


class ProductDrink extends ObjectType

{

    public function __construct()

    {

        $config = [

            "name" => "ProductDrink",

            "fields" => [

                "__type" => Type:string(),

                "price" => Type::float(),

            ],

        ];

        parent::__construct($config);

    }

}

以及使用 __invoke 方法的 ProductProperties 的联合类

这不起作用,否则我就不会在这里,看着 graphql-playground 我收到这条消息

"debugMessage": "Lighthouse failed while trying to load a type: App\\GraphQL\\Types\\ProductDish\n\nMake sure the type is present in your schema definition.\n"

问题是我不确定 1)这是否是正确的方法 2)为什么 lighthouse 无法加载类型。

你能告诉我处理这个问题的正确方法吗?

请记住

  • ProductDishProductDrink不是Eloquent 模型,而是 Product 类型上产品 json 字段属性的简单“包装器”,这是一个 Eloquent 模型

  • ProductServing也是如此,它是一个键:值对

  • 如果我删除工会,一切都会正常。我的意思是,如果 Product 类型具有分配给 ProductDish 类型或 ProductDrink 的属性,一切都会顺利进行,但我需要联合


PIPIONE
浏览 60回答 1
1回答

慕姐4208626

我走在正确的轨道上,但我需要解决一些问题首先,在ProductProperties文件内部,注册表需要一个简单的字符串而不是名称空间,所以我必须键入->get("ProductDish")而不是->get(ProductDish::class)然后删除__type类型中的字段,以及 graphql 模式,因为我可以在模型内部使用变异器Product并根据某些参数确定哪种类型,像这样的东西public function getPropertisAttribute($properties) {       $properties = json_decode($properties, true);   $properties["__type"] = // .. some logic to get the right type   return $properties;}一旦我有了类型,我就可以将其用回我的ProductProperties联合类中
打开App,查看更多内容
随时随地看视频慕课网APP