# Migration guide: InventoryItem data field

Audience: Developer

Migration guide for the breaking change from inventoryJSONData to the unified data field

## Overview

This guide covers a breaking change in the Pyck GraphQL API where the `inventoryJSONData` field has been removed from the `InventoryItem` type. This field has been replaced with the unified `data` field that is now available across all entity types through the shared DataMixin.

## What changed

**Before**: InventoryItem had a separate `inventoryJSONData` field for storing custom JSON data.

```graphql
type InventoryItem {
  id: ID!
  sku: String!
  inventoryJSONData: Map  # Removed
  # ... other fields
}
```

**After**: InventoryItem uses the unified `data` field (type `Map`).

```graphql
type InventoryItem implements NodeInventory {
  id: ID!
  sku: String!
  data: Map
  # ... other fields
}
```

## Breaking changes

1. **Field Removed**: The `inventoryJSONData` field no longer exists on `InventoryItem`
2. **Database Migration**: The `inventory_json_data` column has been removed from the items table
3. **Data Preserved**: All data previously stored in `inventoryJSONData` has been migrated to the `data` field

## Migration steps

### Step 1: Update GraphQL queries

Replace all occurrences of `inventoryJSONData` with `data` in your queries.

**Before:**

```graphql
query GetInventoryItems {
  inventoryItems(first: 10) {
    edges {
      node {
        id
        sku
        inventoryJSONData
      }
    }
  }
}
```

**After:**

```graphql
query GetInventoryItems {
  inventoryItems(first: 10) {
    edges {
      node {
        id
        sku
        data
      }
    }
  }
}
```

### Step 2: Update mutations

Update create and update mutations to use `data` instead of `inventoryJSONData`.

**Before:**

```graphql
mutation CreateItem($input: CreateInventoryItemInput!) {
  createInventoryItem(input: $input) {
    id
    sku
    inventoryJSONData
  }
}

# Variables
{
  "input": {
    "sku": "ITEM-001",
    "inventoryJSONData": {
      "weight": 1.5,
      "color": "blue"
    }
  }
}
```

**After:**

```graphql
mutation CreateItem($input: CreateInventoryItemInput!) {
  createInventoryItem(input: $input) {
    inventoryItem {
      id
      sku
      data
    }
  }
}

# Variables
{
  "input": {
    "sku": "ITEM-001",
    "data": {
      "weight": 1.5,
      "color": "blue"
    }
  }
}
```

### Step 3: Update filter queries

Update where clauses that filter on JSON data fields.

**Before:**

```graphql
query GetItemsByWeight {
  inventoryItems(
    where: {
      inventoryJSONDataHasKey: "weight"
    }
  ) {
    edges {
      node {
        id
        sku
        inventoryJSONData
      }
    }
  }
}
```

**After:**

```graphql
query GetItemsByWeight {
  inventoryItems(
    where: {
      DataHasKey: "weight"
    }
  ) {
    edges {
      node {
        id
        sku
        data
      }
    }
  }
}
```

### Step 4: Update client code

Update your client code to use the new field name.

**Before (JavaScript):**

```javascript
const items = response.data.inventoryItems.edges.map(edge => ({
  id: edge.node.id,
  sku: edge.node.sku,
  customData: edge.node.inventoryJSONData
}));
```

**After (JavaScript):**

```javascript
const items = response.data.inventoryItems.edges.map(edge => ({
  id: edge.node.id,
  sku: edge.node.sku,
  customData: edge.node.data
}));
```

## Complete migration examples

### Example 1: Query with custom data

**Before:**

```graphql
query GetItemsWithMetadata {
  inventoryItems(
    first: 20,
    where: {
      inventoryJSONDataHasKey: "metadata"
    }
  ) {
    edges {
      node {
        id
        sku
        inventoryJSONData
        createdAt
      }
    }
    totalCount
  }
}
```

**After:**

```graphql
query GetItemsWithMetadata {
  inventoryItems(
    first: 20,
    where: {
      DataHasKey: "metadata"
    }
  ) {
    edges {
      node {
        id
        sku
        data
        createdAt
      }
    }
    totalCount
  }
}
```

### Example 2: Create item with custom data

**Before:**

```graphql
mutation CreateProductItem {
  createInventoryItem(input: {
    sku: "PROD-12345",
    inventoryJSONData: {
      category: "electronics",
      manufacturer: "Acme Corp",
      warranty: "2 years"
    }
  }) {
    id
    sku
    inventoryJSONData
  }
}
```

**After:**

```graphql
mutation CreateProductItem {
  createInventoryItem(input: {
    sku: "PROD-12345",
    data: {
      category: "electronics",
      manufacturer: "Acme Corp",
      warranty: "2 years"
    }
  }) {
    inventoryItem {
      id
      sku
      data
    }
  }
}
```

### Example 3: Update item custom data

**Before:**

```graphql
mutation UpdateItemData($id: ID!, $newData: Map!) {
  updateInventoryItem(id: $id, input: {
    inventoryJSONData: $newData
  }) {
    id
    sku
    inventoryJSONData
  }
}
```

**After:**

```graphql
mutation UpdateItemData($id: ID!, $newData: Map!) {
  updateInventoryItem(id: $id, input: {
    data: $newData
  }) {
    inventoryItem {
      id
      sku
      data
    }
  }
}
```

## Effect on entity types

Entity types now use the same `data` field of type `Map`. Shared mixins across entity types reduce duplication in the schema, and the filter names (`DataHasKey`, `DataContains`, etc.) are the same across all entity types.

## Filter input changes

The following filter fields have been renamed for consistency:

| Old Field Name | New Field Name |
|----------------|----------------|
| `inventoryJSONData` | `Data` |
| `inventoryJSONDataHasKey` | `DataHasKey` |
| `inventoryJSONDataIn` | `DataIn` |
| `inventoryJSONDataContains` | `DataContains` |

## Troubleshooting

### Error: Cannot query field `inventoryJSONData` on type `InventoryItem`

This error indicates you are still using the old field name. Update your query to use `data`:

```graphql
# Wrong
inventoryItems {
  edges {
    node {
      inventoryJSONData
    }
  }
}

# Correct
inventoryItems {
  edges {
    node {
      data
    }
  }
}
```

### Error: Field `inventoryJSONDataHasKey` is not defined

Update your where clause to use the new filter field names:

```graphql
# Wrong
where: {
  inventoryJSONDataHasKey: "category"
}

# Correct
where: {
  DataHasKey: "category"
}
```

## Related documentation

- [InventoryItem Type](https://docs.pyck.cloud/api/types/inventoryitem.md)
- [Advanced JSON Field Queries](https://docs.pyck.cloud/developers/advanced/json-field-queries.md)
- [Map Scalar](https://docs.pyck.cloud/api/scalars/map.md)
