-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Shopify - Bulk Import #15847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Shopify - Bulk Import #15847
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: "Execute bulk mutations by uploading a JSONL file containing mutation variables. [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
|
||
| 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(); | ||
|
|
||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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, | ||
| }); | ||
|
|
||
michelle0927 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 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; | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.