Skip to content

Commit f179d25

Browse files
committed
[DOCS-3786] Remove FQL typer workarounds
1 parent 21b78da commit f179d25

File tree

8 files changed

+29
-31
lines changed

8 files changed

+29
-31
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ Customer documents and related API responses:
249249
+ // Use a computed field to calculate the customer's cumulative purchase total.
250250
+ // The field sums purchase `total` values from the customer's linked Order documents.
251251
+ compute totalPurchaseAmt: Number = (customer => customer.orders.fold(0, (sum, order) => {
252-
+ let order: Any = order
253252
+ sum + order.total
254253
+ }))
255254
...

schema/collections.fsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ collection Order {
7272

7373
compute items: Set<OrderItem> = (order => OrderItem.byOrder(order))
7474
compute total: Number = (order => order.items.fold(0, (sum, orderItem) => {
75-
let orderItem: Any = orderItem
75+
let orderItem = orderItem
7676
if (orderItem.product != null) {
7777
sum + orderItem.product.price * orderItem.quantity
7878
} else {
@@ -113,4 +113,4 @@ collection OrderItem {
113113
index byOrderAndProduct {
114114
terms [.order, .product]
115115
}
116-
}
116+
}

schema/functions.fsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function getOrCreateCart(id) {
7070

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

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

9999
// Check that the order items are still in stock.
100100
order!.items.forEach((item) => {
101-
let product: Any = item.product
101+
let product = item.product
102102
if (product.stock < item.quantity) {
103103
abort("One of the selected products does not have the requested quantity in stock.")
104104
}
105105
})
106106

107107
// Decrement the stock of each product in the order.
108108
order!.items.forEach((item) => {
109-
let product: Any = item.product
109+
let product = item.product
110110
product.update({ stock: product.stock - item.quantity })
111111
})
112112

@@ -129,4 +129,4 @@ function validateOrderStatusTransition(oldStatus, newStatus) {
129129
// The order can only transition from shipped to delivered.
130130
abort("Invalid status transition.")
131131
}
132-
}
132+
}

seed/orders.fql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
"query": "
33
let customer = Customer.byEmail('fake@fauna.com').first()!\n
44
let orders = ['cart', 'processing', 'shipped', 'delivered'].map(status => {\n
5-
let order: Any = Order.byCustomer(customer).firstWhere(o => o.status == status)\n
5+
let order = Order.byCustomer(customer).firstWhere(o => o.status == status)\n
66
if (order == null) {\n
7-
let newOrder: Any = Order.create({\n
7+
let newOrder = Order.create({\n
88
customer: customer,\n
99
status: status,\n
1010
createdAt: Time.now(),\n
1111
payment: {}\n
1212
})\n
13-
let product: Any = Product.byName('Drone').first()!\n
14-
let orderItem: Any = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n
13+
let product = Product.byName('Drone').first()!\n
14+
let orderItem = OrderItem.create({ order: newOrder, product: product, quantity: 1 })\n
1515
orderItem\n
1616
newOrder\n
1717
} else {\n

seed/products.fql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
'category': 'movies'
6464
}
6565
].map(p => {\n
66-
let existing: Any = Product.byName(p.name).first()\n
66+
let existing = Product.byName(p.name).first()\n
6767
if (existing != null) {\n
6868
existing!.update({ stock: p.stock })\n
6969
} else {\n

src/main/java/fauna/sample/controllers/customers/CustomersController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Future<Customer> get(@PathVariable String id) {
5050
//
5151
// Use projection to only return the fields you need.
5252
Query query = fql("""
53-
let customer: Any = Customer.byId(${id})!
53+
let customer = Customer.byId(${id})!
5454
${response}
5555
""", args);
5656

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

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

src/main/java/fauna/sample/controllers/orders/OrdersController.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public OrdersController(FaunaClient client) {
9393
Future<Order> get(@PathVariable String id) {
9494
var args = Map.of("id", id, "response", response);
9595
var q = fql("""
96-
let order: Any = Order.byId(${id})!
96+
let order = Order.byId(${id})!
9797
${response}
9898
""", args);
9999

@@ -118,7 +118,7 @@ Future<Order> update(@PathVariable String id, @RequestBody OrderUpdate req) {
118118
// as the order status.
119119
query = fql("""
120120
let req = ${req}
121-
let order: Any = checkout(${id}, req.status, req.payment)
121+
let order = checkout(${id}, req.status, req.payment)
122122
${response}
123123
""", args);
124124
} else {
@@ -127,7 +127,7 @@ Future<Order> update(@PathVariable String id, @RequestBody OrderUpdate req) {
127127
// error. We then use the validateOrderStatusTransition UDF to ensure that the order status transition
128128
// is valid. If the transition is not valid, the UDF will throw an abort error.
129129
query = fql("""
130-
let order: Any = Order.byId(${id})!
130+
let order = Order.byId(${id})!
131131
let req = ${req}
132132
133133
// Validate the order status transition if a status is provided.
@@ -162,7 +162,7 @@ Future<Page<Order>> getByCustomer(@PathVariable("id") String customerId, @Reques
162162
if (afterToken != null) {
163163
/**
164164
* The `afterToken` parameter contains a Fauna `after` cursor.
165-
* `after` cursors may contain special characters, such as `.` or `+`).
165+
* `after` cursors may contain special characters, such as `.` or `+`).
166166
* Make sure to URL encode the `afterToken` value to preserve these
167167
* characters in URLs.
168168
*/
@@ -177,10 +177,8 @@ Future<Page<Order>> getByCustomer(@PathVariable("id") String customerId, @Reques
177177
// the results to return only the fields we care about.
178178
var args = Map.of("customerId", customerId,"pageSize", pageSize,"response", response);
179179
query = fql("""
180-
let customer: Any = Customer.byId(${customerId})!
180+
let customer = Customer.byId(${customerId})!
181181
Order.byCustomer(customer).pageSize(${pageSize}).map((order) => {
182-
let order: Any = order
183-
184182
// Return the order.
185183
${response}
186184
})
@@ -201,7 +199,7 @@ Future<Order> createCart(@PathVariable("id") String customerId) {
201199
// definition can be found 'server/schema/functions.fsl'.
202200
Map<String, Object> args = Map.of("customerId", customerId, "response", response);
203201
Query query = fql("""
204-
let order: Any = getOrCreateCart(${customerId})
202+
let order = getOrCreateCart(${customerId})
205203
206204
// Return the cart.
207205
${response}
@@ -223,7 +221,7 @@ Future<Order> addToCart(@PathVariable("id") String customerId, @RequestBody AddT
223221
Map<String, Object> args = Map.of("customerId", customerId, "req", req, "response", response);
224222
Query query = fql("""
225223
let req = ${req}
226-
let order: Any = createOrUpdateCartItem(${customerId}, req.productName, req.quantity)
224+
let order = createOrUpdateCartItem(${customerId}, req.productName, req.quantity)
227225
228226
// Return the cart as an OrderResponse object.
229227
${response}
@@ -244,7 +242,7 @@ Future<Order> getCart(@PathVariable("id") String customerId) {
244242
// If the document does not exist, Fauna will throw a document_not_found error.
245243
Map<String, Object> args = Map.of("customerId", customerId, "response", response);
246244
Query query = fql("""
247-
let order: Any = Customer.byId(${customerId})!.cart
245+
let order = Customer.byId(${customerId})!.cart
248246
249247
// Return the cart as an OrderResponse object.
250248
${response}

src/main/java/fauna/sample/controllers/products/ProductsController.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Future<Page<Product>> paginate(
4242
if (afterToken != null) {
4343
/**
4444
* The `afterToken` parameter contains a Fauna `after` cursor.
45-
* `after` cursors may contain special characters, such as `.` or `+`).
45+
* `after` cursors may contain special characters, such as `.` or `+`).
4646
* Make sure to URL encode the `afterToken` value to preserve these
4747
* characters in URLs.
4848
*/
@@ -73,14 +73,16 @@ Future<Page<Product>> paginate(
7373
${subQuery}
7474
.map(product => {
7575
let product: Any = product
76-
let category: Any = product.category
7776
{
7877
id: product.id,
7978
name: product.name,
8079
price: product.price,
8180
description: product.description,
8281
stock: product.stock,
83-
category: { id: category.id, name: category.name, description: category.description },
82+
category: {
83+
id: product.category.id,
84+
name: product.category.name,
85+
description: product.category.description },
8486
}
8587
})
8688
""", Map.of("subQuery", subQuery));
@@ -113,7 +115,7 @@ Future<Product> create(@RequestBody ProductRequest req) {
113115
114116
// Create the product with the given values.
115117
let args = { name: input.name, price: input.price, stock: input.stock, description: input.description, category: category }
116-
let product: Any = Product.create(args)
118+
let product = Product.create(args)
117119
118120
// Use projection to only return the fields you need.
119121
product {
@@ -147,7 +149,7 @@ Future<Product> update(@PathVariable String id, @RequestBody ProductRequest req)
147149
148150
// Get the product by id, using the ! operator to assert that the product exists.
149151
// If it does not exist Fauna will throw a document_not_found error.
150-
let product: Any = Product.byId(${id})!
152+
let product = Product.byId(${id})!
151153
152154
// Get the category by name. We can use .first() here because we know that the category
153155
// name is unique.
@@ -217,7 +219,6 @@ Future<Page<Product>> search(@RequestParam(required = false) Integer minPrice,
217219
query = fql("""
218220
Product.sortedByPriceLowToHigh({ from: ${minPrice}, to: ${maxPrice}})
219221
.pageSize(${pageSize}).map(product => {
220-
let product: Any = product
221222
product {
222223
id,
223224
name,

0 commit comments

Comments
 (0)