|
5 | 5 | buildAmplifyConfig,
|
6 | 6 | mockedGenerateClient,
|
7 | 7 | optionsAndHeaders,
|
| 8 | + expectGraphqlMatches, |
8 | 9 | useState,
|
9 | 10 | } from '../../utils';
|
10 | 11 |
|
@@ -35,6 +36,103 @@ describe('lazy loaders edges', () => {
|
35 | 36 |
|
36 | 37 | type Schema = ClientSchema<typeof schema>;
|
37 | 38 |
|
| 39 | + const compositeKeySchema = a.schema({ |
| 40 | + Order: a.model({ |
| 41 | + id: a.id(), |
| 42 | + customerFirstName: a.string().required(), |
| 43 | + customerLastName: a.string().required(), |
| 44 | + customerRegion: a.string().required(), |
| 45 | + customer: a.belongsTo('Customer', ['customerFirstName','customerLastName', 'customerRegion']), |
| 46 | + }).authorization(allow => [allow.publicApiKey()]), |
| 47 | + Customer: a.model({ |
| 48 | + customerFirstName: a.string().required(), |
| 49 | + customerLastName: a.string().required(), |
| 50 | + customerRegion: a.string().required(), |
| 51 | + orders: a.hasMany('Order', ['customerFirstName','customerLastName', 'customerRegion']), |
| 52 | + }).identifier(['customerFirstName', 'customerLastName','customerRegion']).authorization(allow => [allow.publicApiKey()]), |
| 53 | + }) |
| 54 | + |
| 55 | + type CompositeKeySchema = ClientSchema<typeof compositeKeySchema>; |
| 56 | + |
| 57 | + test('belongsTo: related data is queried with foreign keys', async() => { |
| 58 | + const { generateClient, spy} = mockedGenerateClient([ |
| 59 | + { |
| 60 | + data: { |
| 61 | + getOrder: { |
| 62 | + __typeName: 'Order', |
| 63 | + id: 'order-id', |
| 64 | + customerFirstName: 'Some First Name', |
| 65 | + customerLastName: 'Some Last Name', |
| 66 | + customerRegion: 'Region A', |
| 67 | + updatedAt: '2024-03-01T19:05:44.536Z', |
| 68 | + createdAt: '2024-03-01T18:05:44.536Z', |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + data: { |
| 74 | + getCustomer: { |
| 75 | + __typeName: 'Customer', |
| 76 | + customerFirstName: 'Some First Name', |
| 77 | + customerLastName: 'Some Last Name', |
| 78 | + region: 'Region A', |
| 79 | + orders: [ |
| 80 | + { |
| 81 | + __typeName: 'Order', |
| 82 | + id: 'order-id', |
| 83 | + customerFirstName: 'Some First Name', |
| 84 | + customerLastName: 'Some Last Name', |
| 85 | + customerRegion: 'Region A', |
| 86 | + updatedAt: '2024-03-01T19:05:44.536Z', |
| 87 | + createdAt: '2024-03-01T18:05:44.536Z', |
| 88 | + }, |
| 89 | + ], |
| 90 | + updatedAt: '2024-03-01T19:05:44.536Z', |
| 91 | + createdAt: '2024-03-01T18:05:44.536Z', |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + data: { |
| 97 | + getCustomer: { |
| 98 | + __typeName: 'Customer', |
| 99 | + customerFirstName: 'Another First Name', |
| 100 | + customerLastName: 'Another Last Name', |
| 101 | + customerRegion: 'Region B', |
| 102 | + orders: [ |
| 103 | + ], |
| 104 | + updatedAt: '2077-03-01T19:05:44.536Z', |
| 105 | + createdAt: '2077-03-01T18:05:44.536Z', |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ]); |
| 110 | + const config = await buildAmplifyConfig(compositeKeySchema); |
| 111 | + Amplify.configure(config); |
| 112 | + const client = generateClient<CompositeKeySchema>(); |
| 113 | + |
| 114 | + const { data: order } = await client.models.Order.get({ |
| 115 | + id: 'order-id', |
| 116 | + }); |
| 117 | + const { data: customer } = await order!.customer(); |
| 118 | + |
| 119 | + //both primary key and sort key are used in query |
| 120 | + expectGraphqlMatches( |
| 121 | + optionsAndHeaders(spy)[1][0].query, |
| 122 | + `query($customerFirstName: String!,$customerLastName: String!, $customerRegion: String!) |
| 123 | + { getCustomer(customerFirstName: $customerFirstName,customerLastName: $customerLastName, |
| 124 | + customerRegion: $customerRegion) |
| 125 | + { customerFirstName customerLastName customerRegion createdAt updatedAt } }`, |
| 126 | + ); |
| 127 | + |
| 128 | + //sort keys are processed by the reduce() correctly |
| 129 | + expect(optionsAndHeaders(spy)[1][0].variables).toEqual({ |
| 130 | + customerFirstName: 'Some First Name', |
| 131 | + customerLastName: 'Some Last Name', |
| 132 | + customerRegion: 'Region A', |
| 133 | + }) |
| 134 | + }); |
| 135 | + |
38 | 136 | describe('when target data does not exist', () => {
|
39 | 137 | test('hasMany: returns []', async () => {
|
40 | 138 | const { generateClient } = mockedGenerateClient([
|
|
0 commit comments