Schema for ecommerce with multiple variants/attributes

What is the most appropriate way to design an eCommerce that has different variations/attributes depending on the product type.

For example lets consider computeruniverse, gpu and macbook should have different fitters:
GPU listing https://www.computeruniverse.net/en/c/hardware-components/amd-graphics-cards
MacBook listing https://www.computeruniverse.net/en/c/laptops-tablet-pcs-pcs/apple-macbook

Could anyone give me a basic graphql schema structure for such scenario?

Thanks a lot.

1 Like

Hi @panakour

  • One approach is to use union
type Laptop {
  AttributeID: ID
  CPU: String
  RAM: String
  price: Int
}

type GPU {
  AttributeID: ID
  Chip: String
  memory: Int
  clock: Int
  price: Int
}
union ProductAttributesUnion = Laptop | GPU

type Product {
  ProductID:ID
  attributes: ProductAttributesUnion
  created_at: DateTime
}

Now for inserting a laptop product you can use this:

mutation addProducts {
  addProduct(input:{
    price:1000,
    created_at:"2021",
    attributes:{
      laptopRef:{
        CPU:"i7"
        RAM: "16GB"
      }
    }
  }) {
    numUids
  }
}

For getting the results you can make this query:

query getProducts {
  queryProduct {
    ProductID
    price
    attributes {
      ...on Laptop {
        RAM
        CPU
      }
      ...on GPU {
        clock
        memory
      }
    }
  }
}

This might be thes results:

{
"data": {
    "queryProduct": [
      {
        "ProductID": "0x3e0e6bfb2",
        "price": 1000,
        "attributes": {
          "clock": 50,
          "memory": 100
        }
      },
      {
        "ProductID": "0x3e0e6bfb4",
        "price": 1000,
        "attributes": {
          "RAM": "16GB",
          "CPU": "i7"
        }
      }
    ]
}

But keep in mind that there are some bugs related to unions like this one.

  • Another approach is to have some set of key values:
type Attribute {
  AttributeID: ID
  attribute:String!
  value:String!
}

type Product {
  ProductID:ID
  attributes: [Attribute]
  price: Int
  created_at: DateTime
}

Now for inserting a laptop:

mutation addProducts {
  addProduct(input:{
    price:1000,
    created_at:"2021",
    attributes:[
      {
        attribute: "cpu"
      	value: "i7"
      },
      {
        attribute:"ram",
        value: "16G"
      }
    ]
  }) {
    numUids
  }
}

And for getting the products you can use this:

query getProducts {
  queryProduct {
    ProductID
    price
    attributes {
      attribute
      value
    }
  }
}

And this could be the result of the query:

  "data": {
    "queryProduct": [
      {
        "ProductID": "0x3e0e6bfb6",
        "price": 1000,
        "attributes": [
          {
            "attribute": "cpu",
            "value": "i7"
          },
          {
            "attribute": "ram",
            "value": "16G"
          }
        ]
      }
    ]
1 Like

@pshaddel thank you very much for your reply.

1 Like