Skip to main content

Step-by-Step Data Interaction

Understanding Data Flow

This section provides a detailed walkthrough of the data flow within the system when a user performs common actions. We'll use the example of adding a new node (item or category) to illustrate the process.

Data Interaction Steps

1. User Initiates Action

  • Action: The user decides to add a new node.
  • Interaction: Clicks on the plus (+) icon and fills out the form with the new node's details.
  • Data Captured:
    • Node Title: The name of the new item or category.
    • Current Location ID: The unique identifier of the current node where the new node will be added.

2. Front End Processes Input

  • Validation: Checks that the node title is not empty and meets any client-side validation rules.
  • Preparation: Constructs a GraphQL mutation with the input data.

3. Front End Sends Request to API

GraphQL Mutation Example:

mutation CreateNode($title: String!, $parentId: ID!) {
  createNode(title: $title, parentId: $parentId) {
    id
    title
    parentId
  }
}

 Variables:

{
  "title": "New Node",
  "parentId": "1234"
}

Communication: Sends the mutation to the API endpoint (http://127.0.0.1/api) via an HTTP POST request.

4. API Receives and Validates Request

  • Authentication: Verifies the user's session token or cookie to ensure they are authenticated.
  • Authorization: Checks if the user has permission to add nodes.
  • Data Validation: Ensures the title and parentId are valid and that the parent node exists.

5. API Interacts with Database

Insert Node:

INSERT INTO nodes (title)
VALUES ('New Node')
RETURNING id;

Establish Relationship:

INSERT INTO node_relations (parent_id, child_id)
VALUES ('1234', 'newly_generated_id');

Database Response: Returns the new node's unique identifier (id).

6. API Sends Response to Front End

Success Response Example:

{
  "data": {
    "createNode": {
      "id": "5678",
      "title": "New Node",
      "parentId": "1234"
    }
  }
}

Error Handling: If an error occurs, the API sends an error response with details.

7. Front End Updates UI

  • UI Update:
    • Adds the new node under the current node in the navigation tree.
    • Provides immediate visual feedback to the user.
  • State Management: Updates the application state to include the new node.

8. Real-Time Updates to Other Users

  • GraphQL Subscriptions:
    • Other connected clients subscribed to node updates receive a notification.
    • Their UIs automatically reflect the addition of the new node without requiring a page refresh.

Data Flow Diagram


sequenceDiagram
    participant User
    participant FrontEnd
    participant API
    participant Database

    User->>FrontEnd: Clicks "Add Node" and submits form
    FrontEnd->>API: Sends GraphQL mutation (createNode)
    API->>API: Authenticates and validates data
    API->>Database: Inserts new node and relationship
    Database-->>API: Returns new node ID
    API-->>FrontEnd: Sends success response
    FrontEnd-->>User: Updates UI with new node
    API-->>FrontEnd: Sends subscription update to other clients
    FrontEnd-->>User: Other clients' UIs update in real-time

image.png

Key Considerations

  • Authentication and Authorization:

    • Ensures only permitted users can modify the inventory.
    • Uses secure tokens or session cookies for verification.
  • Data Validation:

    • Prevents invalid or malicious data from entering the system.
    • Both client-side and server-side validations are implemented.
  • Error Handling:

    • Provides meaningful error messages to the user.
    • Logs errors for administrative review without exposing sensitive information.
  • Performance Optimization:

    • Uses efficient database queries.
    • Minimizes network requests by batching where possible.
  • Scalability:

    • Designed to handle multiple concurrent users.
    • Real-time updates ensure data consistency across all clients.