generate-gql-tada-types.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { generateOutput } from '@gql.tada/cli-utils';
  2. import { buildClientSchema, printSchema } from 'graphql';
  3. import { writeFileSync, readFileSync, mkdirSync } from 'fs';
  4. import { join } from 'path';
  5. import { tmpdir } from 'os';
  6. const ADMIN_SCHEMA_JSON = join(__dirname, '../../schema-admin.json');
  7. const SHOP_SCHEMA_JSON = join(__dirname, '../../schema-shop.json');
  8. const PACKAGES_WITH_E2E = [
  9. 'packages/core/e2e',
  10. 'packages/elasticsearch-plugin/e2e',
  11. 'packages/payments-plugin/e2e',
  12. 'packages/asset-server-plugin/e2e',
  13. ];
  14. async function generateGqlTadaTypes() {
  15. console.log('Generating gql.tada types for e2e tests...\n');
  16. for (const packagePath of PACKAGES_WITH_E2E) {
  17. console.log(`Processing ${packagePath}...`);
  18. await generateTypesForPackage(packagePath);
  19. }
  20. console.log('\n✓ All gql.tada types generated successfully');
  21. }
  22. async function generateTypesForPackage(packagePath: string) {
  23. const graphqlDir = join(__dirname, '../../', packagePath, 'graphql');
  24. mkdirSync(graphqlDir, { recursive: true });
  25. await generateTypesForApi('admin', ADMIN_SCHEMA_JSON, graphqlDir);
  26. await generateTypesForApi('shop', SHOP_SCHEMA_JSON, graphqlDir);
  27. }
  28. async function generateTypesForApi(
  29. apiType: 'admin' | 'shop',
  30. schemaJsonPath: string,
  31. outputDir: string,
  32. ) {
  33. const schemaJson = JSON.parse(readFileSync(schemaJsonPath, 'utf-8'));
  34. const schema = buildClientSchema(schemaJson.data);
  35. const sdl = printSchema(schema);
  36. const tempDir = join(tmpdir(), `gql-tada-${apiType}-${Date.now()}`);
  37. mkdirSync(tempDir, { recursive: true });
  38. const schemaPath = join(tempDir, 'schema.graphql');
  39. writeFileSync(schemaPath, sdl);
  40. const tsConfigContent = {
  41. compilerOptions: {
  42. plugins: [
  43. {
  44. name: 'gql.tada/ts-plugin',
  45. schema: schemaPath,
  46. },
  47. ],
  48. },
  49. };
  50. const tsConfigPath = join(tempDir, 'tsconfig.json');
  51. writeFileSync(tsConfigPath, JSON.stringify(tsConfigContent, null, 2));
  52. const outputPath = join(outputDir, `graphql-env-${apiType}.d.ts`);
  53. await generateOutput({
  54. output: outputPath,
  55. tsconfig: tsConfigPath,
  56. });
  57. const graphqlWrapperPath = join(outputDir, `graphql-${apiType}.ts`);
  58. const wrapperContent = `import type { introspection } from './graphql-env-${apiType}.d.ts';
  59. import { initGraphQLTada } from 'gql.tada';
  60. export const graphql = initGraphQLTada<{
  61. disableMasking: true;
  62. introspection: introspection;
  63. scalars: {
  64. DateTime: string;
  65. JSON: any;
  66. Money: number;
  67. };
  68. }>();
  69. export type { FragmentOf, ResultOf, VariablesOf } from 'gql.tada';
  70. export { readFragment } from 'gql.tada';
  71. `;
  72. writeFileSync(graphqlWrapperPath, wrapperContent);
  73. console.log(` ✓ Generated ${apiType} API types`);
  74. }
  75. generateGqlTadaTypes().catch(err => {
  76. console.error('Failed to generate gql.tada types:', err);
  77. process.exit(1);
  78. });