Attach Applied Discount Database Table

This example outlines how to attach a database table that’s in the same Amazon RDS instance as the example solution. Suppose you want to attach your MyAppliedDiscount table to apply a discount for one or more order items.

Update the Schema 

Add the type and input declarations for the MyAppliedDiscount table to the GraphQL schema.

1input AWSDateOperator {
2	eq: AWSDate
3	gt: AWSDate
4	ge: AWSDate
5	in: [AWSDate]
6	like: AWSDate
7	lt: AWSDate
8	le: AWSDate
9	ne: AWSDate
10	nin: [AWSDate]
11}
12
13input AWSDateTimeOperator {
14	eq: AWSDateTime
15	gt: AWSDateTime
16	ge: AWSDateTime
17	in: [AWSDateTime]
18	like: AWSDateTime
19	lt: AWSDateTime
20	le: AWSDateTime
21	ne: AWSDateTime
22	nin: [AWSDateTime]
23}
24
25input AWSJSONOperator {
26	eq: AWSJSON
27	gt: AWSJSON
28	ge: AWSJSON
29	in: [AWSJSON]
30	like: AWSJSON
31	lt: AWSJSON
32	le: AWSJSON
33	ne: AWSJSON
34	nin: [AWSJSON]
35}
36
37input AWSTimeOperator {
38	eq: AWSTime
39	gt: AWSTime
40	ge: AWSTime
41	in: [AWSTime]
42	like: AWSTime
43	lt: AWSTime
44	le: AWSTime
45	ne: AWSTime
46	nin: [AWSTime]
47}
48
49input BooleanOperator {
50	eq: Boolean
51	gt: Boolean
52	ge: Boolean
53	in: [Boolean]
54	like: Boolean
55	lt: Boolean
56	le: Boolean
57	ne: Boolean
58	nin: [Boolean]
59}
60
61enum Direction {
62	ASC
63	DESC
64}
65
66enum Enumeration {
67	VALUE1
68	VALUE2
69	VALUE3
70	VALUE4
71	VALUE5
72	VALUE6
73	VALUE7
74	VALUE8
75	VALUE9
76}
77
78input FloatOperator {
79	eq: Float
80	gt: Float
81	ge: Float
82	in: [Float]
83	like: Float
84	lt: Float
85	le: Float
86	ne: Float
87	nin: [Float]
88}
89
90input IDOperator {
91	eq: ID
92	gt: ID
93	ge: ID
94	in: [ID]
95	like: ID
96	lt: ID
97	le: ID
98	ne: ID
99	nin: [ID]
100}
101
102input IntOperator {
103	eq: Int
104	gt: Int
105	ge: Int
106	in: [Int]
107	like: Int
108	lt: Int
109	le: Int
110	ne: Int
111	nin: [Int]
112}
113
114type Mutation {
115	create_MyOrder(input: MyOrder_CreateInput!): MyOrder
116	create_MyOrderItem(input: MyOrderItem_CreateInput!): MyOrderItem
117	create_MyProduct(input: MyProduct_CreateInput!): MyProduct
118	create_MyAppliedDiscount(input: MyAppliedDiscount_CreateInput!): MyAppliedDiscount
119	delete_MyOrder(id: ID!): MyOrder
120	delete_MyOrderItem(id: ID!): MyOrderItem
121	delete_MyProduct(id: ID!): MyProduct
122	delete_MyAppliedDiscount(id: ID!): MyAppliedDiscount
123	update_MyOrder(input: MyOrder_UpdateInput!): MyOrder
124	update_MyOrderItem(input: MyOrderItem_UpdateInput!): MyOrderItem
125	update_MyProduct(input: MyProduct_UpdateInput!): MyProduct
126	update_MyAppliedDiscount(input: MyAppliedDiscount_UpdateInput!): MyAppliedDiscount
127}
128
129type MyAppliedDiscount implements Node {
130	id: ID!
131	DiscountId: ID!
132	OrderItemOrderId: String
133	OrderItemProductId: String
134	Multiplier: Float
135}
136
137type MyAppliedDiscount_Connection {
138	edges: [MyAppliedDiscount_Edge]
139	pageInfo: PageInfo!
140}
141
142input MyAppliedDiscount_CreateInput {
143	DiscountId: ID!
144	OrderItemOrderId: String
145	OrderItemProductId: String
146	Multiplier: Float
147}
148
149type MyAppliedDiscount_Edge {
150	cursor: String!
151	node: MyAppliedDiscount
152}
153
154input MyAppliedDiscount_FilterInput {
155	and: [MyAppliedDiscount_FilterInput]
156	not: MyAppliedDiscount_FilterInput
157	or: [MyAppliedDiscount_FilterInput]
158	id: IDOperator
159	DiscountId: StringOperator
160	OrderItemOrderId: StringOperator
161	OrderItemProductId: StringOperator
162	Multiplier: FloatOperator
163}
164
165input MyAppliedDiscount_OrderByInput {
166	id: OrderByClause
167	DiscountId: OrderByClause
168	OrderItemOrderId: OrderByClause
169	OrderItemProductId: OrderByClause
170	Multiplier: OrderByClause
171}
172
173input MyAppliedDiscount_UpdateInput {
174	id: ID!
175	DiscountId: ID!
176	OrderItemOrderId: String
177	OrderItemProductId: String
178	Multiplier: Float
179}
180
181type MyOrder implements Node {
182	id: ID!
183	OrderId: ID!
184	OrderDate: AWSDateTime
185	Status: String
186	TotalCost: Float
187}
188
189type MyOrderItem implements Node {
190	id: ID!
191	ParentOrderId: ID!
192	ParentProductId: ID!
193	Quantity: Int
194}
195
196type MyOrderItem_Connection {
197	edges: [MyOrderItem_Edge]
198	pageInfo: PageInfo!
199}
200
201input MyOrderItem_CreateInput {
202	ParentOrderId: ID
203	ParentProductId: ID
204	Quantity: Int
205}
206
207type MyOrderItem_Edge {
208	cursor: String!
209	node: MyOrderItem
210}
211
212input MyOrderItem_FilterInput {
213	and: [MyOrderItem_FilterInput]
214	not: MyOrderItem_FilterInput
215	or: [MyOrderItem_FilterInput]
216	id: IDOperator
217	ParentOrderId: StringOperator
218	ParentProductId: StringOperator
219	Quantity: IntOperator
220}
221
222input MyOrderItem_OrderByInput {
223	id: OrderByClause
224	ParentOrderId: OrderByClause
225	ParentProductId: OrderByClause
226	Quantity: OrderByClause
227}
228
229input MyOrderItem_UpdateInput {
230	id: ID!
231	ParentOrderId: ID
232	ParentProductId: ID
233	Quantity: Int
234}
235
236type MyOrder_Connection {
237	edges: [MyOrder_Edge]
238	pageInfo: PageInfo!
239}
240
241input MyOrder_CreateInput {
242	OrderDate: AWSDateTime
243	OrderId: ID!
244	Status: String
245	TotalCost: Float
246}
247
248type MyOrder_Edge {
249	cursor: String!
250	node: MyOrder
251}
252
253input MyOrder_FilterInput {
254	and: [MyOrder_FilterInput]
255	not: MyOrder_FilterInput
256	or: [MyOrder_FilterInput]
257	id: IDOperator
258	OrderDate: AWSDateTimeOperator
259	OrderId: StringOperator
260	Status: StringOperator
261	TotalCost: FloatOperator
262}
263
264input MyOrder_OrderByInput {
265	id: OrderByClause
266	OrderId: OrderByClause
267	OrderDate: OrderByClause
268	Status: OrderByClause
269	TotalCost: OrderByClause
270}
271
272input MyOrder_UpdateInput {
273	id: ID!
274	OrderDate: AWSDateTime
275	OrderId: String
276	Status: String
277	TotalCost: Float
278}
279
280type MyProduct implements Node {
281	id: ID!
282	ProductId: ID!
283	Name: String
284	Price: Float
285	Discontinued: Boolean
286}
287
288type MyProduct_Connection {
289	edges: [MyProduct_Edge]
290	pageInfo: PageInfo!
291}
292
293input MyProduct_CreateInput {
294	ProductId: ID!
295	Name: String
296	Price: Float
297	Discontinued: Boolean
298}
299
300type MyProduct_Edge {
301	cursor: String!
302	node: MyProduct
303}
304
305input MyProduct_FilterInput {
306	and: [MyProduct_FilterInput]
307	not: MyProduct_FilterInput
308	or: [MyProduct_FilterInput]
309	id: IDOperator
310	ProductId: StringOperator
311	Name: StringOperator
312	Price: FloatOperator
313	Discontinued: BooleanOperator
314}
315
316input MyProduct_OrderByInput {
317	id: OrderByClause
318	ProductId: OrderByClause
319	Name: OrderByClause
320	Price: OrderByClause
321	Discontinued: OrderByClause
322}
323
324input MyProduct_UpdateInput {
325	id: ID!
326	ProductId: String
327	Name: String
328	Price: Float
329	Discontinued: Boolean
330}
331
332interface Node {
333	id: ID!
334}
335
336enum NullsOrder {
337	NULLS_FIRST
338	NULLS_LAST
339}
340
341input OrderByClause {
342	direction: Direction
343	nulls: NullsOrder
344}
345
346type PageInfo {
347	endCursor: String
348	hasNextPage: Boolean!
349}
350
351type Query {
352	myOrder(
353		limit: Int,
354		offset: Int,
355		orderBy: [MyOrder_OrderByInput],
356		where: MyOrder_FilterInput,
357		first: Int,
358		after: String
359	): MyOrder_Connection
360	myOrderItem(
361		limit: Int,
362		offset: Int,
363		orderBy: [MyOrderItem_OrderByInput],
364		where: MyOrderItem_FilterInput,
365		first: Int,
366		after: String
367	): MyOrderItem_Connection
368	myProduct(
369		limit: Int,
370		offset: Int,
371		orderBy: [MyProduct_OrderByInput],
372		where: MyProduct_FilterInput,
373		first: Int,
374		after: String
375	): MyProduct_Connection
376	myAppliedDiscount(
377		limit: Int,
378		offset: Int,
379		orderBy: [MyAppliedDiscount_OrderByInput],
380		where: MyAppliedDiscount_FilterInput,
381		first: Int,
382		after: String
383	): MyAppliedDiscount_Connection
384	node(id: ID!): Node
385}
386
387input StringOperator {
388	eq: String
389	gt: String
390	ge: String
391	in: [String]
392	like: String
393	lt: String
394	le: String
395	ne: String
396	nin: [String]
397}

