Skip to content

Commit

Permalink
[DOCS-3786] Remove FQL typer workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodewig committed Nov 21, 2024
1 parent 21b78da commit d142d14
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ Customer documents and related API responses:
+ // Use a computed field to calculate the customer's cumulative purchase total.
+ // The field sums purchase `total` values from the customer's linked Order documents.
+ compute totalPurchaseAmt: Number = (customer => customer.orders.fold(0, (sum, order) => {
+ let order: Any = order
+ sum + order.total
+ }))
...
Expand Down
5 changes: 2 additions & 3 deletions schema/collections.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ collection Order {

compute items: Set<OrderItem> = (order => OrderItem.byOrder(order))
compute total: Number = (order => order.items.fold(0, (sum, orderItem) => {
let orderItem: Any = orderItem
if (orderItem.product != null) {
sum + orderItem.product.price * orderItem.quantity
sum + orderItem.product!.price * orderItem.quantity
} else {
sum
}
Expand Down Expand Up @@ -113,4 +112,4 @@ collection OrderItem {
index byOrderAndProduct {
terms [.order, .product]
}
}
}
8 changes: 4 additions & 4 deletions schema/functions.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getOrCreateCart(id) {

function checkout(orderId, status, payment) {
// Find the order by id, using the ! operator to assert that the order exists.
let order: Any = Order.byId(orderId)!
let order = Order.byId(orderId)!

// Check that we are setting the order to the processing status. If not, we should
// not be calling this function.
Expand Down Expand Up @@ -98,15 +98,15 @@ function checkout(orderId, status, payment) {

// Check that the order items are still in stock.
order!.items.forEach((item) => {
let product: Any = item.product
let product = item.product
if (product.stock < item.quantity) {
abort("One of the selected products does not have the requested quantity in stock.")
}
})

// Decrement the stock of each product in the order.
order!.items.forEach((item) => {
let product: Any = item.product
let product = item.product
product.update({ stock: product.stock - item.quantity })
})

Expand All @@ -129,4 +129,4 @@ function validateOrderStatusTransition(oldStatus, newStatus) {
// The order can only transition from shipped to delivered.
abort("Invalid status transition.")
}
}
}
8 changes: 4 additions & 4 deletions seed/orders.fql
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"query": "
let customer = Customer.byEmail('fake@fauna.com').first()!\n
let orders = ['cart', 'processing', 'shipped', 'delivered'].map(status => {\n
let order: Any = Order.byCustomer(customer).firstWhere(o => o.status == status)\n
let order = Order.byCustomer(customer).firstWhere(o => o.status == status)\n
if (order == null) {\n
let newOrder: Any = Order.create({\n
let newOrder = Order.create({\n
customer: customer,\n
status: status,\n
createdAt: Time.now(),\n
payment: {}\n
})\n
let product: Any = Product.byName('Drone').first()!\n
let orderItem: Any = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n
let product = Product.byName('Drone').first()!\n
let orderItem = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n
orderItem\n
newOrder\n
} else {\n
Expand Down
2 changes: 1 addition & 1 deletion seed/products.fql
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
'category': 'movies'
}
].map(p => {\n
let existing: Any = Product.byName(p.name).first()\n
let existing = Product.byName(p.name).first()\n
if (existing != null) {\n
existing!.update({ stock: p.stock })\n
} else {\n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Future<Customer> get(@PathVariable String id) {
//
// Use projection to only return the fields you need.
Query query = fql("""
let customer: Any = Customer.byId(${id})!
let customer = Customer.byId(${id})!
${response}
""", args);

Expand All @@ -71,7 +71,7 @@ Future<Customer> create(@RequestBody Customer customer) {
// Create a new Customer document with the provided fields.
// Use projection to only return the fields you need.
Query query = fql("""
let customer: Any = Customer.create(${customer})
let customer = Customer.create(${customer})
${response}
""", args);

Expand Down Expand Up @@ -99,7 +99,7 @@ Future<Customer> update(@PathVariable String id, @RequestBody Customer customer)
//
// Use projection to only return the fields you need.
Query query = fql("""
let customer: Any = Customer.byId(${id})!.update(${customer})
let customer = Customer.byId(${id})!.update(${customer})
${response}
""", args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public OrdersController(FaunaClient client) {
Future<Order> get(@PathVariable String id) {
var args = Map.of("id", id, "response", response);
var q = fql("""
let order: Any = Order.byId(${id})!
let order = Order.byId(${id})!
${response}
""", args);

Expand All @@ -118,7 +118,7 @@ Future<Order> update(@PathVariable String id, @RequestBody OrderUpdate req) {
// as the order status.
query = fql("""
let req = ${req}
let order: Any = checkout(${id}, req.status, req.payment)
let order = checkout(${id}, req.status, req.payment)
${response}
""", args);
} else {
Expand All @@ -127,7 +127,7 @@ Future<Order> update(@PathVariable String id, @RequestBody OrderUpdate req) {
// error. We then use the validateOrderStatusTransition UDF to ensure that the order status transition
// is valid. If the transition is not valid, the UDF will throw an abort error.
query = fql("""
let order: Any = Order.byId(${id})!
let order = Order.byId(${id})!
let req = ${req}
// Validate the order status transition if a status is provided.
Expand Down Expand Up @@ -162,7 +162,7 @@ Future<Page<Order>> getByCustomer(@PathVariable("id") String customerId, @Reques
if (afterToken != null) {
/**
* The `afterToken` parameter contains a Fauna `after` cursor.
* `after` cursors may contain special characters, such as `.` or `+`).
* `after` cursors may contain special characters, such as `.` or `+`).
* Make sure to URL encode the `afterToken` value to preserve these
* characters in URLs.
*/
Expand All @@ -177,10 +177,8 @@ Future<Page<Order>> getByCustomer(@PathVariable("id") String customerId, @Reques
// the results to return only the fields we care about.
var args = Map.of("customerId", customerId,"pageSize", pageSize,"response", response);
query = fql("""
let customer: Any = Customer.byId(${customerId})!
let customer = Customer.byId(${customerId})!
Order.byCustomer(customer).pageSize(${pageSize}).map((order) => {
let order: Any = order
// Return the order.
${response}
})
Expand All @@ -201,7 +199,7 @@ Future<Order> createCart(@PathVariable("id") String customerId) {
// definition can be found 'server/schema/functions.fsl'.
Map<String, Object> args = Map.of("customerId", customerId, "response", response);
Query query = fql("""
let order: Any = getOrCreateCart(${customerId})
let order = getOrCreateCart(${customerId})
// Return the cart.
${response}
Expand All @@ -223,7 +221,7 @@ Future<Order> addToCart(@PathVariable("id") String customerId, @RequestBody AddT
Map<String, Object> args = Map.of("customerId", customerId, "req", req, "response", response);
Query query = fql("""
let req = ${req}
let order: Any = createOrUpdateCartItem(${customerId}, req.productName, req.quantity)
let order = createOrUpdateCartItem(${customerId}, req.productName, req.quantity)
// Return the cart as an OrderResponse object.
${response}
Expand All @@ -244,7 +242,7 @@ Future<Order> getCart(@PathVariable("id") String customerId) {
// If the document does not exist, Fauna will throw a document_not_found error.
Map<String, Object> args = Map.of("customerId", customerId, "response", response);
Query query = fql("""
let order: Any = Customer.byId(${customerId})!.cart
let order = Customer.byId(${customerId})!.cart
// Return the cart as an OrderResponse object.
${response}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Future<Page<Product>> paginate(
if (afterToken != null) {
/**
* The `afterToken` parameter contains a Fauna `after` cursor.
* `after` cursors may contain special characters, such as `.` or `+`).
* `after` cursors may contain special characters, such as `.` or `+`).
* Make sure to URL encode the `afterToken` value to preserve these
* characters in URLs.
*/
Expand Down Expand Up @@ -73,14 +73,16 @@ Future<Page<Product>> paginate(
${subQuery}
.map(product => {
let product: Any = product
let category: Any = product.category
{
id: product.id,
name: product.name,
price: product.price,
description: product.description,
stock: product.stock,
category: { id: category.id, name: category.name, description: category.description },
category: {
id: product.category.id,
name: product.category.name,
description: product.category.description },
}
})
""", Map.of("subQuery", subQuery));
Expand Down Expand Up @@ -113,7 +115,7 @@ Future<Product> create(@RequestBody ProductRequest req) {
// Create the product with the given values.
let args = { name: input.name, price: input.price, stock: input.stock, description: input.description, category: category }
let product: Any = Product.create(args)
let product = Product.create(args)
// Use projection to only return the fields you need.
product {
Expand Down Expand Up @@ -147,7 +149,7 @@ Future<Product> update(@PathVariable String id, @RequestBody ProductRequest req)
// Get the product by id, using the ! operator to assert that the product exists.
// If it does not exist Fauna will throw a document_not_found error.
let product: Any = Product.byId(${id})!
let product = Product.byId(${id})!
// Get the category by name. We can use .first() here because we know that the category
// name is unique.
Expand Down Expand Up @@ -217,7 +219,6 @@ Future<Page<Product>> search(@RequestParam(required = false) Integer minPrice,
query = fql("""
Product.sortedByPriceLowToHigh({ from: ${minPrice}, to: ${maxPrice}})
.pageSize(${pageSize}).map(product => {
let product: Any = product
product {
id,
name,
Expand Down

0 comments on commit d142d14

Please sign in to comment.