在 Laravel 6、Lighthouse 中,debugMessage”: “Expected a value of type \”String\” but received: [\”en-US\”,\”zh-CN\”,\”sp-ES\”,\”fr-FR\”,\”de-DE\”,\”pt-PT\”,\”it-IT\”]
1、在 Laravel 6、Lighthouse 中,debugMessage”: “Expected a value of type \”String\” but received: [\”en-US\”,\”zh-CN\”,\”sp-ES\”,\”fr-FR\”,\”de-DE\”,\”pt-PT\”,\”it-IT\”]。如图1
{ "errors": [ { "debugMessage": "Expected a value of type \"String\" but received: [\"en-US\",\"zh-CN\",\"sp-ES\",\"fr-FR\",\"de-DE\",\"pt-PT\",\"it-IT\"]", "message": "Internal server error", "extensions": { "category": "internal" }, "locations": [ { "line": 34, "column": 9 } ], "path": [ "onlineStoreTheme", "current", "settings", 31, "value" ], "trace": ... }, { "debugMessage": "Cannot return null for non-nullable field \"ThemePreset.sections\".", "message": "Internal server error", "extensions": { "category": "internal" }, "locations": [ { "line": 139, "column": 3 } ], "path": [ "onlineStoreTheme", "current", "sections", 0 ], "trace": ... } ], "data": { "onlineStoreTheme": null } }
2、打印 dd($resolvedData[‘current’][‘settings’][31]); ,结果如下。第 31 个元素的 value ,为数组格式。如图2
Illuminate\Support\Collection {#11225 #items: array:33 [ "available_languages" => array:4 [ "id" => Modules\ThemeStore\Resolver\Id {#11223 #prefixes: array:3 [ 0 => "theme" 1 => "current" 2 => "available_languages" ] } "settingId" => "available_languages" "value" => array:7 [ 0 => "en-US" 1 => "zh-CN" 2 => "sp-ES" 3 => "fr-FR" 4 => "de-DE" 5 => "pt-PT" 6 => "it-IT" ] "type" => "select" ] ] }
3、编辑解析器方法,var_dump($value);,打印出值的列表
int(0) int(0) string(7) "#ff0000" string(7) "#000000" string(7) "#990000" string(7) "#212529" string(7) "#999999" string(7) "#c90d0d" string(40) "linear-gradient(90deg, #c90d0d, #c90d0d)" string(7) "#c90d0d" string(40) "linear-gradient(90deg, #c90d0d, #c90d0d)" string(7) "#ffffff" string(40) "linear-gradient(90deg, #ffffff, #ffffff)" string(7) "#f9f1e6" string(40) "linear-gradient(90deg, #f9f1e6, #f9f1e6)" string(9) "Open Sans" string(9) "Open Sans" string(9) "Open Sans" NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL bool(false) string(5) "en-US" bool(false) array(7) { [0]=> string(5) "en-US" [1]=> string(5) "zh-CN" [2]=> string(5) "sp-ES" [3]=> string(5) "fr-FR" [4]=> string(5) "de-DE" [5]=> string(5) "pt-PT" [6]=> string(5) "it-IT" } string(6) "global"
4、由于 value 的类型设置为:String,因此,需要将数组转换为 String 格式。否则会验证响应格式失败。
'value' => is_array($value) ? json_encode($value) : $value,
5、”debugMessage”: “Expected a value of type \”String\” but received: [\”en-US\”,\”zh-CN\”,\”sp-ES\”,\”fr-FR\”,\”de-DE\”,\”pt-PT\”,\”it-IT\”]”, 已经消失。符合预期。仅剩下 “debugMessage”: “Cannot return null for non-nullable field \”ThemePreset.sections\”.”。如图3
6、打印 dd($this->resolveSectionSettings($preset, $themeSettingSchema, $id));。Illuminate\Support\Collection {#11189 #items: array:3 [ "header" => null "footer" => null "announcement-bar" => null ] }
7、最后找到原因,在于集合中的回调函数未 return 。如图4
private function resolveSectionSettings(ThemeSettingSet $preset, Theme $themeSettingSchema, Id $id) { $themeId = $this->viewStorage->getThemeId(); return collect($preset->getSections()) ->map(function(SectionSetting $sectionSetting, $sectionId) use($themeSettingSchema, $id, $themeId) { $sectionSchema = $themeSettingSchema->getSection($sectionId); $globalSectionId = $id->with($sectionId); return $this->resolveSectionSetting($globalSectionId, $sectionId, $themeId, $sectionSetting, $sectionSchema); }); }
8、请求响应 200。如图5
近期评论