Update the Table Metadata 

Add metadata entries in the Parameter Store for the MyAppliedDiscount table.

1**/appsync/typemetadata/MyOrder**
2
3{
4  "secretName": "graphqlsample",
5  "secretRegion": "us-west-2",
6  "keyFields": [
7    "OrderId"
8  ],
9  "databaseTableName": "graphqlsample.my_order",
10  "graphQLFields": {
11    "OrderId": {
12      "fieldType": "String",
13      "columnName": "order_id"
14    },
15    "OrderDate": {
16      "fieldType": "AWSDateTime",
17      "columnName": "order_date"
18    },
19    "Status": {
20      "fieldType": "String",
21      "columnName": "status"
22    },
23    "TotalCost": {
24      "fieldType": "Float",
25      "columnName": "total_cost"
26    }
27  }
28}
29
30
31**/appsync/typemetadata/MyOrderItem**
32
33
34{
35  "secretName": "graphqlsample",
36  "secretRegion": "us-west-2",
37  "keyFields": [
38    "ParentOrderId",
39    "ParentProductId"
40  ],
41  "databaseTableName": "graphqlsample.my_order_item",
42  "graphQLFields": {
43    "ParentOrderId": {
44      "fieldType": "String",
45      "columnName": "parent_order_id"
46    },
47    "ParentProductId": {
48      "fieldType": "String",
49      "columnName": "parent_product_id"
50    },
51    "Quantity": {
52      "fieldType": "Int",
53      "columnName": "quantity"
54    }
55  }
56}
57
58**/appsync/typemetadata/MyProduct**
59
60{
61  "secretName": "graphqlsample",
62  "secretRegion": "us-west-2",
63    "keyFields": ["ProductId"],
64  "databaseTableName": "graphqlsample.my_product",
65  "graphQLFields": {
66    "ProductId": {
67      "fieldType": "String",
68      "columnName": "product_id"
69    },
70    "Name": {
71      "fieldType": "String",
72      "columnName": "name"
73    },
74    "Price": {
75      "fieldType": "Float",
76      "columnName": "price"
77    },
78    "Discontinued": {
79      "fieldType": "Boolean",
80      "columnName": "discontinued"
81    }
82  }
83}
84
85**/appsync/typemetadata/MyAppliedDiscount**
86
87{
88  "secretName": "graphqlsample",
89  "secretRegion": "us-west-2",
90  "keyFields": [
91    "DiscountId",
92    "OrderItemOrderId",
93    "OrderItemProductId"
94  ],
95  "databaseTableName": "postgres.my_applied_discount",
96  "graphQLFields": {
97    "DiscountId": {
98      "fieldType": "String",
99      "columnName": "discount_id"
100    },
101    "OrderItemOrderId": {
102      "fieldType": "String",
103      "columnName": "order_item_order_id"
104    },
105    "OrderItemProductId": {
106      "fieldType": "String",
107      "columnName": "order_item_product_id"
108    },
109    "Multiplier": {
110      "fieldType": "Float",
111      "columnName": "multiplier"
112    }
113  }
114}

