在 Laravel 6、Lighthouse 5 中,方法 __invoke 的参数 $args,其内部顺序与前端请求参数不一致
1、前端请求参数如下所示。如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | { "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": "<img draggable="false" role="img" class="emoji" alt=" }, { "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。代码实现如下
1 2 3 4 5 6 | 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 位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | { "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": "<img draggable="false" role="img" class="emoji" alt=" }, { "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 位。
1 2 | print_r( $context ->request()->input( 'variables' )); exit ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | { "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": "<img draggable="false" role="img" class="emoji" alt=" }, { "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 位。代码实现如下
1 2 3 | $content = json_decode( $context ->request()->getContent(), true); echo json_encode( $content [ 'variables' ]); exit ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | { "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": "<img draggable="false" role="img" class="emoji" alt=" }, { "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 条回复
[…] […]