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

feat: [GraphQL] update form with relationship #1030

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,77 @@ describe('amplify form renderer tests', () => {
expect(declaration).toMatchSnapshot();
});

it('should generate an update form with hasMany relationship', () => {
const { componentText, declaration } = generateWithAmplifyFormRenderer(
'forms/relationships/update-comment',
'datastore/relationships/has-many-comment',
{ ...defaultCLIRenderConfig, ...rendererConfigWithGraphQL },
{ isNonModelSupported: true, isRelationshipSupported: true },
);

// check for import statement for graphql operation
expect(componentText).not.toContain('DataStore');

expect(componentText).toContain('await API.graphql({');
expect(componentText).toContain('query: updateComment');
expect(componentText).toContain('const postRecords = await API.graphql({');

expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
});

it('should generate an update form with many to many relationship', () => {
const { componentText, declaration } = generateWithAmplifyFormRenderer(
'forms/relationships/update-class',
'datastore/relationships/many-to-many-class',
{ ...defaultCLIRenderConfig, ...rendererConfigWithGraphQL },
{ isNonModelSupported: true, isRelationshipSupported: true },
);

// check for import statement for graphql operation
expect(componentText).not.toContain('DataStore');

expect(componentText).toContain('await API.graphql({');
expect(componentText).toContain('createStudentClass');
expect(componentText).toContain('deleteStudentClass');
expect(componentText).toContain('updateClass');

expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
});

it('should generate an upgrade form with multiple relationship & cpk', () => {
const { componentText, declaration } = generateWithAmplifyFormRenderer(
'forms/cpk-teacher-datastore-update',
'datastore/cpk-relationships',
{ ...defaultCLIRenderConfig, ...rendererConfigWithGraphQL },
{ isNonModelSupported: true, isRelationshipSupported: true },
);

// check for import statement for graphql operation
expect(componentText).not.toContain('DataStore');

// hasOne
expect(componentText).toContain('specialTeacherId: specialTeacherIdProp');
expect(componentText).toContain('query: getCPKTeacher,');
expect(componentText).toContain('Student: (r) => r?.specialStudentId');
expect(componentText).toContain('JSON.stringify({ specialStudentId: r?.specialStudentId })');

// manyToMany
expect(componentText).toContain('const count = cPKClassesMap.get(getIDValue.CPKClasses?.(r))');
expect(componentText).toContain('cPKClassesMap.set(getIDValue.CPKClasses?.(r), newCount)');
expect(componentText).toContain('const count = linkedCPKClassesMap.get(getIDValue.CPKClasses?.(r))');
expect(componentText).toContain('linkedCPKClassesMap.set(getIDValue.CPKClasses?.(r), newCount)');
expect(componentText).toContain('cpkTeacher: cPKTeacherRecord');

// hasMany
expect(componentText).toContain('cPKProjectsSet.add(getIDValue.CPKProjects?.(r)');
expect(componentText).toContain('linkedCPKProjectsSet.add(getIDValue.CPKProjects?.(r))');

expect(componentText).toMatchSnapshot();
expect(declaration).toMatchSnapshot();
});

it('should generate a create form with multiple hasOne relationships', () => {
const { componentText, declaration } = generateWithAmplifyFormRenderer(
'forms/book-datastore-relationship-multiple',
Expand Down
607 changes: 358 additions & 249 deletions packages/codegen-ui-react/lib/forms/form-renderer-helper/relationship.ts

Large diffs are not rendered by default.

55 changes: 33 additions & 22 deletions packages/codegen-ui-react/lib/utils/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,53 @@ export const getGraphqlCallExpression = (
model: string,
importCollection: ImportCollection,
inputs?: ObjectLiteralElementLike[],
filters?: ObjectLiteralElementLike[],
): CallExpression => {
const query = getGraphqlQueryForModel(action, model);
const graphqlVariables: ObjectLiteralElementLike[] = [];

importCollection.addMappedImport(ImportValue.API);

if (inputs) {
graphqlVariables.push(
factory.createPropertyAssignment(
factory.createIdentifier('input'),
factory.createObjectLiteralExpression(inputs, true),
),
);
}

if (action === ActionType.LIST || action === ActionType.GET) {
importCollection.addGraphqlQueryImport(query);
// filter applies to list
if (filters) {
graphqlVariables.push(
factory.createPropertyAssignment(
factory.createIdentifier('filter'),
factory.createObjectLiteralExpression(filters, true),
),
);
}
} else {
importCollection.addGraphqlMutationImport(query);
}
importCollection.addModelImport(model);

const graphqlOptions: ObjectLiteralElementLike[] = [
factory.createPropertyAssignment(factory.createIdentifier('query'), factory.createIdentifier(query)),
];

if (graphqlVariables.length > 0) {
graphqlOptions.push(
factory.createPropertyAssignment(
factory.createIdentifier('variables'),
factory.createObjectLiteralExpression(graphqlVariables),
),
);
}
return factory.createCallExpression(
factory.createPropertyAccessExpression(factory.createIdentifier('API'), factory.createIdentifier('graphql')),
undefined,
[
factory.createObjectLiteralExpression(
inputs
? [
factory.createPropertyAssignment(factory.createIdentifier('query'), factory.createIdentifier(query)),
factory.createPropertyAssignment(
factory.createIdentifier('variables'),
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('input'),
factory.createObjectLiteralExpression(inputs, true),
),
],
true,
),
),
]
: [factory.createPropertyAssignment(factory.createIdentifier('query'), factory.createIdentifier(query))],
true,
),
],
[factory.createObjectLiteralExpression(graphqlOptions, true)],
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"models": {
"Comment": {
"name": "Comment",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"content": {
"name": "content",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"postID": {
"name": "postID",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"Post": {
"name": "Post",
"isArray": false,
"type": {
"model": "Post"
},
"isRequired": false,
"attributes": [],
"association": {
"connectionType": "BELONGS_TO",
"targetName": "postCommentsId"
}
},
"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": "Comments",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "key",
"properties": {
"name": "byPost",
"fields": [
"postID"
]
}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "public",
"operations": [
"read"
]
},
{
"provider": "userPools",
"ownerField": "owner",
"allow": "owner",
"identityClaim": "cognito:username",
"operations": [
"create",
"update",
"delete",
"read"
]
}
]
}
}
]
},
"Post": {
"name": "Post",
"fields": {
"id": {
"name": "id",
"isArray": false,
"type": "ID",
"isRequired": true,
"attributes": []
},
"title": {
"name": "title",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"content": {
"name": "content",
"isArray": false,
"type": "String",
"isRequired": false,
"attributes": []
},
"Comments": {
"name": "Comments",
"isArray": true,
"type": {
"model": "Comment"
},
"isRequired": false,
"attributes": [],
"isArrayNullable": true,
"association": {
"connectionType": "HAS_MANY",
"associatedWith": [
"postID"
]
}
},
"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": "Posts",
"attributes": [
{
"type": "model",
"properties": {}
},
{
"type": "auth",
"properties": {
"rules": [
{
"allow": "public",
"operations": [
"read"
]
},
{
"provider": "userPools",
"ownerField": "owner",
"allow": "owner",
"identityClaim": "cognito:username",
"operations": [
"create",
"update",
"delete",
"read"
]
}
]
}
}
]
}
},
"enums": {},
"nonModels": {},
"codegenVersion": "3.3.6",
"version": "0f95fb2e55520e7f5b5ee65562b0bb59"
}
Loading