Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(codegen-ui): fix multiple has one relationships #1005

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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