Skip to content

Commit 96b7b9d

Browse files
authored
fix(codegen-ui): fix multiple has one relationships (#1005)
* fix(codegen-ui): fix multiple has one relationships * chore(deps): run in band --------- Co-authored-by: Christopher Woolum <[email protected]>
1 parent 8db1582 commit 96b7b9d

File tree

5 files changed

+351
-49
lines changed

5 files changed

+351
-49
lines changed

packages/codegen-ui-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"scripts": {
1515
"test": "jest",
1616
"test:watch": "jest --watch",
17-
"test:ci": "jest --ci --maxWorkers=30%",
17+
"test:ci": "jest --ci -i",
1818
"test:update": "jest --updateSnapshot",
1919
"build": "tsc -p tsconfig.build.json",
2020
"build:watch": "npm run build -- --watch"
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
{
2+
"version": "1",
3+
"models": {
4+
"Foo": {
5+
"name": "Foo",
6+
"fields": {
7+
"id": {
8+
"name": "id",
9+
"isArray": false,
10+
"type": "ID",
11+
"isRequired": true,
12+
"attributes": []
13+
},
14+
"User1": {
15+
"name": "User1",
16+
"isArray": false,
17+
"type": {
18+
"model": "User"
19+
},
20+
"isRequired": false,
21+
"attributes": [],
22+
"association": {
23+
"connectionType": "HAS_ONE",
24+
"associatedWith": [
25+
"id"
26+
],
27+
"targetNames": [
28+
"fooUser1Id"
29+
]
30+
}
31+
},
32+
"User2": {
33+
"name": "User2",
34+
"isArray": false,
35+
"type": {
36+
"model": "User"
37+
},
38+
"isRequired": false,
39+
"attributes": [],
40+
"association": {
41+
"connectionType": "HAS_ONE",
42+
"associatedWith": [
43+
"id"
44+
],
45+
"targetNames": [
46+
"fooUser2Id"
47+
]
48+
}
49+
},
50+
"createdAt": {
51+
"name": "createdAt",
52+
"isArray": false,
53+
"type": "AWSDateTime",
54+
"isRequired": false,
55+
"attributes": [],
56+
"isReadOnly": true
57+
},
58+
"updatedAt": {
59+
"name": "updatedAt",
60+
"isArray": false,
61+
"type": "AWSDateTime",
62+
"isRequired": false,
63+
"attributes": [],
64+
"isReadOnly": true
65+
},
66+
"fooUser1Id": {
67+
"name": "fooUser1Id",
68+
"isArray": false,
69+
"type": "ID",
70+
"isRequired": false,
71+
"attributes": []
72+
},
73+
"fooUser2Id": {
74+
"name": "fooUser2Id",
75+
"isArray": false,
76+
"type": "ID",
77+
"isRequired": false,
78+
"attributes": []
79+
}
80+
},
81+
"syncable": true,
82+
"pluralName": "Foos",
83+
"attributes": [
84+
{
85+
"type": "model",
86+
"properties": {}
87+
},
88+
{
89+
"type": "auth",
90+
"properties": {
91+
"rules": [
92+
{
93+
"allow": "public",
94+
"operations": [
95+
"create",
96+
"update",
97+
"delete",
98+
"read"
99+
]
100+
}
101+
]
102+
}
103+
}
104+
],
105+
"primaryKeyInfo": {
106+
"isCustomPrimaryKey": false,
107+
"primaryKeyFieldName": "id",
108+
"sortKeyFieldNames": []
109+
}
110+
},
111+
"User": {
112+
"name": "User",
113+
"fields": {
114+
"id": {
115+
"name": "id",
116+
"isArray": false,
117+
"type": "ID",
118+
"isRequired": true,
119+
"attributes": []
120+
},
121+
"name": {
122+
"name": "name",
123+
"isArray": false,
124+
"type": "String",
125+
"isRequired": true,
126+
"attributes": []
127+
},
128+
"image": {
129+
"name": "image",
130+
"isArray": false,
131+
"type": "String",
132+
"isRequired": false,
133+
"attributes": []
134+
},
135+
"bio": {
136+
"name": "bio",
137+
"isArray": false,
138+
"type": "String",
139+
"isRequired": true,
140+
"attributes": []
141+
},
142+
"gender": {
143+
"name": "gender",
144+
"isArray": false,
145+
"type": {
146+
"enum": "Genders"
147+
},
148+
"isRequired": true,
149+
"attributes": []
150+
},
151+
"lookingFor": {
152+
"name": "lookingFor",
153+
"isArray": false,
154+
"type": {
155+
"enum": "Genders"
156+
},
157+
"isRequired": true,
158+
"attributes": []
159+
},
160+
"sub": {
161+
"name": "sub",
162+
"isArray": false,
163+
"type": "String",
164+
"isRequired": true,
165+
"attributes": []
166+
},
167+
"createdAt": {
168+
"name": "createdAt",
169+
"isArray": false,
170+
"type": "AWSDateTime",
171+
"isRequired": false,
172+
"attributes": [],
173+
"isReadOnly": true
174+
},
175+
"updatedAt": {
176+
"name": "updatedAt",
177+
"isArray": false,
178+
"type": "AWSDateTime",
179+
"isRequired": false,
180+
"attributes": [],
181+
"isReadOnly": true
182+
}
183+
},
184+
"syncable": true,
185+
"pluralName": "Users",
186+
"attributes": [
187+
{
188+
"type": "model",
189+
"properties": {}
190+
},
191+
{
192+
"type": "auth",
193+
"properties": {
194+
"rules": [
195+
{
196+
"allow": "public",
197+
"operations": [
198+
"create",
199+
"update",
200+
"delete",
201+
"read"
202+
]
203+
}
204+
]
205+
}
206+
}
207+
],
208+
"primaryKeyInfo": {
209+
"isCustomPrimaryKey": false,
210+
"primaryKeyFieldName": "id",
211+
"sortKeyFieldNames": []
212+
}
213+
}
214+
},
215+
"enums": {
216+
"Genders": {
217+
"name": "Genders",
218+
"values": [
219+
"MALE",
220+
"FEMALE",
221+
"OTHER"
222+
]
223+
}
224+
},
225+
"nonModels": {}
226+
}

packages/codegen-ui/lib/__tests__/generate-form-definition/helpers/model-fields-configs.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
limitations under the License.
1515
*/
1616

17+
import { ModelIntrospectionSchema } from '@aws-amplify/appsync-modelgen-plugin';
1718
import {
1819
mapModelFieldsConfigs,
1920
getFieldTypeMapKey,
2021
getFieldConfigFromModelField,
2122
} from '../../../generate-form-definition/helpers';
23+
import { getGenericFromDataStore } from '../../../generic-from-datastore';
2224
import { FormDefinition, GenericDataField, GenericDataSchema } from '../../../types';
2325
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';
26+
import schemaWith2HasOne from '../../__utils__/introspection-schemas/schema-with-2-has-one.json';
2427

2528
describe('mapModelFieldsConfigs', () => {
2629
it('should map to elementMatrix and add to modelFieldsConfigs', () => {
@@ -100,6 +103,74 @@ describe('mapModelFieldsConfigs', () => {
100103
expect(() => mapModelFieldsConfigs({ dataTypeName: 'Cat', formDefinition, dataSchema })).toThrow();
101104
});
102105

106+
it('should properly map all hasOne relationships if multiple are provided', () => {
107+
const genericDataSchema = getGenericFromDataStore(schemaWith2HasOne as unknown as ModelIntrospectionSchema);
108+
109+
const modelFields = mapModelFieldsConfigs({
110+
dataTypeName: 'Foo',
111+
formDefinition: {
112+
form: {
113+
layoutStyle: { horizontalGap: { value: '12' }, verticalGap: { value: '12' }, outerPadding: { value: '5' } },
114+
labelDecorator: 'none',
115+
},
116+
elements: {},
117+
buttons: {
118+
buttonConfigs: {},
119+
position: '',
120+
buttonMatrix: [],
121+
},
122+
elementMatrix: [],
123+
},
124+
dataSchema: genericDataSchema,
125+
formActionType: 'create',
126+
featureFlags: { isRelationshipSupported: true },
127+
});
128+
129+
const userValueMappings = {
130+
bindingProperties: {
131+
User: {
132+
bindingProperties: {
133+
model: 'User',
134+
},
135+
type: 'Data',
136+
},
137+
},
138+
values: [
139+
{
140+
displayValue: {
141+
concat: [
142+
{
143+
bindingProperties: {
144+
field: 'name',
145+
property: 'User',
146+
},
147+
},
148+
{
149+
value: ' - ',
150+
},
151+
{
152+
bindingProperties: {
153+
field: 'id',
154+
property: 'User',
155+
},
156+
},
157+
],
158+
isDefault: true,
159+
},
160+
value: {
161+
bindingProperties: {
162+
field: 'id',
163+
property: 'User',
164+
},
165+
},
166+
},
167+
],
168+
};
169+
170+
expect(modelFields.fooUser1Id.inputType?.valueMappings).toEqual(userValueMappings);
171+
expect(modelFields.fooUser2Id.inputType?.valueMappings).toEqual(userValueMappings);
172+
});
173+
103174
it('should generate config from id field but not add it to matrix', () => {
104175
const formDefinition: FormDefinition = getBasicFormDefinition();
105176

0 commit comments

Comments
 (0)