你好嗎?我正在與聯(lián)合斗爭,我想對基于自定義類型的聯(lián)合類型的自定義解析器進(jìn)行一些澄清。Mi 當(dāng)前架構(gòu)是product.graphql 在內(nèi)部導(dǎo)入的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!} 當(dāng)然,僅此模式是行不通的,因?yàn)?Lighthouse 無法理解自定義類型。我為類型創(chuàng)建了兩個類:// 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 的聯(lián)合類這不起作用,否則我就不會在這里,看著 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 類型上產(chǎn)品 json 字段屬性的簡單“包裝器”,這是一個 Eloquent 模型ProductServing也是如此,它是一個鍵:值對如果我刪除工會,一切都會正常。我的意思是,如果 Product 類型具有分配給 ProductDish 類型或 ProductDrink 的屬性,一切都會順利進(jìn)行,但我需要聯(lián)合
1 回答

慕姐4208626
TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個贊
我走在正確的軌道上,但我需要解決一些問題
首先,在ProductProperties文件內(nèi)部,注冊表需要一個簡單的字符串而不是名稱空間,所以我必須鍵入->get("ProductDish")而不是->get(ProductDish::class)然后刪除__type類型中的字段,以及 graphql 模式,因?yàn)槲铱梢栽谀P蛢?nèi)部使用變異器Product并根據(jù)某些參數(shù)確定哪種類型,像這樣的東西
public function getPropertisAttribute($properties) {
$properties = json_decode($properties, true);
$properties["__type"] = // .. some logic to get the right type
return $properties;
}
一旦我有了類型,我就可以將其用回我的ProductProperties聯(lián)合類中
- 1 回答
- 0 關(guān)注
- 206 瀏覽
添加回答
舉報
0/150
提交
取消