在 Laravel 6、Lighthouse 5 中,方法 __invoke 的参数 $args,其内部顺序与前端请求参数不一致
1、前端请求参数如下所示。如图1
{ "query": "\nmutation ThemeUpdate($themeId: ID!, $settings: [ThemeSettingDataInput!], $sections: [ThemeSettingsDataSectionInput!], $appEmbeds: [ThemeSettingsDataBlockInput!], $sessionId: String!) {\n onlineStoreThemeSettingsDataUpdate(\n themeId: $themeId\n settings: $settings\n sections: $sections\n appEmbeds: $appEmbeds\n sessionId: $sessionId\n ) {\n sessionId\n __typename\n }\n}\n", "variables": { "themeId": "97cdd852-2ca0-48eb-b119-6afb68e2254c", "settings": [], "sections": [ { "sectionId": "announcement-bar", "disabled": false, "type": "announcement-bar", "settings": [ { "settingId": "enable", "value": "true" }, { "settingId": "homepage_only", "value": "false" }, { "settingId": "sticky", "value": "false" } ], "blocks": [ { "blockId": "announcement-bar-0", "disabled": true, "type": "announcement", "settings": [ { "settingId": "text", "value": "❤Free Shipping Over $100.0❤" }, { "settingId": "image", "value": "" }, { "settingId": "mobile_image", "value": "" }, { "settingId": "background_color", "value": "#000000" }, { "settingId": "text_color", "value": "#ffffff" }, { "settingId": "link", "value": "" } ] } ] }, { "sectionId": "header", "disabled": false, "type": "header", "settings": [ { "settingId": "sticky", "value": "false" }, { "settingId": "menu", "value": "" }, { "settingId": "secondary_menu", "value": "" }, { "settingId": "text_color", "value": "#000000" }, { "settingId": "background_color", "value": "#ffffff" }, { "settingId": "transparent", "value": "true" }, { "settingId": "transparent_text_color", "value": "#ffffff" }, { "settingId": "logo", "value": "" }, { "settingId": "logo_max_width", "value": "180" } ], "blocks": [] }, { "sectionId": "footer", "disabled": false, "type": "footer", "settings": [ { "settingId": "style", "value": "first" }, { "settingId": "1", "value": "" }, { "settingId": "text_color", "value": "#222222" }, { "settingId": "background_color", "value": "#f7f7f9" }, { "settingId": "4", "value": "" }, { "settingId": "show_payment_icons", "value": "false" }, { "settingId": "show_dmca", "value": "true" } ], "blocks": [] } ], "appEmbeds": [], "sessionId": "gY32VAiyjotH8tsfJQZ5aopd9okgAZE4dExf" }, "operationName": "ThemeUpdate" }
2、后端将方法 __invoke 的参数 $args 转换为 json,其对应前端请求体中的字段:variables。代码实现如下
public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) { $variables = json_encode($args); echo $variables; exit; }
3、打印 json 如下。多出了字段:directive,然后 json 内部的 key 的顺序与前端不一致。appEmbeds 的位置本应该在第 4 位,现在在第 2 位
{ "themeId": "97cdd852-2ca0-48eb-b119-6afb68e2254c", "appEmbeds": [], "settings": [], "sections": [ { "sectionId": "announcement-bar", "type": "announcement-bar", "disabled": false, "settings": [ { "settingId": "enable", "value": "true" }, { "settingId": "homepage_only", "value": "false" }, { "settingId": "sticky", "value": "false" } ], "blocks": [ { "blockId": "announcement-bar-0", "type": "announcement", "disabled": true, "settings": [ { "settingId": "text", "value": "❤Free Shipping Over $100.0❤" }, { "settingId": "image", "value": "" }, { "settingId": "mobile_image", "value": "" }, { "settingId": "background_color", "value": "#000000" }, { "settingId": "text_color", "value": "#ffffff" }, { "settingId": "link", "value": "" } ] } ] }, { "sectionId": "header", "type": "header", "disabled": false, "settings": [ { "settingId": "sticky", "value": "false" }, { "settingId": "menu", "value": "" }, { "settingId": "secondary_menu", "value": "" }, { "settingId": "text_color", "value": "#000000" }, { "settingId": "background_color", "value": "#ffffff" }, { "settingId": "transparent", "value": "true" }, { "settingId": "transparent_text_color", "value": "#ffffff" }, { "settingId": "logo", "value": "" }, { "settingId": "logo_max_width", "value": "180" } ], "blocks": [] }, { "sectionId": "footer", "type": "footer", "disabled": false, "settings": [ { "settingId": "style", "value": "first" }, { "settingId": "1", "value": "" }, { "settingId": "text_color", "value": "#222222" }, { "settingId": "background_color", "value": "#f7f7f9" }, { "settingId": "4", "value": "" }, { "settingId": "show_payment_icons", "value": "false" }, { "settingId": "show_dmca", "value": "true" } ], "blocks": [] } ], "sessionId": "gY32VAiyjotH8tsfJQZ5aopd9okgAZE4dExf", "directive": null }
4、基于 Illuminate\Http\Request 获取请求参数。其顺序仍然不一致。appEmbeds 的位置本应该在第 4 位,现在在第 2 位。
print_r($context->request()->input('variables')); exit;
{ "themeId": "97cdd852-2ca0-48eb-b119-6afb68e2254c", "settings": [], "sections": [ { "sectionId": "announcement-bar", "disabled": false, "type": "announcement-bar", "settings": [ { "settingId": "enable", "value": "true" }, { "settingId": "homepage_only", "value": "false" }, { "settingId": "sticky", "value": "false" } ], "blocks": [ { "blockId": "announcement-bar-0", "disabled": false, "type": "announcement", "settings": [ { "settingId": "text", "value": "❤Free Shipping Over $100.0❤" }, { "settingId": "image", "value": null }, { "settingId": "mobile_image", "value": null }, { "settingId": "background_color", "value": "#000000" }, { "settingId": "text_color", "value": "#ffffff" }, { "settingId": "link", "value": null } ] } ] }, { "sectionId": "header", "disabled": false, "type": "header", "settings": [ { "settingId": "sticky", "value": "false" }, { "settingId": "menu", "value": null }, { "settingId": "secondary_menu", "value": null }, { "settingId": "text_color", "value": "#000000" }, { "settingId": "background_color", "value": "#ffffff" }, { "settingId": "transparent", "value": "true" }, { "settingId": "transparent_text_color", "value": "#ffffff" }, { "settingId": "logo", "value": null }, { "settingId": "logo_max_width", "value": "180" } ], "blocks": [] }, { "sectionId": "footer", "disabled": false, "type": "footer", "settings": [ { "settingId": "style", "value": "first" }, { "settingId": "1", "value": null }, { "settingId": "text_color", "value": "#222222" }, { "settingId": "background_color", "value": "#f7f7f9" }, { "settingId": "4", "value": null }, { "settingId": "show_payment_icons", "value": "false" }, { "settingId": "show_dmca", "value": "true" } ], "blocks": [] } ], "appEmbeds": [], "sessionId": "gY32VAiyjotH8tsfJQZ5aopd9okgAZE4dExf" }
5、基于 Illuminate\Http\Request 获取请求体的原始内容。前后端顺序一致。appEmbeds 的位置现在第 4 位。代码实现如下
$content = json_decode($context->request()->getContent(), true); echo json_encode($content['variables']); exit;
{ "themeId": "97cdd852-2ca0-48eb-b119-6afb68e2254c", "settings": [], "sections": [ { "sectionId": "announcement-bar", "disabled": false, "type": "announcement-bar", "settings": [ { "settingId": "enable", "value": "true" }, { "settingId": "homepage_only", "value": "false" }, { "settingId": "sticky", "value": "false" } ], "blocks": [ { "blockId": "announcement-bar-0", "disabled": false, "type": "announcement", "settings": [ { "settingId": "text", "value": "❤Free Shipping Over $100.0❤" }, { "settingId": "image", "value": "" }, { "settingId": "mobile_image", "value": "" }, { "settingId": "background_color", "value": "#000000" }, { "settingId": "text_color", "value": "#ffffff" }, { "settingId": "link", "value": "" } ] } ] }, { "sectionId": "header", "disabled": false, "type": "header", "settings": [ { "settingId": "sticky", "value": "false" }, { "settingId": "menu", "value": "" }, { "settingId": "secondary_menu", "value": "" }, { "settingId": "text_color", "value": "#000000" }, { "settingId": "background_color", "value": "#ffffff" }, { "settingId": "transparent", "value": "true" }, { "settingId": "transparent_text_color", "value": "#ffffff" }, { "settingId": "logo", "value": "" }, { "settingId": "logo_max_width", "value": "180" } ], "blocks": [] }, { "sectionId": "footer", "disabled": false, "type": "footer", "settings": [ { "settingId": "style", "value": "first" }, { "settingId": "1", "value": "" }, { "settingId": "text_color", "value": "#222222" }, { "settingId": "background_color", "value": "#f7f7f9" }, { "settingId": "4", "value": "" }, { "settingId": "show_payment_icons", "value": "false" }, { "settingId": "show_dmca", "value": "true" } ], "blocks": [] } ], "appEmbeds": [], "sessionId": "gY32VAiyjotH8tsfJQZ5aopd9okgAZE4dExf" }
1 条回复
[…] […]