Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions components/shopify/actions/bulk-import/bulk-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import shopify from "../../shopify.app.mjs";
import { axios } from "@pipedream/platform";
import fs from "fs";
import FormData from "form-data";

export default {
key: "shopify-bulk-import",
name: "Bulk Import",
description: "Add tags. [See the documentation](https://shopify.dev/docs/api/admin-graphql/latest/mutations/bulkoperationrunmutation)",
version: "0.0.1",
type: "action",
props: {
shopify,
alert: {

Check warning on line 14 in components/shopify/actions/bulk-import/bulk-import.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a label. See https://pipedream.com/docs/components/guidelines/#props

Check warning on line 14 in components/shopify/actions/bulk-import/bulk-import.mjs

View workflow job for this annotation

GitHub Actions / Lint Code Base

Component prop alert must have a description. See https://pipedream.com/docs/components/guidelines/#props
type: "alert",
alertType: "info",
content: "Use the Shopify trigger \"New Event Emitted (Instant)\" with event type `bulk_operations/finish` to receive notifications when bulk imports are completed",
},
mutation: {
type: "string",
label: "Mutation",
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 } } }`",
},
filePath: {
type: "string",
label: "File Path",
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.",
},
clientIdentifier: {
type: "string",
label: "Client Identifier",
description: "An optional identifier which may be used for querying",
optional: true,
},
},
methods: {
stagedUploadQuery() {
return `
mutation stagedUploadsCreate($input: [StagedUploadInput!]!) {
stagedUploadsCreate(input: $input) {
stagedTargets {
url
resourceUrl
parameters {
name
value
}
}
}
}
`;
},
bulkImportMutation() {
return `
mutation bulkOperationRunMutation($clientIdentifier: String, $mutation: String!, $stagedUploadPath: String!) {
bulkOperationRunMutation(clientIdentifier: $clientIdentifier, mutation: $mutation, stagedUploadPath: $stagedUploadPath) {
bulkOperation {
id
completedAt
createdAt
fileSize
objectCount
rootObjectCount
partialDataUrl
query
status
url
}
userErrors {
field
message
}
}
}
`;
},
},
async run({ $ }) {
const filePath = this.filePath.includes("/tmp")
? this.filePath
: `/tmp/${this.filePath}`;
const filename = filePath.split("/").pop();

// create staged upload path

const { stagedUploadsCreate: { stagedTargets } }
= await this.shopify._makeGraphQlRequest(this.stagedUploadQuery(), {
input: [
{
resource: "BULK_MUTATION_VARIABLES",
filename,
mimeType: "text/jsonl",
httpMethod: "POST",
},
],
});

const {
url, parameters,
} = stagedTargets[0];

// upload file to staged upload path

let stagedUploadPath;
const form = new FormData();
parameters.forEach(({
name, value,
}) => {
form.append(name, value);
if (name === "key") {
stagedUploadPath = value;
}
});
form.append("file", fs.createReadStream(filePath));

await axios($, {
url,
method: "POST",
headers: form.getHeaders(),
data: form,
});

// perform bulk import

const response = await this.shopify._makeGraphQlRequest(this.bulkImportMutation(), {
mutation: this.mutation,
stagedUploadPath,
clientIdentifier: this.clientIdentifier,
});

if (response.bulkOperationRunMutation.userErrors.length > 0) {
throw new Error(response.bulkOperationRunMutation.userErrors[0].message);
}

$.export("$summary", "Successfully completed bulk import");
return response;
},
};
3 changes: 2 additions & 1 deletion components/shopify/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/shopify",
"version": "0.6.8",
"version": "0.6.9",
"description": "Pipedream Shopify Components",
"main": "shopify.app.mjs",
"keywords": [
Expand All @@ -13,6 +13,7 @@
"@pipedream/platform": "^3.0.3",
"async-retry": "^1.3.3",
"bottleneck": "^2.19.5",
"form-data": "^4.0.2",
"lodash.get": "^4.4.2",
"lodash.topath": "^4.5.2",
"shopify-api-node": "^3.14.2"
Expand Down
Loading
Loading