Skip to content

Commit

Permalink
fix(codegen-ui): fix multiple has one relationships (#1005)
Browse files Browse the repository at this point in the history
* fix(codegen-ui): fix multiple has one relationships

* chore(deps): run in band

---------

Co-authored-by: Christopher Woolum <[email protected]>
  • Loading branch information
cwoolum and Christopher Woolum authored May 2, 2023
1 parent 8db1582 commit 96b7b9d
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/codegen-ui-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "jest --ci --maxWorkers=30%",
"test:ci": "jest --ci -i",
"test:update": "jest --updateSnapshot",
"build": "tsc -p tsconfig.build.json",
"build:watch": "npm run build -- --watch"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
{
"version": "1",
"models": {
"Foo": {
"name": "Foo",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"User1": {
"name": "User1",
"isArray": false,
"type": {
"model": "User"
},
"isRequired": false,
"attributes": [],
"association": {
"connectionType": "HAS_ONE",
"associatedWith": [
"id"
],
"targetNames": [
"fooUser1Id"
]
}
},
"User2": {
"name": "User2",
"isArray": false,
"type": {
"model": "User"
},
"isRequired": false,
"attributes": [],
"association": {
"connectionType": "HAS_ONE",
"associatedWith": [
"id"
],
"targetNames": [
"fooUser2Id"
]
}
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"fooUser1Id": {
"name": "fooUser1Id",
"isArray": false,
"type": "ID",
"isRequired": false,
"attributes": []
},
"fooUser2Id": {
"name": "fooUser2Id",
"isArray": false,
"type": "ID",
"isRequired": false,
"attributes": []
}
},
"syncable": true,
"pluralName": "Foos",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "public",
"operations": [
"create",
"update",
"delete",
"read"
]
}
]
}
}
],
"primaryKeyInfo": {
"isCustomPrimaryKey": false,
"primaryKeyFieldName": "id",
"sortKeyFieldNames": []
}
},
"User": {
"name": "User",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"name": {
"name": "name",
"isArray": false,
"type": "String",
"isRequired": true,
"attributes": []
},
"image": {
"name": "image",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"bio": {
"name": "bio",
"isArray": false,
"type": "String",
"isRequired": true,
"attributes": []
},
"gender": {
"name": "gender",
"isArray": false,
"type": {
"enum": "Genders"
},
"isRequired": true,
"attributes": []
},
"lookingFor": {
"name": "lookingFor",
"isArray": false,
"type": {
"enum": "Genders"
},
"isRequired": true,
"attributes": []
},
"sub": {
"name": "sub",
"isArray": false,
"type": "String",
"isRequired": true,
"attributes": []
},
"createdAt": {
"name": "createdAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
},
"updatedAt": {
"name": "updatedAt",
"isArray": false,
"type": "AWSDateTime",
"isRequired": false,
"attributes": [],
"isReadOnly": true
}
},
"syncable": true,
"pluralName": "Users",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "public",
"operations": [
"create",
"update",
"delete",
"read"
]
}
]
}
}
],
"primaryKeyInfo": {
"isCustomPrimaryKey": false,
"primaryKeyFieldName": "id",
"sortKeyFieldNames": []
}
}
},
"enums": {
"Genders": {
"name": "Genders",
"values": [
"MALE",
"FEMALE",
"OTHER"
]
}
},
"nonModels": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
limitations under the License.
*/

import { ModelIntrospectionSchema } from '@aws-amplify/appsync-modelgen-plugin';
import {
mapModelFieldsConfigs,
getFieldTypeMapKey,
getFieldConfigFromModelField,
} from '../../../generate-form-definition/helpers';
import { getGenericFromDataStore } from '../../../generic-from-datastore';
import { FormDefinition, GenericDataField, GenericDataSchema } from '../../../types';
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';
import schemaWith2HasOne from '../../__utils__/introspection-schemas/schema-with-2-has-one.json';

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

it('should properly map all hasOne relationships if multiple are provided', () => {
const genericDataSchema = getGenericFromDataStore(schemaWith2HasOne as unknown as ModelIntrospectionSchema);

const modelFields = mapModelFieldsConfigs({
dataTypeName: 'Foo',
formDefinition: {
form: {
layoutStyle: { horizontalGap: { value: '12' }, verticalGap: { value: '12' }, outerPadding: { value: '5' } },
labelDecorator: 'none',
},
elements: {},
buttons: {
buttonConfigs: {},
position: '',
buttonMatrix: [],
},
elementMatrix: [],
},
dataSchema: genericDataSchema,
formActionType: 'create',
featureFlags: { isRelationshipSupported: true },
});

const userValueMappings = {
bindingProperties: {
User: {
bindingProperties: {
model: 'User',
},
type: 'Data',
},
},
values: [
{
displayValue: {
concat: [
{
bindingProperties: {
field: 'name',
property: 'User',
},
},
{
value: ' - ',
},
{
bindingProperties: {
field: 'id',
property: 'User',
},
},
],
isDefault: true,
},
value: {
bindingProperties: {
field: 'id',
property: 'User',
},
},
},
],
};

expect(modelFields.fooUser1Id.inputType?.valueMappings).toEqual(userValueMappings);
expect(modelFields.fooUser2Id.inputType?.valueMappings).toEqual(userValueMappings);
});

it('should generate config from id field but not add it to matrix', () => {
const formDefinition: FormDefinition = getBasicFormDefinition();

Expand Down
Loading

0 comments on commit 96b7b9d

Please sign in to comment.