Generate Table Qualifiers 

MyOrder 

This is a sample table qualifier generated for an external ID based on the id field of the MyOrder table.

1{
2  "columns": {
3    "id": {
4      "values": [
5        {
6          "definition": "\"MyOrder-\"&SUBSTITUTE(ExternalId, \"-\", \"\\\\-\")"
7        }
8      ]
9    },
10    "ExternalId": {
11      "virtual": true,
12      "values": [
13        {
14          "definition": "SUBSTITUTE(RIGHT(id, LEN(id)-8), \"\\\\-\", \"-\")"
15        }
16      ]
17    }
18  }
19}

MyOrderItem 

This is a sample table qualifier generated for an external ID based on the id field of the MyOrderItem table.

1{
2  "columns": {
3    "id": {
4      "values": [
5        {
6          "definition": "\"MyOrderItem-\"&SUBSTITUTE(SUBSTITUTE(ExternalId, \"-\", \"\\\\-\"), \"\\\\-PRD\", \"-PRD\")"
7        }
8      ]
9    },
10    "ExternalId": {
11      "virtual": true,
12      "values": [
13        {
14          "definition": "SUBSTITUTE(RIGHT(id, LEN(id)-12), \"\\\\-\", \"-\")"
15        }
16      ]
17    }
18  }
19}

