亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Lighthouse 找不到我的 Union 自定義類型

Lighthouse 找不到我的 Union 自定義類型

PHP
PIPIONE 2023-11-03 15:29:19
你好嗎?我正在與聯合斗爭,我想對基于自定義類型的聯合類型的自定義解析器進行一些澄清。Mi 當前架構是product.graphql 在內部導入的schema.graphqltype ProductServing {    name: String    price: Float}type ProductDish {    price: Float    calories: Int    servings: [ProductServing]}type ProductDrink {    price: Float    servings: [ProductServing]}union ProductProperties = ProductDish | ProductDrinktype 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\Typesclass 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 無法加載類型。你能告訴我處理這個問題的正確方法嗎?請記住ProductDish和ProductDrink不是Eloquent 模型,而是 Product 類型上產品 json 字段屬性的簡單“包裝器”,這是一個 Eloquent 模型ProductServing也是如此,它是一個鍵:值對如果我刪除工會,一切都會正常。我的意思是,如果 Product 類型具有分配給 ProductDish 類型或 ProductDrink 的屬性,一切都會順利進行,但我需要聯合
查看完整描述

1 回答

?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

我走在正確的軌道上,但我需要解決一些問題


首先,在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聯合類中


查看完整回答
反對 回復 2023-11-03
  • 1 回答
  • 0 關注
  • 218 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號