|
| 1 | +import shopify from "../../shopify.app.mjs"; |
| 2 | +import { axios } from "@pipedream/platform"; |
| 3 | +import fs from "fs"; |
| 4 | +import FormData from "form-data"; |
| 5 | + |
| 6 | +export default { |
| 7 | + key: "shopify-bulk-import", |
| 8 | + name: "Bulk Import", |
| 9 | + description: "Add tags. [See the documentation](https://shopify.dev/docs/api/admin-graphql/latest/mutations/bulkoperationrunmutation)", |
| 10 | + version: "0.0.1", |
| 11 | + type: "action", |
| 12 | + props: { |
| 13 | + shopify, |
| 14 | + alert: { |
| 15 | + type: "alert", |
| 16 | + alertType: "info", |
| 17 | + content: "Use the Shopify trigger \"New Event Emitted (Instant)\" with event type `bulk_operations/finish` to receive notifications when bulk imports are completed", |
| 18 | + }, |
| 19 | + mutation: { |
| 20 | + type: "string", |
| 21 | + label: "Mutation", |
| 22 | + description: "The mutation to be executed in bulk. [See the documentation](https://shopify.dev/docs/api/usage/bulk-operations/imports) for a list of supported mutations. Example: `mutation call($input: ProductInput!) { productCreate(input: $input) { product { id title } } }`", |
| 23 | + }, |
| 24 | + filePath: { |
| 25 | + type: "string", |
| 26 | + label: "File Path", |
| 27 | + description: "File path in the `/tmp` directory of a JSONL file including the variables for the mutation. Each line in the JSONL file represents one input unit. The mutation runs once on each line of the input file. [See the documentation](https://shopify.dev/docs/api/usage/bulk-operations/imports) for more information.", |
| 28 | + }, |
| 29 | + clientIdentifier: { |
| 30 | + type: "string", |
| 31 | + label: "Client Identifier", |
| 32 | + description: "An optional identifier which may be used for querying", |
| 33 | + optional: true, |
| 34 | + }, |
| 35 | + }, |
| 36 | + methods: { |
| 37 | + stagedUploadQuery() { |
| 38 | + return ` |
| 39 | + mutation stagedUploadsCreate($input: [StagedUploadInput!]!) { |
| 40 | + stagedUploadsCreate(input: $input) { |
| 41 | + stagedTargets { |
| 42 | + url |
| 43 | + resourceUrl |
| 44 | + parameters { |
| 45 | + name |
| 46 | + value |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + `; |
| 52 | + }, |
| 53 | + bulkImportMutation() { |
| 54 | + return ` |
| 55 | + mutation bulkOperationRunMutation($clientIdentifier: String, $mutation: String!, $stagedUploadPath: String!) { |
| 56 | + bulkOperationRunMutation(clientIdentifier: $clientIdentifier, mutation: $mutation, stagedUploadPath: $stagedUploadPath) { |
| 57 | + bulkOperation { |
| 58 | + id |
| 59 | + completedAt |
| 60 | + createdAt |
| 61 | + fileSize |
| 62 | + objectCount |
| 63 | + rootObjectCount |
| 64 | + partialDataUrl |
| 65 | + query |
| 66 | + status |
| 67 | + url |
| 68 | + } |
| 69 | + userErrors { |
| 70 | + field |
| 71 | + message |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + `; |
| 76 | + }, |
| 77 | + }, |
| 78 | + async run({ $ }) { |
| 79 | + const filePath = this.filePath.includes("/tmp") |
| 80 | + ? this.filePath |
| 81 | + : `/tmp/${this.filePath}`; |
| 82 | + const filename = filePath.split("/").pop(); |
| 83 | + |
| 84 | + // create staged upload path |
| 85 | + |
| 86 | + const { stagedUploadsCreate: { stagedTargets } } |
| 87 | + = await this.shopify._makeGraphQlRequest(this.stagedUploadQuery(), { |
| 88 | + input: [ |
| 89 | + { |
| 90 | + resource: "BULK_MUTATION_VARIABLES", |
| 91 | + filename, |
| 92 | + mimeType: "text/jsonl", |
| 93 | + httpMethod: "POST", |
| 94 | + }, |
| 95 | + ], |
| 96 | + }); |
| 97 | + |
| 98 | + const { |
| 99 | + url, parameters, |
| 100 | + } = stagedTargets[0]; |
| 101 | + |
| 102 | + // upload file to staged upload path |
| 103 | + |
| 104 | + let stagedUploadPath; |
| 105 | + const form = new FormData(); |
| 106 | + parameters.forEach(({ |
| 107 | + name, value, |
| 108 | + }) => { |
| 109 | + form.append(name, value); |
| 110 | + if (name === "key") { |
| 111 | + stagedUploadPath = value; |
| 112 | + } |
| 113 | + }); |
| 114 | + form.append("file", fs.createReadStream(filePath)); |
| 115 | + |
| 116 | + await axios($, { |
| 117 | + url, |
| 118 | + method: "POST", |
| 119 | + headers: form.getHeaders(), |
| 120 | + data: form, |
| 121 | + debug: true, |
| 122 | + }); |
| 123 | + |
| 124 | + // perform bulk import |
| 125 | + |
| 126 | + const response = await this.shopify._makeGraphQlRequest(this.bulkImportMutation(), { |
| 127 | + mutation: this.mutation, |
| 128 | + stagedUploadPath, |
| 129 | + clientIdentifier: this.clientIdentifier, |
| 130 | + }); |
| 131 | + |
| 132 | + if (response.bulkOperationRunMutation.userErrors.length > 0) { |
| 133 | + throw new Error(response.bulkOperationRunMutation.userErrors[0].message); |
| 134 | + } |
| 135 | + |
| 136 | + $.export("$summary", "Successfully completed bulk import"); |
| 137 | + return response; |
| 138 | + }, |
| 139 | +}; |
0 commit comments