mglaman / commerce_cart_api

Original sandbox WIP for the module now on Drupal.org. Do not fork. Patches accepted on Drupal.org
https://www.drupal.org/project/commerce_cart_api
9 stars 1 forks source link

Consider making the order payload more self describing #9

Closed mglaman closed 5 years ago

mglaman commented 6 years ago

In talks with the API-First and JSON API folks, one consideration was wrapping the order data in an "envelope". Instead of it being a root property in the JSON, we could nest it. This would allow providing a "links" attribute.

So, as an example, we'd have

[
  {
    "id": "2",
    "data": {
      "order_id": 2,
      "uuid": "be9cb309-2c77-4f85-9444-013c91ab9318",
      "order_number": null,
      "store_id": 1,
      "total_price": {
        "number": "403.97",
        "currency_code": "USD"
      },
      "order_items": [
        {
          "order_item_id": 9,
          "uuid": "dcaa6901-f799-4505-af4c-9d1db9a85bbb",
          "type": "physical_product_variation",
          "purchased_entity": 4,
          "title": "Drupal Commerce Hoodie - Green, Small",
          "quantity": "2.00",
          "unit_price": {
            "number": "52.00",
            "currency_code": "USD"
          },
          "total_price": {
            "number": "104.00",
            "currency_code": "USD"
          }
        },
        {
          "order_item_id": 21,
          "uuid": "3bb80d13-ec7d-4e69-af33-a2f014c5e284",
          "type": "physical_product_variation",
          "purchased_entity": 15,
          "title": "Pronto600 Instant Camera",
          "quantity": "3.00",
          "unit_price": {
            "number": "99.99",
            "currency_code": "USD"
          },
          "total_price": {
            "number": "299.97",
            "currency_code": "USD"
          }
        }
      ]
    },
    "links": {
      "cart": "/cart",
      "checkout": "/checkout/2"
    }
  }
]
gabesullice commented 6 years ago

@mglaman, I'm curious why your example doesn't fully implement the JSON API document structure but only takes influence from it. For example, your example puts the id outside the data key and it doesn't have attribute or relationships keys.

mglaman commented 6 years ago

@mglaman, I'm curious why your example doesn't fully implement the JSON API document structure but only takes influence from it.

Because I quick wrote this :) And I don't want to rewrite the internal normalizers JSON API module does not expose.

mglaman commented 6 years ago

Also, I don't think an order should define relationships. Order items should be represented as if embedded entities.

gabesullice commented 6 years ago

Because I quick wrote this :)

That's what I figured but wanted to check. I realize now that I probably sounded harsh, sorry!

Also, I don't think an order should define relationships. Order items should be represented as if embedded entities.

Let's discuss this at DrupalCon, there are some really nice properties that come out of it that I bet you will appreciate :)

mglaman commented 6 years ago

@gabesullice I didn't take it harsh. I also don't know the JSON API spec schema very well (I know there's data.attributes and some things :D that's about it. I wanted to POC it and have this topic open so I could purge it from my Todoist finally.

It'll be great to chat at DrupalCon so we have something strong and stable.