Skip to content

Commit

Permalink
add optional maybeAspectRatioFilter arg to listItems Query so the…
Browse files Browse the repository at this point in the history
… `FrontsPinboardArticleButton` can request matching crops without having to get ALL the items for that pinboard and filter out the majority that it doesn't need
  • Loading branch information
twrichards committed Nov 23, 2024
1 parent 21fe195 commit bb03b86
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 48 deletions.
48 changes: 24 additions & 24 deletions cdk/lib/__snapshots__/stack.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,7 @@ Object {
type Query {
# database-bridge-lambda queries
listItems(pinboardId: String!): [Item]
listItems(pinboardId: String!, maybeAspectRatioFilter: String): [Item]
listLastItemSeenByUsers(pinboardId: String!): [LastItemSeenByUser]
getMyUser: MyUser
searchMentionableUsers(prefix: String!): UsersAndGroups
Expand Down Expand Up @@ -2680,7 +2680,7 @@ type PinboardIdWithItemCounts {
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "addManuallyOpenedPinboardIds",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2701,7 +2701,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "changeFeatureFlag",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2722,7 +2722,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "claimItem",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2743,7 +2743,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "createItem",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2764,7 +2764,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "deleteItem",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2785,7 +2785,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "editItem",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2806,7 +2806,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "removeManuallyOpenedPinboardIds",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2827,7 +2827,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "seenItem",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2848,7 +2848,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "setWebPushSubscriptionForUser",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2869,7 +2869,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "visitTourStep",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Mutation",
},
Expand All @@ -2890,7 +2890,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "getGroupPinboardIds",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -2911,7 +2911,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "getItemCounts",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -2932,7 +2932,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "getMyUser",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -2953,7 +2953,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "getUsers",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -2974,7 +2974,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "listItems",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -2995,7 +2995,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "listLastItemSeenByUsers",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -3016,7 +3016,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "database_bridge_lambda_ds",
"FieldName": "searchMentionableUsers",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand Down Expand Up @@ -3145,7 +3145,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "grid_bridge_lambda_ds",
"FieldName": "asGridPayload",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -3166,7 +3166,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "grid_bridge_lambda_ds",
"FieldName": "getGridSearchSummary",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand Down Expand Up @@ -3295,7 +3295,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "workflow_bridge_lambda_ds",
"FieldName": "getPinboardByComposerId",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -3316,7 +3316,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "workflow_bridge_lambda_ds",
"FieldName": "getPinboardsByIds",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -3337,7 +3337,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "workflow_bridge_lambda_ds",
"FieldName": "getPinboardsByPaths",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand All @@ -3358,7 +3358,7 @@ $util.toJson($ctx.result)",
"DataSourceName": "workflow_bridge_lambda_ds",
"FieldName": "listPinboards",
"Kind": "UNIT",
"ResponseMappingTemplate": "## schema checksum : 707f5a6af9682053bcaf47d47c4f26d0
"ResponseMappingTemplate": "## schema checksum : da5ab4d1435d8fb629c068ee4933d29c
$util.toJson($ctx.result)",
"TypeName": "Query",
},
Expand Down
4 changes: 2 additions & 2 deletions client/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ const itemReturnFields = `

// TODO: consider updating the resolver (cdk/stack.ts) to use a Query with a secondary index (if performance degrades when we have lots of items)
export const gqlGetInitialItems = gql`
query MyQuery($pinboardId: String!) {
listItems(pinboardId: $pinboardId) {
query MyQuery($pinboardId: String!, $maybeAspectRatioFilter: String) {
listItems(pinboardId: $pinboardId, maybeAspectRatioFilter: $maybeAspectRatioFilter) {
${itemReturnFields}
}
}
Expand Down
26 changes: 6 additions & 20 deletions client/src/fronts/frontsPinboardArticleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,22 @@ export const FrontsPinboardArticleButton = ({
Array<[PayloadWithThumbnail, Item]>
>([]);

const [getItems, { loading }] = useLazyQuery(
gqlGetInitialItems //TODO consider creating new query,
);
const [getItems, { loading }] = useLazyQuery(gqlGetInitialItems);

useEffect(() => {
if (isPinboardData(maybePinboardData) && withDraggableThumbsOfRatio) {
getItems({
variables: {
pinboardId: maybePinboardData.id,
maybeAspectRatioFilter: withDraggableThumbsOfRatio,
},
}).then(({ data }) => {
data?.listItems &&
setCropsAtRequiredRatio(
data.listItems.reduce(
(acc: Array<[PayloadWithThumbnail, Item]>, item: Item) => {
const parsedPayload =
item.payload &&
(JSON.parse(item.payload) as PayloadWithThumbnail);
if (
item.type === "grid-crop" &&
parsedPayload &&
parsedPayload.aspectRatio === withDraggableThumbsOfRatio
) {
return [...acc, [parsedPayload, item]];
} else {
return acc;
}
},
[]
)
data.listItems.map((item: Item) => [
JSON.parse(item.payload!),
item,
])
);
});
}
Expand Down
7 changes: 6 additions & 1 deletion database-bridge-lambda/src/sql/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ export const deleteItem = async (

export const listItems = (
sql: Sql,
args: { pinboardId: string },
args: { pinboardId: string; maybeAspectRatioFilter?: string },
userEmail: string
) => sql`
SELECT ${fragmentItemFields(sql, userEmail)}
FROM "Item"
WHERE "pinboardId" = ${args.pinboardId}
AND "isArchived" = false
${
args.maybeAspectRatioFilter
? sql`AND "type" = 'grid-crop' AND "payload" ->> 'aspectRatio' = ${args.maybeAspectRatioFilter}`
: sql``
}
`;

export const claimItem = (
Expand Down
2 changes: 1 addition & 1 deletion shared/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ schema {

type Query {
# database-bridge-lambda queries
listItems(pinboardId: String!): [Item]
listItems(pinboardId: String!, maybeAspectRatioFilter: String): [Item]
listLastItemSeenByUsers(pinboardId: String!): [LastItemSeenByUser]
getMyUser: MyUser
searchMentionableUsers(prefix: String!): UsersAndGroups
Expand Down

0 comments on commit bb03b86

Please sign in to comment.