Paginate access review source loader in n8n node
The loader fetched only the first 500 sources with no pageInfo/cursor follow-up, so organizations with more than 500 scoped sources could not select sources past the first page when creating or updating a campaign. Loop over pages using pageInfo.endCursor until exhausted. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
@@ -22,16 +22,20 @@ import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n
|
|||||||
import { proboApiRequest } from '../../GenericFunctions';
|
import { proboApiRequest } from '../../GenericFunctions';
|
||||||
|
|
||||||
const organizationSourcesQuery = `
|
const organizationSourcesQuery = `
|
||||||
query AccessReviewSources($organizationId: ID!) {
|
query AccessReviewSources($organizationId: ID!, $after: CursorKey) {
|
||||||
organization: node(id: $organizationId) {
|
organization: node(id: $organizationId) {
|
||||||
... on Organization {
|
... on Organization {
|
||||||
accessReviewSources(first: 500) {
|
accessReviewSources(first: 100, after: $after) {
|
||||||
edges {
|
edges {
|
||||||
node {
|
node {
|
||||||
id
|
id
|
||||||
name
|
name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pageInfo {
|
||||||
|
hasNextPage
|
||||||
|
endCursor
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,10 +55,9 @@ const campaignOrganizationQuery = `
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
function mapSources(responseData: IDataObject): INodePropertyOptions[] {
|
function mapSources(responseData: IDataObject): INodePropertyOptions[] {
|
||||||
const data = responseData.data as IDataObject | undefined;
|
const edges = getAccessReviewSourcesConnection(responseData)?.edges as
|
||||||
const organization = data?.organization as IDataObject | undefined;
|
| Array<{ node: IDataObject }>
|
||||||
const accessReviewSources = organization?.accessReviewSources as IDataObject | undefined;
|
| undefined;
|
||||||
const edges = accessReviewSources?.edges as Array<{ node: IDataObject }> | undefined;
|
|
||||||
|
|
||||||
return (edges ?? [])
|
return (edges ?? [])
|
||||||
.map((edge) => edge.node)
|
.map((edge) => edge.node)
|
||||||
@@ -65,6 +68,24 @@ function mapSources(responseData: IDataObject): INodePropertyOptions[] {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getAccessReviewSourcesConnection(responseData: IDataObject): IDataObject | undefined {
|
||||||
|
const data = responseData.data as IDataObject | undefined;
|
||||||
|
const organization = data?.organization as IDataObject | undefined;
|
||||||
|
|
||||||
|
return organization?.accessReviewSources as IDataObject | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPageInfo(responseData: IDataObject): { hasNextPage: boolean; endCursor?: string } {
|
||||||
|
const pageInfo = getAccessReviewSourcesConnection(responseData)?.pageInfo as
|
||||||
|
| IDataObject
|
||||||
|
| undefined;
|
||||||
|
|
||||||
|
return {
|
||||||
|
hasNextPage: pageInfo?.hasNextPage === true,
|
||||||
|
endCursor: typeof pageInfo?.endCursor === 'string' ? pageInfo.endCursor : undefined,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
async function resolveOrganizationId(
|
async function resolveOrganizationId(
|
||||||
this: ILoadOptionsFunctions,
|
this: ILoadOptionsFunctions,
|
||||||
): Promise<string | undefined> {
|
): Promise<string | undefined> {
|
||||||
@@ -96,9 +117,20 @@ export async function getAccessReviewSources(
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseData = await proboApiRequest.call(this, organizationSourcesQuery, {
|
const options: INodePropertyOptions[] = [];
|
||||||
organizationId,
|
let after: string | undefined;
|
||||||
});
|
|
||||||
|
|
||||||
return mapSources(responseData);
|
do {
|
||||||
|
const responseData = await proboApiRequest.call(this, organizationSourcesQuery, {
|
||||||
|
organizationId,
|
||||||
|
after,
|
||||||
|
});
|
||||||
|
|
||||||
|
options.push(...mapSources(responseData));
|
||||||
|
|
||||||
|
const pageInfo = getPageInfo(responseData);
|
||||||
|
after = pageInfo.hasNextPage ? pageInfo.endCursor : undefined;
|
||||||
|
} while (after);
|
||||||
|
|
||||||
|
return options;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user