diff --git a/CHANGELOG.md b/CHANGELOG.md index 2569d8a..7a9b8b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to the API contract are documented here. ## Unreleased +- Add `Order.totalAmount`. `Order.total` is deprecated and will be removed once + thicket-web and thicket-mobile have migrated. + ## 0.1.0 - Initial schema: products, carts, orders, payments, checkout, and full refunds. diff --git a/README.md b/README.md index 98612a9..3b0f0ba 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Example query: orders(customerId: "cus_avery") { number status - total + totalAmount payment { status last4 } } } diff --git a/schema/schema.graphql b/schema/schema.graphql index d118aa5..eba39ef 100644 --- a/schema/schema.graphql +++ b/schema/schema.graphql @@ -63,7 +63,9 @@ type Order { "Sales tax, in USD." tax: Float! "Amount charged for the order (subtotal + tax), in USD." - total: Float! + totalAmount: Float! + "Amount charged for the order (subtotal + tax), in USD." + total: Float! @deprecated(reason: "Use totalAmount.") "ISO 4217 currency code." currency: String! payment: Payment! diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 75861b1..6d6bfe0 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -76,6 +76,8 @@ export const resolvers = { }, subtotal: (order: OrderRecord) => computeTotals(order.items).subtotal, tax: (order: OrderRecord) => computeTotals(order.items).tax, + totalAmount: (order: OrderRecord) => computeTotals(order.items).total, + /** @deprecated Use totalAmount. Kept until clients have migrated. */ total: (order: OrderRecord) => computeTotals(order.items).total, currency: () => 'USD', payment: (order: OrderRecord, _: unknown, { store }: Context) => { diff --git a/test/orders.test.ts b/test/orders.test.ts index 486d092..a8732ab 100644 --- a/test/orders.test.ts +++ b/test/orders.test.ts @@ -9,6 +9,7 @@ const ORDER = /* GraphQL */ ` subtotal tax total + totalAmount currency items { quantity unitPrice lineTotal product { name } } payment { status method last4 amount } @@ -27,6 +28,7 @@ describe('orders', () => { subtotal: 43.99, tax: 3.63, total: 47.62, + totalAmount: 47.62, currency: 'USD', }); }); @@ -35,7 +37,15 @@ describe('orders', () => { const { execute } = setup(); const { data } = await execute(ORDER, { id: 'ord_1001' }); expect(data.order.payment).toEqual({ status: 'CAPTURED', method: 'card', last4: '4242', amount: 47.62 }); - expect(data.order.payment.amount).toBe(data.order.total); + expect(data.order.payment.amount).toBe(data.order.totalAmount); + }); + + it('keeps the deprecated total field equal to totalAmount', async () => { + const { execute } = setup(); + const { data } = await execute(`{ orders { total totalAmount } }`); + for (const order of data.orders) { + expect(order.total).toBe(order.totalAmount); + } }); it('every order exposes a payment', async () => {