Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const id = coll.collection.id;
- const { loading, error, data, fetchMore, extensions, refetch } = useQuery(GET_COLLECTION, {
- variables: { id }
- });
- const [updatePrice] = useMutation(UPDATE_PRICE);
- // Given a product, returns a promise that resolves when all variants are processed
- async function process_product(product){
- const variants = product.node.variants.edges;
- for (let i = 0; i < variants.length; i++){
- await process_variant(variants[i]);
- }
- }
- // Given a variant, returns a promise after the product is processed
- async function process_variant(variant){
- if (variant) {
- console.log('doing stuff')
- }
- else {
- console.log('doing other stuff')
- }
- const productVariableInput = {};
- const variables = { input: productVariableInput };
- try {
- const {data, extensions} = await updatePrice({ variables });
- const remaining_throttle = extensions.cost.throttleStatus.currentlyAvailable;
- console.log("Remaining", remaining_throttle)
- console.log(data)
- // Change to a while loop to make sure you actually wait until resources are available
- if (remaining_throttle < 100) {
- console.log("WAITING")
- await wait(18000);
- }
- } catch (e) {
- console.log('error:', e);
- }
- console.log("AFTER")
- return 0;
- }
- const redirectToModify = async (data, totalProducts) => {
- await wait(20000);
- let cursor;
- const products = data.collection.product.edges;
- totalProducts = totalProducts - products.length;
- // Wait for all products to finish processing
- for (var i = 0; i < products.length; i++){
- await process_product(products[i]);
- }
- if (totalProducts > 0) {
- console.log("Calling")
- await wait(15000);
- handleDiscountMore(data, cursor, totalProducts)
- }
- };
- function updateQuery(previousResult, { fetchMoreResult }){
- console.log("adding", fetchMoreResult);
- redirectToModify(fetchMoreResult, pc);
- return fetchMoreResult;
- }
- //Below function is Just for reference. It gets called before checking the throttleStatus above. afaik there's no problem with this
- function handleDiscountMore(data, cursor, pc) {
- console.log("Call received")
- const variables = { id: data.collection.id, cursor };
- fetchMore({ variables, updateQuery })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement