|
| 1 | +# Saleor GraphQL Loader |
| 2 | + |
| 3 | +`saleor-gql-loader` is small python package that allows you to quickly and easily |
| 4 | +create entities such as categories, warehouses, products on your saleor website |
| 5 | +using the graphQL endpoint exposed by saleor. |
| 6 | + |
| 7 | +As of now, `saleor-gql-loader` allows to create the following entities: |
| 8 | + |
| 9 | +- [x] warehouse |
| 10 | +- [x] shipping_zone |
| 11 | +- [x] attribute |
| 12 | +- [x] attribute_value |
| 13 | +- [x] product_type |
| 14 | +- [x] category |
| 15 | +- [x] product |
| 16 | +- [x] product_variant |
| 17 | + |
| 18 | +PR for supporting more graphQL mutations and/or queries are more than welcome. |
| 19 | + |
| 20 | +In it's current state, the project is in very early alpha, might be unstable |
| 21 | +and no tests are provided. |
| 22 | + |
| 23 | +_disclaimer: This project is not connected nor it has been endorsed by saleor |
| 24 | +team/community._ |
| 25 | + |
| 26 | +## installation |
| 27 | + |
| 28 | +using Pypi: |
| 29 | + |
| 30 | +```bash |
| 31 | +pip install saleor-gql-loader |
| 32 | +``` |
| 33 | + |
| 34 | +Or cloning the repo: |
| 35 | + |
| 36 | +```bash |
| 37 | +git clone https://github.com/grll/saleor-gql-loader.git |
| 38 | +``` |
| 39 | + |
| 40 | +## usage |
| 41 | + |
| 42 | +### prerequisities |
| 43 | + |
| 44 | +The first requirement is to have a running saleor installation with the latest |
| 45 | +version installed (2.9). |
| 46 | + |
| 47 | +Before being able to use the package to create entities you need to create a |
| 48 | +saleor app with the necessary permissions to create the entities you need. |
| 49 | + |
| 50 | +One way of doing that is to use the specific django cli custom command `create_app`: |
| 51 | + |
| 52 | +```bash |
| 53 | +python manage.py create_app etl --permission account.manage_users \ |
| 54 | + --permission account.manage_staff \ |
| 55 | + --permission app.manage_apps \ |
| 56 | + --permission app.manage_apps \ |
| 57 | + --permission discount.manage_discounts \ |
| 58 | + --permission plugins.manage_plugins \ |
| 59 | + --permission giftcard.manage_gift_card \ |
| 60 | + --permission menu.manage_menus \ |
| 61 | + --permission order.manage_orders \ |
| 62 | + --permission page.manage_pages \ |
| 63 | + --permission product.manage_products \ |
| 64 | + --permission shipping.manage_shipping \ |
| 65 | + --permission site.manage_settings \ |
| 66 | + --permission site.manage_translations \ |
| 67 | + --permission webhook.manage_webhooks \ |
| 68 | + --permission checkout.manage_checkouts |
| 69 | +``` |
| 70 | + |
| 71 | +> This command will return a token. Keep it somewhere as it will be need to use the |
| 72 | +> loader. |
| 73 | +
|
| 74 | +### loading data |
| 75 | + |
| 76 | +`saleor-gql-loader` package exposes a single class that needs to be initialized |
| 77 | +with the authentication token generated in the section above. Then for each entity |
| 78 | +that you want to create there is a corresponding method on the class. |
| 79 | + |
| 80 | +```python |
| 81 | +from saleor_gql_loader import ETLDataLoader |
| 82 | + |
| 83 | +# initialize the data_loader (optionally provide an endpoint url as second parameter) |
| 84 | +data_loader = ETLDataLoader("LcLNVgUt8mu8yKJ0Wrh3nADnTT21uv") |
| 85 | + |
| 86 | +# create a warehouse |
| 87 | +warehouse_id = etl_data_loader.create_warehouse() |
| 88 | +``` |
| 89 | + |
| 90 | +by default the `ETLDataLoader` will create a warehouse with sensible default values |
| 91 | +in order to not make the query fail. You can override any parameter from the graphQL |
| 92 | +type corresponding to the input of the underlying mutation. |
| 93 | + |
| 94 | +For example, to set the name and email of my warehouse: |
| 95 | + |
| 96 | +```python |
| 97 | +# create a warehouse with specified name and email |
| 98 | +warehouse_id = etl_data_loader.create_warehouse(name="my warehouse name", email="email@example.com") |
| 99 | +``` |
| 100 | + |
| 101 | +When a input field is mandatory it will need to be passed as first argument for example |
| 102 | +you can't create an attribute_value without specifying on which attribute id: |
| 103 | + |
| 104 | +```python |
| 105 | +# create a year attribute |
| 106 | +year_attribute_id = etl_data_loader.create_attribute(name="year") |
| 107 | + |
| 108 | +# add the following year value to the year attribute |
| 109 | +possible_year_values = [2020, 2019, 2018, 2017] |
| 110 | +for year in possible_year_values: |
| 111 | + etl_data_loader.create_attribute_value(year_attribute_id, name=year) |
| 112 | +``` |
| 113 | + |
| 114 | +That's all there is to it. I added a jupyter notebook as an example with more usage [here](https://github.com/grll/saleor-gql-loader/blob/master/saleor_gql_loader/example.ipynb) where you will find a full |
| 115 | +example that I used to populate my data. |
| 116 | + |
| 117 | +For more details, I recommend you to check out the code, I tried to document it as much |
| 118 | +as possible (it's only one module with one class). |
| 119 | + |
| 120 | +once again for any new features additions comments, feel free to open an issue or |
| 121 | +even better make a Pull Request. |
0 commit comments