Skip to content

Instantly share code, notes, and snippets.

@albingeorge
Created March 3, 2023 07:33
Show Gist options
  • Save albingeorge/6e7058f049da8c1a6ab92713335387ec to your computer and use it in GitHub Desktop.
Save albingeorge/6e7058f049da8c1a6ab92713335387ec to your computer and use it in GitHub Desktop.

Assumptions

  • Using REST API calls

Approach 1

Request

{
  "data": {
    "name": "John",
    "age": 26,
    ...
  }
}

Response

Success

{
  "status": "sucess"
}

Failure

{
  "status": "error",
  "error": {
    "message": "Customer already exists"
  }
}

Pros

  • Faster from both client side and server side
  • Easier implementation and moving parts in code
  • Ideal if client side dont need further data from backend to show the grid

Cons

  • If there are additional data in the response you need in the grid(like "id" field which would be generated at the db side), it could not be shown in the front-end

Approach 2

Request

{
  "data": {
    "name": "John",
    "age": 26,
    ...
  }
}

Response

Success

{
  "status": "success",
  "data": {
    "id": 123
    "name": "John",
    "age": 26,
  }
}

Failure

{
  "status": "error",
  "error": {
    "message": "Customer already exists"
  }
}

Pros

  • Faster from both client side and server side
  • Easier implementation and moving parts in code
  • Fetch more data from backend as required(ex, "id" field)

Cons

  • As the data(columns in this case) grows, the response grows as well. Not really that much big of a deal to be honest.

Approach 3(not preferred)

Create request

{
  "data": {
    "name": "John",
    "age": 26,
    ...
  }
}

Response

Success

{
  "status": "sucess",
  "data": {
    "id": 123
  }
}

Failure

{
  "status": "error",
  "error": {
    "message": "Customer already exists"
  }
}

Follow up request if first request is success

Fetch request

{
  "data": {
    "id": 123
  }
}

Response

Success

{
  "status": "success",
  "data": {
    "id": 123
    "name": "John",
    "age": 26,
  }
}

Pros

  • None I can think of

Cons

  • Both server side and client side need to handle multiple requests for a single operation, meaning more moving parts, which translates to more errors and edge cases(For example, what do you show on the client side, if the first call is success, but second call fails?).
  • Laggy user experience(based on latency of these calls)
  • Higher implementation cost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment