Fix n8n use legacy API

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2026-02-03 07:56:46 +01:00
parent 31f2cde8d0
commit e2e695205a
2 changed files with 92 additions and 19 deletions

View File

@@ -7,9 +7,16 @@ import type {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow'; import { NodeApiError } from 'n8n-workflow';
export async function proboApiRequest( type ApiRequestFn = (
this: IExecuteFunctions | IHookFunctions, this: IExecuteFunctions | IHookFunctions,
query: string, query: string,
variables?: IDataObject,
) => Promise<IDataObject>;
async function proboGraphqlRequest(
this: IExecuteFunctions | IHookFunctions,
apiPath: string,
query: string,
variables: IDataObject = {}, variables: IDataObject = {},
): Promise<IDataObject> { ): Promise<IDataObject> {
const credentials = await this.getCredentials('proboApi'); const credentials = await this.getCredentials('proboApi');
@@ -21,7 +28,7 @@ export async function proboApiRequest(
const options: IHttpRequestOptions = { const options: IHttpRequestOptions = {
method: 'POST', method: 'POST',
baseURL: `${credentials.server}`, baseURL: `${credentials.server}`,
url: '/api/console/v1/graphql', url: apiPath,
headers: { headers: {
Authorization: `Bearer ${credentials.apiKey}`, Authorization: `Bearer ${credentials.apiKey}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -52,8 +59,25 @@ export async function proboApiRequest(
} }
} }
export async function proboApiRequestAllItems( export async function proboApiRequest(
this: IExecuteFunctions | IHookFunctions,
query: string,
variables: IDataObject = {},
): Promise<IDataObject> {
return proboGraphqlRequest.call(this, '/api/console/v1/graphql', query, variables);
}
export async function proboConnectApiRequest(
this: IExecuteFunctions | IHookFunctions,
query: string,
variables: IDataObject = {},
): Promise<IDataObject> {
return proboGraphqlRequest.call(this, '/api/connect/v1/graphql', query, variables);
}
async function proboGraphqlRequestAllItems(
this: IExecuteFunctions, this: IExecuteFunctions,
requestFn: ApiRequestFn,
query: string, query: string,
variables: IDataObject, variables: IDataObject,
getConnection: (response: IDataObject) => IDataObject | undefined, getConnection: (response: IDataObject) => IDataObject | undefined,
@@ -80,7 +104,7 @@ export async function proboApiRequestAllItems(
requestVariables.after = cursor; requestVariables.after = cursor;
} }
const responseData = await proboApiRequest.call(this, query, requestVariables); const responseData = await requestFn.call(this, query, requestVariables);
const connection = getConnection(responseData); const connection = getConnection(responseData);
if (connection?.edges) { if (connection?.edges) {
@@ -103,3 +127,41 @@ export async function proboApiRequestAllItems(
return items; return items;
} }
export async function proboApiRequestAllItems(
this: IExecuteFunctions,
query: string,
variables: IDataObject,
getConnection: (response: IDataObject) => IDataObject | undefined,
returnAll: boolean = true,
limit: number = 0,
): Promise<IDataObject[]> {
return proboGraphqlRequestAllItems.call(
this,
proboApiRequest,
query,
variables,
getConnection,
returnAll,
limit,
);
}
export async function proboConnectApiRequestAllItems(
this: IExecuteFunctions,
query: string,
variables: IDataObject,
getConnection: (response: IDataObject) => IDataObject | undefined,
returnAll: boolean = true,
limit: number = 0,
): Promise<IDataObject[]> {
return proboGraphqlRequestAllItems.call(
this,
proboConnectApiRequest,
query,
variables,
getConnection,
returnAll,
limit,
);
}

View File

@@ -1,5 +1,5 @@
import type { INodeProperties, IExecuteFunctions, INodeExecutionData, IDataObject } from 'n8n-workflow'; import type { INodeProperties, IExecuteFunctions, INodeExecutionData, IDataObject } from 'n8n-workflow';
import { proboApiRequestAllItems } from '../../GenericFunctions'; import { proboConnectApiRequestAllItems } from '../../GenericFunctions';
export const description: INodeProperties[] = [ export const description: INodeProperties[] = [
{ {
@@ -44,19 +44,21 @@ export async function execute(
const query = ` const query = `
query GetOrganizations($first: Int, $after: CursorKey) { query GetOrganizations($first: Int, $after: CursorKey) {
viewer { viewer {
organizations(first: $first, after: $after) { memberships(first: $first, after: $after) {
edges { edges {
node { node {
id organization {
name id
description name
websiteUrl description
email websiteUrl
headquarterAddress email
logoUrl headquarterAddress
horizontalLogoUrl logoUrl
createdAt horizontalLogoUrl
updatedAt createdAt
updatedAt
}
} }
} }
pageInfo { pageInfo {
@@ -68,19 +70,28 @@ export async function execute(
} }
`; `;
const organizations = await proboApiRequestAllItems.call( const memberships = await proboConnectApiRequestAllItems.call(
this, this,
query, query,
{}, {},
(response) => { (response: IDataObject) => {
const data = response?.data as IDataObject | undefined; const data = response?.data as IDataObject | undefined;
const viewer = data?.viewer as IDataObject | undefined; const viewer = data?.viewer as IDataObject | undefined;
return viewer?.organizations as IDataObject | undefined; return viewer?.memberships as IDataObject | undefined;
}, },
returnAll, returnAll,
limit, limit,
); );
const organizationMap = new Map<string, IDataObject>();
for (const membership of memberships) {
const org = membership.organization as IDataObject | undefined;
if (org && org.id) {
organizationMap.set(org.id as string, org);
}
}
const organizations = Array.from(organizationMap.values());
return { return {
json: { organizations }, json: { organizations },
pairedItem: { item: itemIndex }, pairedItem: { item: itemIndex },