Files
upstream-twenty/packages/twenty-server/test/integration/graphql/codegen/index.ts
upstream-mirror e7da97194a
Some checks failed
App Prod-Parity E2E Dispatch / dispatch (push) Failing after 10s
CD Storybook UI / build (push) Failing after 3m25s
CD Storybook UI / dispatch (push) Has been skipped
CD deploy main / website-changed-files (push) Successful in 18m8s
CI Agent Skills / changed-files-check (push) Successful in 18m9s
CI Create App E2E minimal / changed-files-check (push) Successful in 24s
CI Create App / changed-files-check (push) Successful in 26s
CI Docs / changed-files-check (push) Successful in 26s
CI Emails / changed-files-check (push) Successful in 28s
CI Example App Hello World / changed-files-check (push) Successful in 23s
CI Front / changed-files-check (push) Has been skipped
CI Example App Postcard / changed-files-check (push) Successful in 27s
CI UI / changed-files-check (push) Has been skipped
CI Utils Test / changed-files-check (push) Successful in 45s
CI Twenty Apps / discover (push) Successful in 1m3s
CD deploy main / deploy-main (push) Failing after 3s
CI Agent Skills / agent-skills-validate (push) Has been skipped
CI Create App / create-app-test (lint) (push) Has been skipped
CI Emails / emails-test (push) Has been skipped
CI Front / front-task (lingui:compile) (push) Has been skipped
CI Front / front-task (lint) (push) Has been skipped
CI Example App Postcard / example-app-postcard (push) Has been skipped
CI UI / ui-task (lint) (push) Has been skipped
CI UI / ui-task (pack-size) (push) Has been skipped
CI UI / ui-task (size) (push) Has been skipped
CI UI / ui-task (test) (push) Has been skipped
CI UI / ui-task (typecheck) (push) Has been skipped
CI Utils Test / utils-test (push) Has been skipped
CI Twenty Apps / ${{ matrix.app.name }} (push) Has been skipped
CI Emails / ci-emails-status-check (push) Successful in 2s
CI Example App Hello World / ci-example-app-hello-world-status-check (push) Successful in 2s
CI Example App Postcard / ci-example-app-postcard-status-check (push) Successful in 3s
CI UI / ui-sb-test (push) Has been skipped
CI Utils Test / ci-utils-test-status-check (push) Successful in 2s
CI Twenty Apps / ci-twenty-apps-status-check (push) Successful in 2s
CI UI / ci-ui-status-check (push) Successful in 2s
CI Desktop / desktop (push) Failing after 1m8s
Push docs to Crowdin / Push documentation to Crowdin (push) Failing after 55s
Push translations to Crowdin / Extract and upload translations (push) Failing after 5s
Push website translations to Crowdin / Extract and upload website translations (push) Failing after 5s
CI Create App / create-app-test (test) (push) Has been skipped
CI Create App / create-app-test (typecheck) (push) Has been skipped
CI Docs / docs-lint (push) Has been skipped
CI Example App Hello World / example-app-hello-world (push) Has been skipped
CI Front / front-task (lingui:extract) (push) Has been skipped
CI Front / front-task (test) (push) Has been skipped
CI Front / front-task (typecheck) (push) Has been skipped
CI UI / ui-sb-build (push) Failing after 1m32s
CI Twenty Apps / ${{ matrix.app.name }} (${{ matrix.server-source }}) (push) Has been skipped
CI Agent Skills / agent-skills-publish (push) Has been skipped
CI Create App / ci-create-app-status-check (push) Successful in 2s
CI Agent Skills / agent-skills-remote-source (push) Has been skipped
CI Create App E2E minimal / create-app-e2e-minimal (push) Has been cancelled
CI E2E Main / e2e-test (push) Has been cancelled
CI Front / front-sb-build (push) Has been cancelled
CI Front / front-build (push) Has been cancelled
CI Create App E2E minimal / ci-create-app-e2e-minimal-status-check (push) Has been cancelled
CI E2E Main / ci-e2e-main-status-check (push) Has been cancelled
CI E2E Main / notify-main-ci-failure (push) Has been cancelled
CI E2E Main / qa-scout-comment (push) Has been cancelled
CI Front / front-sb-test (push) Has been cancelled
CI Front / front-sb-screenshots (push) Has been cancelled
CI Front / ci-front-status-check (push) Has been cancelled
DPA Sub-Processor Sync / sync-subprocessors (push) Failing after 3s
AI Catalog Sync / sync-catalog (push) Failing after 2s
snapshot of https://github.com/twentyhq/twenty.git (main, 2026-09-17; blobs > 131072B omitted)
2026-09-17 23:28:13 +05:00

