Which is better approach? one request or two requests

169 Views Asked by At

I have a table that returns JSON like this:

{ 
  "status": 200,
  "body": [
      {
        "category": "Sports",
        "items": [
             {
               "id": 1,
               "name": "Football"
             },
             {
               "id": 1,
               "name": "Volly"
             },
             {
               "id": 2,
               "name": "BasketBall"
             }
         ]
      },
      {
        "category": "Fruits",
        "items": [
             {
               "id": 1,
               "name": "Apple"
             },
             {
               "id": 1,
               "name": "Bananna"
             },
             {
               "id": 2,
               "name": "Orange"
             }
         ]
      },
      {
        "category": "Electronics",
        "items": [
             {
               "id": 1,
               "name": "Phone"
             },
             {
               "id": 1,
               "name": "Tablets"
             },
             {
               "id": 2,
               "name": "Computers"
             }
         ]
      }
  ],
}

The URL for this JSON will be like http://localhost:8080/categories

On my react native app I want to return all the items of each category based on the category name.

My question is which is the best approach to make this:

1: To communicate with this URL http://localhost:8080/categories and filter which category do I want on react native?

OR

2: To create two different URL for each category for example:

http://localhost:8080/fruits

SELECT ID, ITEM FROM CATEGORY WHERE CATEGORY.ID = 'fruits'

http://localhost:8080/sports

SELECT ID, ITEM FROM CATEGORY WHERE CATEGORY.ID = 'SPORTS'

The number 2 approach will return already a filtered response for me.

1

There are 1 best solutions below

0
On BEST ANSWER

As a rule of thumb, network requests are a bit expensive and adds up when scaling. Better to issue 1 request and then select the data you want appropriately client side. Another option to use would be GraphQL which was designed explicitly for your purpose of querying for data with more granular control than you'd get from a typical REST api.

https://graphql.org/