Skip to content

Commit

Permalink
fix: collection components should not directly query for child collec…
Browse files Browse the repository at this point in the history
…tion data
  • Loading branch information
awinberg-aws committed Aug 18, 2023
1 parent 3674ebd commit 0a83efb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -946,15 +946,8 @@ export default function AuthorProfileCollection(
query: listAuthors,
variables,
})
).data.listAuthors;
var loaded = await Promise.all(
result.items.map(async (item) => {
return {
...item,
};
})
);
newCache.push(...loaded);
).data.listAuthors.items;
newCache.push(...result);
newNext = result.nextToken;
}
const cacheSlice = newCache.slice((page - 1) * pageSize, page * pageSize);
Expand Down Expand Up @@ -1128,15 +1121,8 @@ export default function CollectionOfCustomButtons(
query: listUsers,
variables,
})
).data.listUsers;
var loaded = await Promise.all(
result.items.map(async (item) => {
return {
...item,
};
})
);
newCache.push(...loaded);
).data.listUsers.items;
newCache.push(...result);
newNext = result.nextToken;
}
const cacheSlice = newCache.slice((page - 1) * pageSize, page * pageSize);
Expand Down Expand Up @@ -1301,15 +1287,8 @@ export default function ListingCardCollection(
query: listUntitledModels,
variables,
})
).data.listUntitledModels;
var loaded = await Promise.all(
result.items.map(async (item) => {
return {
...item,
};
})
);
newCache.push(...loaded);
).data.listUntitledModels.items;
newCache.push(...result);
newNext = result.nextToken;
}
const cacheSlice = newCache.slice((page - 1) * pageSize, page * pageSize);
Expand Down Expand Up @@ -1573,29 +1552,8 @@ export default function AuthorProfileCollection(
query: listAuthors,
variables,
})
).data.listAuthors;
var loaded = await Promise.all(
result.items.map(async (item) => {
const Books = (
await API.graphql({
query: booksByAuthorID,
variables: { authorID: item.id },
})
).data.booksByAuthorID.items;
const Publishers = (
await API.graphql({
query: publishersByAuthorID,
variables: { authorID: item.id },
})
).data.publishersByAuthorID.items;
return {
...item,
Books,
Publisher,
};
})
);
newCache.push(...loaded);
).data.listAuthors.items;
newCache.push(...result);
newNext = result.nextToken;
}
const cacheSlice = newCache.slice((page - 1) * pageSize, page * pageSize);
Expand Down
117 changes: 26 additions & 91 deletions packages/codegen-ui-react/lib/react-studio-template-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2064,107 +2064,42 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
undefined,
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createParenthesizedExpression(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('API'),
factory.createIdentifier('graphql'),
),
undefined,
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('query'),
factory.createIdentifier(modelQuery),
),
factory.createShorthandPropertyAssignment(
factory.createIdentifier('variables'),
undefined,
),
],
true,
),
],
),
),
),
factory.createIdentifier('data'),
),
factory.createIdentifier(modelQuery),
),
),
],
ts.NodeFlags.Const,
),
),
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier('loaded'),
undefined,
undefined,
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('Promise'),
factory.createIdentifier('all'),
),
undefined,
[
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('result'),
factory.createIdentifier('items'),
),
factory.createIdentifier('map'),
),
undefined,
[
factory.createArrowFunction(
[factory.createToken(ts.SyntaxKind.AsyncKeyword)],
factory.createParenthesizedExpression(
factory.createAwaitExpression(
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier('API'),
factory.createIdentifier('graphql'),
),
undefined,
[
factory.createParameterDeclaration(
undefined,
undefined,
undefined,
factory.createIdentifier('item'),
undefined,
undefined,
undefined,
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('query'),
factory.createIdentifier(modelQuery),
),
factory.createShorthandPropertyAssignment(
factory.createIdentifier('variables'),
undefined,
),
],
true,
),
],
undefined,
factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
factory.createBlock(
[
...loadedFieldStatements,
factory.createReturnStatement(
factory.createObjectLiteralExpression(
[
factory.createSpreadAssignment(factory.createIdentifier('item')),
...loadedFields,
],
true,
),
),
],
true,
),
),
],
),
),
],
factory.createIdentifier('data'),
),
factory.createIdentifier(modelQuery),
),
factory.createIdentifier('items'),
),
),
],
ts.NodeFlags.AwaitContext,
ts.NodeFlags.Const,
),
),
factory.createExpressionStatement(
Expand All @@ -2174,7 +2109,7 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
factory.createIdentifier('push'),
),
undefined,
[factory.createSpreadElement(factory.createIdentifier('loaded'))],
[factory.createSpreadElement(factory.createIdentifier('result'))],
),
),
factory.createExpressionStatement(
Expand Down

0 comments on commit 0a83efb

Please sign in to comment.