Clean Up GraphQL Response and Convert to JSON without edges and nodes

baberuth22
Excursionist
16 0 3

I am calling the Shopify admin api.

When I use the REST API, I get beautiful JSON objects in my responses.

[{
 {
    "id": 7036594978985,
    "title": "7 Shakra Bracelet",
    "body_html": "<p>7 chakra bracelet, in blue or black.</p>",
    "vendor": "Company 123",
    "product_type": "Bracelet",
    "created_at": "2021-06-17T17:45:05-04:00",
    "handle": "chain-bracelet",
    "updated_at": "2021-06-23T20:08:21-04:00",
    "published_at": "2021-06-17T17:45:04-04:00",
    "template_suffix": null,
    "published_scope": "web",
    "tags": "Beads, Bracelet",
    "admin_graphql_api_id": "gid://shopify/Product/7036594978985",
    "variants": [
      {
        "id": 40671266963625,
        "product_id": 7036594978985,
        "title": "Blue",
        "price": "42.99",
        "sku": null,
        "position": 1,
        "inventory_policy": "deny",
        "compare_at_price": "44.99",
        "fulfillment_service": "manual",
        "inventory_management": null,
        "option1": "Blue",
    
      }
  },
  {
    id: 7036594978986
...
]

However, if I use GraphQL I end up with a bunch of edges and nodes that don't play well with my front end JS.

{
  "products": {
    "edges": [
      {
        "node": {
          "description": "Sleek and black",
          "featuredImage": {
            "id": "gid://shopify/ProductImage/15464783282345",
            "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
          },
          "handle": "skirt",
          "id": "gid://shopify/Product/4773734482089",
          "images": {
            "edges": [
              {
                "node": {
                  "id": "gid://shopify/ProductImage/15464783282345",
                  "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
                }
              },
              {
                "node": {
                  "id": "gid://shopify/ProductImage/26072838406313",
                  "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb7.jpg?v=1612636091"
                }
              },
              {
                "node": {
                  "id": "gid://shopify/ProductImage/26072838373545",
                  "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb6.jpg?v=1612636091"
                }
              }
            ]
          },

I'd like to flatten the array a little bit by replacing all edges and nodes with the friendly minimal JSON structure.

From this:

{
  "products": {
    "edges": [
      {
        "node": {
          "description": "Sleek and black",
          "featuredImage": {
            "id": "gid://shopify/ProductImage/15464783282345",
            "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
          },

To this:

{
  "products": [
      {
          "description": "Sleek and black",
          "featuredImage": {
            "id": "gid://shopify/ProductImage/15464783282345",
            "originalSrc": "https://cdn.shopify.com/s/files/1/0268/1005/6873/products/bb20.jpg?v=1602116082"
      },

I'd like to be able to call a recursive PHP function to replace all nodes and edges nested in the array.

I tried this, but I think it causes iteration issues.

public function parse_and_remove (&$arr) {
    
    foreach ($arr as $key => $val) {
        if (($key == 'edges' || $key == 'node') && (is_array($val) || is_object($val))) {
            $arr = $val;
            $val = $this->parse_and_remove($val);
        } else if (is_array($val) || is_object($val)) {
            $val = $this->parse_and_remove($val);
        } else {

        }
    }
    return $arr;
}

PART II

How is everyone okay with this kind of response in GraphQL? It's a pain to work with on the front end where API responses are often consumed. Don't give me the array of edges and the node then finally the object. I just want that array of objects

0 Likes