在 GraphQL Mutation 中,在同一次 HTTP 请求中,执行多个 Mutation 的实现
1、在 GraphQL Mutation 中,在同一次 HTTP 请求中,现在仅执行了一个 Mutation。如图1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | mutation UploadThemeAsset($input: ThemeAssetUploadInput!) { onlineStoreThemeAssetUpload(input: $input) { themeAsset { id themeId value key publicUrl mimeType checksum category warnings createdAt updatable deletable renameable updatable } } } |
1 2 3 4 5 6 7 | { "input": { "themeId": "{{themeId}}", "key": "assets/js/1.js", "value": "string" } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | { "data": { "onlineStoreThemeAssetUpload": { "themeAsset": { "id": "346186", "themeId": "98ad1110-014d-4a61-bbb4-d74bc4ed30f7", "value": "string", "key": "assets/js/1.js", "publicUrl": null, "mimeType": "text/plain", "checksum": "ecb252044b5ea0f679ee78ec1a12904739e2904d", "category": "asset", "warnings": [], "createdAt": "2023-03-14 17:36:36", "updatable": false, "deletable": false, "renameable": false } } } } |
2、当单位时间内 HTTP 请求数量过多时,会触发接口的限流。如图2
3、计划在同一次 HTTP 请求中,执行多个 Mutation。参考:https://graphql.cn/learn/queries/#aliases ,使用别名。如图3
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 | mutation UploadThemeAsset($input1: ThemeAssetUploadInput!, $input2: ThemeAssetUploadInput!) { themeAssetUpload1: onlineStoreThemeAssetUpload(input: $input1) { themeAsset { ...themeAssetFields } } themeAssetUpload2: onlineStoreThemeAssetUpload(input: $input2) { themeAsset { ...themeAssetFields } } } fragment themeAssetFields on ThemeAsset { id themeId value key publicUrl mimeType checksum category warnings createdAt updatable deletable renameable updatable } |
1 2 3 4 5 6 7 8 9 10 11 12 | { "input1": { "themeId": "{{themeId}}", "key": "assets/js/1.js", "value": "string" }, "input2": { "themeId": "{{themeId}}", "key": "assets/js/2.js", "value": "string" } } |
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 | { "data": { "themeAssetUpload1": { "themeAsset": { "id": "346186", "themeId": "98ad1110-014d-4a61-bbb4-d74bc4ed30f7", "value": "string", "key": "assets/js/1.js", "publicUrl": null, "mimeType": "text/plain", "checksum": "ecb252044b5ea0f679ee78ec1a12904739e2904d", "category": "asset", "warnings": [], "createdAt": "2023-03-14 17:36:36", "updatable": false, "deletable": false, "renameable": false } }, "themeAssetUpload2": { "themeAsset": { "id": "346187", "themeId": "98ad1110-014d-4a61-bbb4-d74bc4ed30f7", "value": "string", "key": "assets/js/2.js", "publicUrl": null, "mimeType": "text/plain", "checksum": "ecb252044b5ea0f679ee78ec1a12904739e2904d", "category": "asset", "warnings": [], "createdAt": "2023-03-14 18:04:55", "updatable": false, "deletable": false, "renameable": false } } } } |
近期评论