diff --git a/client/gql.ts b/client/gql.ts index cdf390b5..cbad0600 100644 --- a/client/gql.ts +++ b/client/gql.ts @@ -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 { diff --git a/shared/graphql/operations.ts b/shared/graphql/operations.ts index 5fcd874e..9012f3bf 100644 --- a/shared/graphql/operations.ts +++ b/shared/graphql/operations.ts @@ -14,6 +14,7 @@ export const QUERIES = { workflow: [ "listPinboards", "getPinboardsByIds", + "getPinboardsByPaths", "getPinboardByComposerId", ] as const, grid: ["getGridSearchSummary", "asGridPayload"] as const, diff --git a/shared/graphql/schema.graphql b/shared/graphql/schema.graphql index 1989d872..3c7c45b8 100644 --- a/shared/graphql/schema.graphql +++ b/shared/graphql/schema.graphql @@ -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 diff --git a/workflow-bridge-lambda/src/index.ts b/workflow-bridge-lambda/src/index.ts index ab60ac79..7efd5c79 100644 --- a/workflow-bridge-lambda/src/index.ts +++ b/workflow-bridge-lambda/src/index.ts @@ -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); } @@ -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}`