Recursive GraphQL query

Recursive GraphQL query


0

I have the following GraphQL query:

query MyQuery {
  product(uid: "1234") {
    productCategoryConnection: product_categoryConnection {
      edges {
        node {
          ... on ProductCategory {
            title
            identifier
            parentCategoryConnection: parent_categoryConnection {
              edges {
                node {
                  ... on ProductCategory {
                    title
                    identifier
                    parentCategoryConnection: parent_categoryConnection {
                      edges {
                        node {
                          ... on ProductCategory {
                            title
                            identifier
                            parentCategoryConnection: parent_categoryConnection {
                              edges {
                                node {
                                  ... on ProductCategory {
                                    title
                                    identifier
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

The issue is that I don’t know how many levels deep I can go, so, for example, with the above query, I could get this:

{
  "data": {
    "product": {
      "productCategoryConnection": {
        "edges": [
          {
            "node": {
              "title": "Sub-category 1",
              "identifier": "sub-category-1",
              "parentCategoryConnection": {
                "edges": [
                  {
                    "node": {
                      "title": "Category 1",
                      "identifier": "Category 1",
                      "parentCategoryConnection": {
                        "edges": []
                      }
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}

And is some other cases I could get more nested subcategories.

I want to use a recursive version of this that goes as deep as necessary until an empty array is found for edges.

I tried using fragments in this way:

query MyQuery {
  product(uid: "blt742f17e9dfed750e", locale: "en-us") {
    productCategoryConnection: product_categoryConnection {
      edges {
        node {
          ...ProductCategoryFields
        }
      }
    }
  }
}

fragment ProductCategoryFields on ProductCategory {
  title
  identifier
  parentCategoryConnection: parent_categoryConnection {
    edges {
      node {
        ...NestedProductCategoryFields
      }
    }
  }
}

fragment NestedProductCategoryFields on ProductCategory {
  title
  identifier
  parentCategoryConnection: parent_categoryConnection {
    edges {
      node {
        ...ProductCategoryFields
      }
    }
  }
}

But got:

{
  "errors": [
    {
      "message": "Maximum call stack size exceeded",
      "extensions": {}
    }
  ]
}

Is it possible to have a recursive query that will get as many levels deep until finding an empty array? or do I have to keep manually nesting the parentCategoryConnection?


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *