These are all trivial in SQL but doing these in FQL have become a challenge for me, I’ve spent days just staring at code and getting nowhere other than solving basic queries.
Maybe someone can help explain how to accomplish this and break it down into parts to explain what actually happens to the data and how it relates to the results?
What I’m trying to accomplish:
- Find all products with specific colours (One or multiple)
- Find all products with specific size and colours (One or multiple)
- Find all products with specific price or price range
- List all reviews for a specific product
- List all customer reviews for a specific product
- Filter on specifications (Products with specific specs)
- Search in specification (in combination with filter)
- Sort by: price, name, creation_date, weighting
This is a sample schema and sample data:
type Products {
sku: String! @unique
name: String! @unique
price: Float
colours: [Colours]! @relation
reviews: [Reviews] @relation
specifications: [Specifications] @relation
creation_date: String
}
type Colours {
name: String
products: [Products!] @relation
}
type Reviews {
published: Boolean
author: String
products: [Products!] @relation
content: String
}
type Specifications {
type: String
description: String
image_path: String
products: [Products!] @relation
}
Solutions
8 - Sorting
By name/price/date/weighting (Replace placeholders with field-name):
/* Create the index */
CreateIndex({
name: "products_sort_by_fieldname_asc",
source: Collection("Products"),
values: [
{ field: ["data", "fieldname"] },
{ field: ["ref"] }
]
})
/* Query */
Paginate(Match(Index("products_sort_by_fieldname_asc")))
For reverse order, add reverse: true
to the index field, like so:
{ field: ["data", "fieldname"], reverse: true }
8. Bonus > range search
/* Create index */
CreateIndex({
name: "products_by_date",
source: Collection("Products"),
values: [{field: ["data", "creation_date"]}, {field: ["ref"]}]
})
/* Query */
Map(
Paginate(
Range(
Match(Index("products_by_date")),
Date("2020-06-01"),
Date("2020-06-15")
),
),
Lambda((ts, ref) => Get(ref))
)