Skip to content

Commit

Permalink
allow looking up pinboards (workflow stubs) by path, via `workflow-br…
Browse files Browse the repository at this point in the history
…idge-lambda`, utilising guardian/workflow#1119
  • Loading branch information
twrichards committed Oct 8, 2024
1 parent 47b6192 commit 0c32110
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
5 changes: 5 additions & 0 deletions client/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export const gqlGetPinboardsByIds = gql`
getPinboardsByIds(ids: $ids) { ${pinboardReturnFields} }
}
`;
export const gqlGetPinboardsByPaths = gql`
query MyQuery($paths: [String!]!) {
getPinboardsByPaths(paths: $paths) { ${pinboardReturnFields} }
}
`;
export const gqlGetGroupPinboardIds = gql`
query MyQuery {
getGroupPinboardIds {
Expand Down
1 change: 1 addition & 0 deletions shared/graphql/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const QUERIES = {
workflow: [
"listPinboards",
"getPinboardsByIds",
"getPinboardsByPaths",
"getPinboardByComposerId",
] as const,
grid: ["getGridSearchSummary", "asGridPayload"] as const,
Expand Down
1 change: 1 addition & 0 deletions shared/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Query {
# workflow-bridge-lambda queries
listPinboards(searchText: String): [WorkflowStub]
getPinboardsByIds(ids: [String!]!): [WorkflowStub]
getPinboardsByPaths(paths: [String!]!): [WorkflowStub]
getPinboardByComposerId(composerId: String!): WorkflowStub
# grid-bridge-lambda queries
getGridSearchSummary(apiUrl: String!): GridSearchSummary
Expand Down
37 changes: 34 additions & 3 deletions workflow-bridge-lambda/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ const WORKFLOW_DATASTORE_API_URL = `http://${getEnvironmentVariableOrThrow(
"workflowDnsName"
)}/api`;

exports.handler = async (event: {
arguments?: { composerId?: string; ids?: string[]; searchText?: string };
}) => {
interface Arguments {
composerId?: string;
ids?: string[];
searchText?: string;
paths?: string[];
}

exports.handler = async (event: { arguments?: Arguments }) => {
if (event.arguments?.composerId) {
return await getPinboardById("content")(event.arguments.composerId);
}
if (event.arguments?.ids) {
// TODO do this in single call using new endpoint added in https://github.com/guardian/workflow/pull/1119
return await Promise.all(
event.arguments.ids
.map(parseFloat) // workflow IDs are Longs
.map(getPinboardById("stubs"))
);
}
if (event.arguments?.paths !== undefined) {
return await getPinboardsByPaths(event.arguments?.paths);
}
if (event.arguments?.searchText !== undefined) {
return await getAllPinboards(event.arguments?.searchText);
}
Expand Down Expand Up @@ -62,6 +71,28 @@ const getPinboardById =
return { ...data.externalData, ...data };
};

const getPinboardsByPaths = async (identifiers: number[] | string[]) => {
const stubsResponse = await fetch(
`${WORKFLOW_DATASTORE_API_URL}/stubsBy$Path?fieldFilter=id,path`,
{
method: "POST",
body: JSON.stringify(identifiers),
}
);

if (!stubsResponse.ok) {
throw Error(`${stubsResponse.status} ${await stubsResponse.text()}`);
}

const stubsResponseBody = (await stubsResponse.json()) as {
data: {
content: { [status: string]: { id: string; path: string } };
};
};

return Object.values(stubsResponseBody.data.content);
};

const getAllPinboardIds = async ({ isTrashed }: { isTrashed: boolean }) => {
const stubsResponse = await fetch(
`${WORKFLOW_DATASTORE_API_URL}/stubs?fieldFilter=id&trashed=${isTrashed}`
Expand Down

0 comments on commit 0c32110

Please sign in to comment.