MyProduct 

This is a sample table qualifier generated for an external ID based on the id field of the MyProduct table.

1{
2  "columns": {
3    "id": {
4      "values": [
5        {
6          "definition": "\"MyProduct-\"&SUBSTITUTE(ExternalId, \"-\", \"\\\\-\")"
7        }
8      ]
9    },
10    "ExternalId": {
11      "virtual": true,
12      "values": [
13        {
14          "definition": "SUBSTITUTE(RIGHT(id, LEN(id)-10), \"\\\\-\", \"-\")"
15        }
16      ]
17    }
18  }
19}

MyAppliedDiscount 

This is a sample table qualifier for a virtual external ID defined using the values formula.

1{
2  "columns": {
3    "id": {
4      "values": [
5        {
6          "definition": "\"MyAppliedDiscount-\"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(ExternalId, \"-\", \"\\\\-\"), \"\\\\-ORD\", \"-ORD\"), \"\\\\-PRD\", \"-PRD\")"
7        }
8      ]
9    },
10    "ExternalId": {
11      "virtual": true,
12      "values": [
13        {
14          "definition": "SUBSTITUTE(RIGHT(id, LEN(id)-18), \"\\\\-\", \"-\")"
15        }
16      ]
17    },
18    "OrderItemId": {
19      "virtual": true,
20      "values": [
21        {
22          "definition": "OrderItemOrderId&\"-\"&OrderItemProductId"
23        }
24      ]
25    },
26    "OrderItemOrderId": {
27      "values": [
28        {
29          "definition": "LEFT(OrderItemId, FIND(\"-PRD\", OrderItemId)-1)"
30        }
31      ]
32    },
33    "OrderItemProductId": {
34      "values": [
35        {
36          "definition": "RIGHT(OrderItemId, LEN(OrderItemId)-FIND(\"-PRD\",OrderItemId))"
37        }
38      ]
39    }
40  }
41}