{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup\n",
    "# Install the required package\n",
    "# %pip install requests dotenv\n",
    "# %pip install dotenv\n",
    "# %pip install --upgrade ShopifyAPI\n",
    "\n",
    "# Import necessary libraries\n",
    "from dotenv import dotenv_values\n",
    "import requests\n",
    "import json\n",
    "\n",
    "def pretty_print_POST(req):\n",
    "    print('{}\\n{}\\r\\n{}\\r\\n\\r\\n{}'.format(\n",
    "        '-----------START-----------',\n",
    "        req.method + ' ' + req.url,\n",
    "        '\\r\\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),\n",
    "        req.body,\n",
    "    ))\n",
    "\n",
    "# Define a function to send a GraphQL query or mutation\n",
    "def send_graphql_request(endpointUrl, accessToken, query, variables=None, verbose=False):\n",
    "    # Create the request payload\n",
    "    payload = {\n",
    "        \"query\": query,\n",
    "        \"variables\": variables\n",
    "    }\n",
    "    \n",
    "    # Set the headers\n",
    "    headers = {\n",
    "        \"Content-Type\": \"application/json\",\n",
    "        \"X-Shopify-Access-Token\": accessToken,\n",
    "    }\n",
    "\n",
    "    req = requests.Request('POST', endpointUrl, headers=headers, json=payload)\n",
    "    prepared = req.prepare()\n",
    "    if verbose:\n",
    "        pretty_print_POST(prepared)\n",
    "    \n",
    "    # Send the POST request to the GraphQL endpoint\n",
    "    s = requests.Session()\n",
    "    response = s.send(prepared)\n",
    "    \n",
    "    # Check if the response status code is 200 (OK)\n",
    "    if response.status_code == 200:\n",
    "        # Parse and return the response JSON\n",
    "        output = response.json()\n",
    "        if 'extensions' in output:\n",
    "            del output['extensions']\n",
    "        return output\n",
    "    else:\n",
    "        # Return the error details\n",
    "        return {\n",
    "            \"error\": f\"Request failed with status code {response.status_code}\",\n",
    "            \"response\": response.text\n",
    "        }\n",
    "\n",
    "SHOP = 'bigscreenvr'\n",
    "config = dotenv_values(\"../.env\")\n",
    "# accessToken = config['SHOPIFY_TOKEN']\n",
    "# endpointUrl = f\"https://{SHOP}.myshopify.com/admin/api/2024-07/graphql.json\"\n",
    "draftCheckoutId = \"gid://shopify/CheckoutProfile/1710457049\"\n",
    "\n",
    "# BigFab Automation\n",
    "accessToken = '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'\n",
    "endpointUrl = f\"https://{SHOP}.myshopify.com/admin/api/2025-01/graphql.json\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# QUERY - Get font details\n",
    "query = \"\"\"\n",
    "query BrandingQuery {\n",
    "  checkoutBranding(checkoutProfileId: \"gid://shopify/CheckoutProfile/1710457049\") {\n",
    "    designSystem {\n",
    "      typography {\n",
    "        primary {\n",
    "          base {\n",
    "            sources\n",
    "            weight\n",
    "            ... on CheckoutBrandingCustomFont {\n",
    "              genericFileId\n",
    "              sources\n",
    "              weight\n",
    "            }\n",
    "            ... on CheckoutBrandingShopifyFont {\n",
    "              sources\n",
    "              weight\n",
    "            }\n",
    "          }\n",
    "          loadingStrategy\n",
    "          name\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# QUERY - Misc. experiments\n",
    "etTokenAssignedSegID = \"gid://shopify/Segment/530594529497\"\n",
    "\n",
    "query = \"\"\"\n",
    "query ReturnStatus {\n",
    "  customerSegmentMembers(first: 500, segmentId: \"gid://shopify/Segment/530594529497\") {\n",
    "    edges {\n",
    "      node {\n",
    "        id\n",
    "        defaultEmailAddress {\n",
    "          emailAddress}\n",
    "        displayName\n",
    "        metafield(namespace: \"custom\", key: \"eyetracking_beta_token\") {\n",
    "          value\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(response['data']['customerSegmentMembers']['edges'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# QUERY - Get uploaded files (with optional filter)\n",
    "query = \"\"\"\n",
    "query queryFiles {\n",
    "  files(first: 10, query: \"filename:Empty*\") {\n",
    "    edges {\n",
    "      node {\n",
    "        ... on File {\n",
    "          id\n",
    "          fileStatus\n",
    "          preview {\n",
    "            image {\n",
    "              url\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "# Delete any list entry under the \"edges\" key which does not contain: a \"url\" key where the value contains filter\n",
    "# filter = 'android-chrome'\n",
    "# response['data']['files']['edges'] = [edge for edge in response['data']['files']['edges'] if 'url' in edge['node'] and filter in edge['node']['url']]\n",
    "\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Update corner radius, font size\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      designSystem {\n",
    "        cornerRadius {\n",
    "          small,\n",
    "          base,\n",
    "          large\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "  \"checkoutProfileId\": draftCheckoutId,\n",
    "  \"input\": {\n",
    "    \"designSystem\": {\n",
    "      \"cornerRadius\": {\n",
    "        \"small\": 100,\n",
    "        \"base\": 8,\n",
    "        \"large\": 8\n",
    "      },\n",
    "      \"typography\": {\n",
    "        \"size\": {\n",
    "          \"base\": 16,\n",
    "          \"ratio\": 1.125\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Hide cart link\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      customizations {\n",
    "        cartLink {\n",
    "          visibility\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "  \"checkoutProfileId\": draftCheckoutId,\n",
    "  \"input\": {\n",
    "    \"customizations\": {\n",
    "      \"cartLink\": {\n",
    "        \"visibility\": \"HIDDEN\"\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Set fonts\n",
    "regularFont = \"gid://shopify/GenericFile/35447479894233\"\n",
    "mediumFont = \"gid://shopify/GenericFile/35447479828697\"\n",
    "boldFont = \"gid://shopify/GenericFile/35437831291097\"\n",
    "jpRegularFont = \"gid://shopify/GenericFile/35438798504153\"\n",
    "jpMediumFont = \"gid://shopify/GenericFile/35438985281753\"\n",
    "fontName = \"GTWalsheimPro\"\n",
    "jpFontName = \"ZenKakuGothicNew\"\n",
    "mutation = \"\"\"\n",
    "  mutation checkoutBrandingUpsert($checkoutBrandingInput: CheckoutBrandingInput!, $checkoutProfileId: ID!) {\n",
    "    checkoutBrandingUpsert(checkoutBrandingInput: $checkoutBrandingInput, checkoutProfileId: $checkoutProfileId) {\n",
    "      checkoutBranding {\n",
    "        designSystem {\n",
    "          typography {\n",
    "            primary {\n",
    "              base {\n",
    "                sources,\n",
    "                weight\n",
    "              },\n",
    "              bold {\n",
    "                sources,\n",
    "                weight\n",
    "              },\n",
    "              name,\n",
    "              loadingStrategy\n",
    "            },\n",
    "            secondary {\n",
    "              base {\n",
    "                sources,\n",
    "                weight\n",
    "              },\n",
    "              bold {\n",
    "                sources,\n",
    "                weight\n",
    "              },\n",
    "              name,\n",
    "              loadingStrategy\n",
    "            }\n",
    "          }\n",
    "        },\n",
    "        customizations {\n",
    "          primaryButton {\n",
    "            typography {\n",
    "              font\n",
    "            }\n",
    "          },\n",
    "          headingLevel1 {\n",
    "            typography {\n",
    "              font\n",
    "            }\n",
    "          },\n",
    "          headingLevel2 {\n",
    "            typography {\n",
    "              font\n",
    "            }\n",
    "          },\n",
    "          headingLevel3 {\n",
    "            typography {\n",
    "              font\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "      userErrors {\n",
    "        code\n",
    "        field\n",
    "        message\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "\"\"\"\n",
    "variables = {\n",
    "  \"checkoutProfileId\": draftCheckoutId,\n",
    "  \"checkoutBrandingInput\": {\n",
    "    \"designSystem\": {\n",
    "      \"typography\": {\n",
    "        \"primary\": {\n",
    "          \"customFontGroup\": {\n",
    "            \"base\": {\n",
    "              \"genericFileId\": regularFont,\n",
    "              \"weight\": 500,\n",
    "            },\n",
    "            \"bold\": {\n",
    "              \"genericFileId\": regularFont,\n",
    "              \"weight\": 600,\n",
    "            },\n",
    "            \"loadingStrategy\": \"BLOCK\",\n",
    "          }\n",
    "        },\n",
    "        \"secondary\": {\n",
    "          \"customFontGroup\": {\n",
    "            \"base\": {\n",
    "              \"genericFileId\": jpRegularFont,\n",
    "              \"weight\": 500,\n",
    "            },\n",
    "            \"bold\": {\n",
    "              \"genericFileId\": jpRegularFont,\n",
    "              \"weight\": 600,\n",
    "            },\n",
    "            \"loadingStrategy\": \"SWAP\",\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    },\n",
    "    \"customizations\": {\n",
    "      \"primaryButton\": {\n",
    "        \"typography\": {\n",
    "          \"font\": \"PRIMARY\"\n",
    "        }\n",
    "      },\n",
    "      \"headingLevel1\": {\n",
    "        \"typography\": {\n",
    "          \"font\": \"PRIMARY\"\n",
    "        }\n",
    "      },\n",
    "      \"headingLevel2\": {\n",
    "        \"typography\": {\n",
    "          \"font\": \"PRIMARY\"\n",
    "        }\n",
    "      },\n",
    "      \"headingLevel3\": {\n",
    "        \"typography\": {\n",
    "          \"font\": \"PRIMARY\"\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Set color scheme 1\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      designSystem {\n",
    "        colors {\n",
    "          schemes {\n",
    "            scheme1 {\n",
    "              base {\n",
    "                accent\n",
    "              }\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "    \"checkoutProfileId\": draftCheckoutId,\n",
    "    \"input\": {\n",
    "        \"designSystem\": {\n",
    "            \"colors\": {\n",
    "                \"schemes\": {\n",
    "                    \"scheme1\": {\n",
    "                        \"base\": {\n",
    "                            \"accent\": \"#542aff\",\n",
    "                            \"background\": \"#fff\",\n",
    "                            \"border\": \"#dfdfdf\",\n",
    "                            \"decorative\": \"#000\",\n",
    "                            \"icon\": \"#000\",\n",
    "                            \"text\": \"#000\"\n",
    "                        },\n",
    "                        \"control\": {\n",
    "                            # vvv seemingly does nothing\n",
    "                            \"accent\": \"#542aff\",\n",
    "                            \"background\": \"#fff\",\n",
    "                            \"border\": \"#dfdfdf\",\n",
    "                            \"decorative\": \"#000\",\n",
    "                            \"icon\": \"#000\",\n",
    "                            \"text\": \"#000\",\n",
    "                            # vvv seemingly do nothing\n",
    "                            \"selected\": {\n",
    "                                \"accent\": \"#542aff\",\n",
    "                                \"border\": \"#542aff\"\n",
    "                            }\n",
    "                        },\n",
    "                        \"primaryButton\": {\n",
    "                            \"accent\": \"#000\",\n",
    "                            \"background\": \"#000\",\n",
    "                            \"text\": \"#fff\"\n",
    "                        },\n",
    "                        \"secondaryButton\": {\n",
    "                            \"accent\": \"#000\",\n",
    "                            \"background\": \"#f4f3f3\",\n",
    "                            \"border\": \"#000\",\n",
    "                            \"text\": \"#000\"\n",
    "                        }\n",
    "                    }\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Set color scheme 2\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      designSystem {\n",
    "        colors {\n",
    "          schemes {\n",
    "            scheme2 {\n",
    "              base {\n",
    "                accent\n",
    "              }\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "    \"checkoutProfileId\": draftCheckoutId,\n",
    "    \"input\": {\n",
    "        \"designSystem\": {\n",
    "            \"colors\": {\n",
    "                \"schemes\": {\n",
    "                    \"scheme2\": {\n",
    "                        \"base\": {\n",
    "                            \"accent\": \"#542aff\",\n",
    "                            \"background\": \"#f4f3f3\",\n",
    "                            \"border\": \"#dfdfdf\",\n",
    "                            \"decorative\": \"#000\",\n",
    "                            \"icon\": \"#000\",\n",
    "                            \"text\": \"#000\"\n",
    "                        },\n",
    "                        \"control\": {\n",
    "                            # vvv seemingly does nothing\n",
    "                            \"accent\": \"#542aff\",\n",
    "                            \"background\": \"#fff\",\n",
    "                            \"border\": \"#dfdfdf\",\n",
    "                            \"decorative\": \"#000\",\n",
    "                            \"icon\": \"#000\",\n",
    "                            \"text\": \"#000\",\n",
    "                            # vvv seemingly do nothing\n",
    "                            \"selected\": {\n",
    "                                \"accent\": \"#542aff\",\n",
    "                                \"border\": \"#542aff\"\n",
    "                            }\n",
    "                        },\n",
    "                        \"primaryButton\": {\n",
    "                            \"accent\": \"#000\",\n",
    "                            \"background\": \"#000\",\n",
    "                            \"text\": \"#fff\"\n",
    "                        },\n",
    "                        \"secondaryButton\": {\n",
    "                            \"accent\": \"#000\",\n",
    "                            \"background\": \"#f4f3f3\",\n",
    "                            \"border\": \"#000\",\n",
    "                            \"text\": \"#000\"\n",
    "                        }\n",
    "                    }\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - High level customizations\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      customizations {\n",
    "        favicon {\n",
    "          image {\n",
    "            url\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "    \"checkoutProfileId\": draftCheckoutId,\n",
    "    \"input\": {\n",
    "        \"customizations\": {\n",
    "            \"favicon\": {\n",
    "                \"mediaImageId\": \"gid://shopify/MediaImage/30216594653401\"\n",
    "            },\n",
    "            \"header\": {\n",
    "               \"divided\": False,\n",
    "                \"cartLink\": {\n",
    "                    \"contentType\": \"IMAGE\",\n",
    "                    \"image\": {\n",
    "                        \"mediaImageId\": \"gid://shopify/MediaImage/35447723950297\"\n",
    "                    }\n",
    "                }\n",
    "            },\n",
    "            \"footer\": {\n",
    "                \"content\": {\n",
    "                    \"visibility\": \"HIDDEN\"\n",
    "                }\n",
    "            },\n",
    "            \"main\": {\n",
    "                \"divider\": {\n",
    "                    \"visibility\": \"HIDDEN\"\n",
    "                },\n",
    "                \"section\": {\n",
    "                    \"border\": \"NONE\"\n",
    "                }\n",
    "            },\n",
    "            \"content\": {\n",
    "                \"divider\": {\n",
    "                    \"visibility\": \"HIDDEN\"\n",
    "                }\n",
    "            },\n",
    "            \"orderSummary\": {\n",
    "                \"divider\": {\n",
    "                    \"visibility\": \"HIDDEN\"\n",
    "                },\n",
    "                \"section\": {\n",
    "                    \"border\": \"NONE\"\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MUTATION - Element customizations\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($checkoutProfileId: ID!, $input: CheckoutBrandingInput!) {\n",
    "  checkoutBrandingUpsert(checkoutProfileId: $checkoutProfileId, checkoutBrandingInput: $input) {\n",
    "    checkoutBranding {\n",
    "      customizations {\n",
    "        headingLevel1 {\n",
    "          typography {\n",
    "            size\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "    \"checkoutProfileId\": draftCheckoutId,\n",
    "    \"input\": {\n",
    "        \"customizations\": {\n",
    "            \"headingLevel1\": {\n",
    "                \"typography\": {\n",
    "                    \"size\": \"EXTRA_EXTRA_LARGE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"headingLevel2\": {\n",
    "                \"typography\": {\n",
    "                    \"size\": \"EXTRA_LARGE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"headingLevel3\": {\n",
    "                \"typography\": {\n",
    "                    \"size\": \"LARGE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"primaryButton\": {\n",
    "                \"cornerRadius\": \"SMALL\",\n",
    "                \"inlinePadding\": \"EXTRA_LOOSE\",\n",
    "                \"blockPadding\": \"TIGHT\",\n",
    "                \"border\": \"FULL\",\n",
    "                \"typography\": {\n",
    "                    \"size\": \"BASE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"secondaryButton\": {\n",
    "                \"cornerRadius\": \"SMALL\",\n",
    "                \"inlinePadding\": \"EXTRA_LOOSE\",\n",
    "                \"blockPadding\": \"TIGHT\",\n",
    "                \"border\": \"FULL\",\n",
    "                \"background\": \"NONE\",\n",
    "                \"typography\": {\n",
    "                    \"size\": \"BASE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"select\": {\n",
    "                \"typography\": {\n",
    "                    \"size\": \"BASE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"textField\": {\n",
    "                \"typography\": {\n",
    "                    \"size\": \"BASE\",\n",
    "                    \"weight\": \"BASE\"\n",
    "                }\n",
    "            },\n",
    "            \"merchandiseThumbnail\": {\n",
    "                \"border\": \"NONE\"\n",
    "            },\n",
    "            \"expressCheckout\": {\"button\": {\"cornerRadius\": \"SMALL\"}}\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# QUERY - Get total Beyond orders\n",
    "query = \"\"\"\n",
    "query howMany {\n",
    "  ordersCount(query: \"sku:BS1B00\") {\n",
    "    count\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "# Delete any list entry under the \"edges\" key which does not contain: a \"url\" key where the value contains filter\n",
    "# filter = 'android-chrome'\n",
    "# response['data']['files']['edges'] = [edge for edge in response['data']['files']['edges'] if 'url' in edge['node'] and filter in edge['node']['url']]\n",
    "\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# QUERY - Get Beyond orders, surfacing only custom attributes\n",
    "query = \"\"\"\n",
    "query listEm {\n",
    "  orders(first:250, query: \"sku:BS1B00\") {\n",
    "    nodes {\n",
    "      lineItems(first: 10) {\n",
    "        nodes {\n",
    "          customAttributes {\n",
    "            key\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "# Delete any list entry under the \"edges\" key which does not contain: a \"url\" key where the value contains filter\n",
    "# filter = 'android-chrome'\n",
    "# response['data']['files']['edges'] = [edge for edge in response['data']['files']['edges'] if 'url' in edge['node'] and filter in edge['node']['url']]\n",
    "\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# BULK QUERY - Same as above but bulk operation\n",
    "query = \"\"\"\n",
    "query listEm {\n",
    "  orders(query: \"sku:BS1B00\", sortKey: CREATED_AT, last: 500) {\n",
    "    edges {\n",
    "      node {\n",
    "        id\n",
    "        createdAt\n",
    "        lineItems {\n",
    "          edges {\n",
    "            node {\n",
    "              id\n",
    "              customAttributes {\n",
    "                key\n",
    "              }\n",
    "            }\n",
    "          }\n",
    "        }\n",
    "      }\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "mutation = \"\"\"\n",
    "mutation MyMutation($query: String!) {\n",
    "  bulkOperationRunQuery(query: $query) {\n",
    "    bulkOperation {\n",
    "      id\n",
    "      status\n",
    "    }\n",
    "    userErrors {\n",
    "      field\n",
    "      message\n",
    "    }\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "variables = {\n",
    "  \"query\": query\n",
    "}\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, mutation, variables)\n",
    "## Save the bulk operation ID\n",
    "bulkOperationId = response['data']['bulkOperationRunQuery']['bulkOperation']['id']\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# BULK QUERY PART 2 - Checkup\n",
    "query = \"\"\"\n",
    "query howMany {\n",
    "  currentBulkOperation {\n",
    "    id\n",
    "    status\n",
    "    errorCode\n",
    "    createdAt\n",
    "    completedAt\n",
    "    objectCount\n",
    "    fileSize\n",
    "    url\n",
    "    partialDataUrl\n",
    "  }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "response = send_graphql_request(endpointUrl, accessToken, query)\n",
    "# Delete any list entry under the \"edges\" key which does not contain: a \"url\" key where the value contains filter\n",
    "# filter = 'android-chrome'\n",
    "# response['data']['files']['edges'] = [edge for edge in response['data']['files']['edges'] if 'url' in edge['node'] and filter in edge['node']['url']]\n",
    "\n",
    "print(json.dumps(response, indent=2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