214 lines
5.8 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import * as process from 'process';
import { pascalToKebab } from 'twenty-shared/utils';
import { INTROSPECTION_QUERY } from './introspection-query';
import {
type Field,
type InputValue,
type IntrospectionResponse,
type TypeRef,
} from './introspection.interface';
const GRAPHQL_URL = 'http://localhost:3000/graphql';
const BEARER_TOKEN =
'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMDIwMjAyMC05ZTNiLTQ2ZDQtYTU1Ni04OGI5ZGRjMmIwMzQiLCJ3b3Jrc3BhY2VJZCI6IjIwMjAyMDIwLTFjMjUtNGQwMi1iZjI1LTZhZWNjZjdlYTQxOSIsIndvcmtzcGFjZU1lbWJlcklkIjoiMjAyMDIwMjAtMDY4Ny00YzQxLWI3MDctZWQxYmZjYTk3MmE3IiwiaWF0IjoxNzI2NDkyNTAyLCJleHAiOjEzMjQ1MDE2NTAyfQ.zM6TbfeOqYVH5Sgryc2zf02hd9uqUOSL1-iJlMgwzsI';
const TEST_OUTPUT_DIR = './test/integration/graphql/suites/object-generated';
const fetchGraphQLSchema = async (): Promise<IntrospectionResponse> => {
const headers = {
Authorization: BEARER_TOKEN,
'Content-Type': 'application/json',
};
const response = await fetch(GRAPHQL_URL, {
method: 'POST',
headers,
body: JSON.stringify({ query: INTROSPECTION_QUERY }),
});
if (!response.ok) {
throw new Error(`Failed to fetch schema: ${response.statusText}`);
}
return response.json();
};
const unwrapType = (typeInfo: TypeRef): any => {
while (typeInfo.ofType) {
typeInfo = typeInfo.ofType;
}
return typeInfo;
};
const hasRequiredArgs = (args: InputValue[]): boolean => {
return args.some((arg) => unwrapType(arg.type).kind === 'NON_NULL');
};
const generateTestContent = (
queryName: string,
fields: Field[],
): string | null => {
const fieldNames = fields
.filter((f) => ['SCALAR', 'ENUM'].includes(unwrapType(f.type).kind))
.map((f) => f.name);
if (fieldNames.length === 0) {
// oxlint-disable-next-line no-console
console.log(`Skipping ${queryName}: No usable fields found.`);
return null;
}
const fieldSelection = fieldNames.join('\n ');
const expectSelection = fieldNames
.map((f) => `expect(${queryName}).toHaveProperty('${f}');`)
.join('\n ');
return `import request from 'supertest';
const client = request(\`http://localhost:\${APP_PORT}\`);
describe('${queryName}Resolver (e2e)', () => {
it('should find many ${queryName}', () => {
const queryData = {
query: \`
query ${queryName} {
${queryName} {
edges {
node {
${fieldSelection}
}
}
}
}
\`,
};
return client
.post('/graphql')
.set('Authorization', \`Bearer \${APPLE_JANE_ADMIN_ACCESS_TOKEN}\`)
.send(queryData)
.expect(200)
.expect((res) => {
expect(res.body.data).toBeDefined();
expect(res.body.errors).toBeUndefined();
})
.expect((res) => {
const data = res.body.data.${queryName};
expect(data).toBeDefined();
expect(Array.isArray(data.edges)).toBe(true);
const edges = data.edges;
if (edges.length > 0) {
const ${queryName} = edges[0].node;
${expectSelection}
}
});
});
});
`;
};
const writeTestFile = (
queryName: string,
content: string | null,
force = false,
): string => {
if (!content) return 'skipped';
const fileName = `${pascalToKebab(queryName)}.integration-spec.ts`;
const filePath = path.join(TEST_OUTPUT_DIR, fileName);
if (fs.existsSync(filePath) && !force) {
return 'skipped';
}
fs.writeFileSync(filePath, content);
return force ? 'updated' : 'created';
};
const generateTests = async (force = false) => {
fs.mkdirSync(TEST_OUTPUT_DIR, { recursive: true });
const schemaData = await fetchGraphQLSchema();
const types = schemaData.data.__schema.types;
const queryTypeName = schemaData.data.__schema.queryType.name;
const queryType = types.find((t: any) => t.name === queryTypeName);
let createdCount = 0;
let updatedCount = 0;
let totalCount = 0;
if (!queryType?.fields) {
// oxlint-disable-next-line no-console
console.log('No query fields found.');
return;
}
for (const query of queryType.fields) {
const queryName = query.name;
if (hasRequiredArgs(query.args)) continue;
if (queryName.includes('Duplicates')) continue;
const queryReturnType = unwrapType(query.type);
if (
queryReturnType.kind === 'OBJECT' &&
queryReturnType.name.includes('Connection')
) {
totalCount++;
const connectionTypeInfo = types.find(
(f: any) => f.name === queryReturnType.name,
);
const edgeTypeInfo = connectionTypeInfo?.fields?.find(
(f: any) => f.name === 'edges',
);
if (edgeTypeInfo) {
const returnType = unwrapType(edgeTypeInfo.type);
const returnTypeInfo = types.find(
(t: any) => t.name === returnType.name,
);
const returnNodeTypeInfo = returnTypeInfo?.fields?.find(
(f: any) => f.name === 'node',
);
if (returnNodeTypeInfo) {
const nodeType = unwrapType(returnNodeTypeInfo.type);
const nodeTypeInfo = types.find((t: any) => t.name === nodeType.name);
if (!nodeTypeInfo?.fields) {
continue;
}
const content = generateTestContent(queryName, nodeTypeInfo?.fields);
const result = writeTestFile(queryName, content, force);
if (result === 'created') createdCount++;
if (result === 'updated') updatedCount++;
}
}
}
}
// oxlint-disable-next-line no-console
console.log(`Number of tests created: ${createdCount}/${totalCount}`);
if (force) {
// oxlint-disable-next-line no-console
console.log(`Number of tests updated: ${updatedCount}/${totalCount}`);
}
};
const forceArg = process.argv.includes('--force');
void generateTests(forceArg);