How can I dynamically add Arguments to an Operation create by sqqlc-codegen

66 Views Asked by At

I am working on a python-tool that interacts with the Shopify GraphQL API. I started using the sqglc-libraby to have the convenience of handling everything with python objects instead of the long-winded dict-like accessing of Grapqhl queries and responses. Having followed the documentation on how to create types and operations (https://sgqlc.readthedocs.io/en/latest/sgqlc.codegen.html) i have created my own operations pytho module. However, im stuck on something that i thought the code-first approach would help me solve:

The below query is stored in my_operations.gql and from it the corresponding code is created by the codegen tool.

query BulkAllStock{
  productVariants{
    edges {
    cursor
      node {
        id
        barcode
        sku
        inventoryItem {
          inventoryLevel{
            id
            available
            quantities(names: ["available", "on_hand", "committed"]) {
                name
                quantity
            }
          }
        }
      }
    }
  }
}

def query_bulk_all_stock():
    _op = sgqlc.operation.Operation(_schema_root.query_type, name='BulkAllStock')
    _op_product_variants = _op.product_variants()
    _op_product_variants_edges = _op_product_variants.edges()
    _op_product_variants_edges.cursor()
    _op_product_variants_edges_node = _op_product_variants_edges.node()
    _op_product_variants_edges_node.id()
    _op_product_variants_edges_node.barcode()
    _op_product_variants_edges_node.sku()
    _op_product_variants_edges_node_inventory_item = _op_product_variants_edges_node.inventory_item()
    _op_product_variants_edges_node_inventory_item_inventory_level = _op_product_variants_edges_node_inventory_item.inventory_level()
    _op_product_variants_edges_node_inventory_item_inventory_level.id()
    _op_product_variants_edges_node_inventory_item_inventory_level.available()
    _op_product_variants_edges_node_inventory_item_inventory_level_quantities = _op_product_variants_edges_node_inventory_item_inventory_level.quantities(names=('available', 'on_hand', 'committed'))
    _op_product_variants_edges_node_inventory_item_inventory_level_quantities.name()
    _op_product_variants_edges_node_inventory_item_inventory_level_quantities.quantity()
    return _op

I would now like to pass further arguments to this query to further narrow down the response data by adding a inventorlevelID to the inventoryLevel field like so:

...
inventoryLevel(locationId: $location_id){
...

Reading the documentation I could not figure out how to do this with the Operation Object that i create from the generated code. It appears to me that i would need to inlcude another query with that arugment included, but that would mean a lot of repeated code.

The background on this is the way that bulk queries in shopify work. These are another set of queries that take another query as a variable in form of a muti line string, this query cant include variables however as explained here: https://stackoverflow.com/questions/69354063/use-variables-in-shopify-bulk-operations-graphql-mutation

So my idea was to first create the operation that is meant to be passed into the bulk-query including the variables i want to have, and then pass the pure grapqhl-string into a bulk query operation that i can simply execute with the sqglc endpoint.

So how could I create an sgqlc operation object where i can add an argument to an arbitrary field if possible at all?

I tried adding variables to the operation after instantiating the object, but that does not seem to work.

0

There are 0 best solutions below