generate-graphql-types.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { generate } from '@graphql-codegen/cli';
  2. import fs from 'fs';
  3. import { buildClientSchema } from 'graphql';
  4. import path from 'path';
  5. import { ADMIN_API_PATH, SHOP_API_PATH } from '../../packages/common/src/shared-constants';
  6. import { downloadIntrospectionSchema } from './download-introspection-schema';
  7. const CLIENT_QUERY_FILES = path.join(
  8. __dirname,
  9. '../../packages/admin-ui/src/lib/core/src/data/definitions/**/*.ts',
  10. );
  11. const specFileToIgnore = [
  12. 'import.e2e-spec',
  13. 'plugin.e2e-spec',
  14. 'shop-definitions',
  15. 'custom-fields.e2e-spec',
  16. 'price-calculation-strategy.e2e-spec',
  17. 'list-query-builder.e2e-spec',
  18. 'shop-order.e2e-spec',
  19. 'database-transactions.e2e-spec',
  20. 'custom-permissions.e2e-spec',
  21. 'parallel-transactions.e2e-spec',
  22. ];
  23. const E2E_ADMIN_QUERY_FILES = path.join(
  24. __dirname,
  25. `../../packages/core/e2e/**/!(${specFileToIgnore.join('|')}).ts`,
  26. );
  27. const E2E_SHOP_QUERY_FILES = [path.join(__dirname, '../../packages/core/e2e/graphql/shop-definitions.ts')];
  28. const E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES = path.join(
  29. __dirname,
  30. '../../packages/elasticsearch-plugin/e2e/**/*.ts',
  31. );
  32. const E2E_ASSET_SERVER_PLUGIN_QUERY_FILES = path.join(
  33. __dirname,
  34. '../../packages/asset-server-plugin/e2e/**/*.ts',
  35. );
  36. const ADMIN_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-admin.json');
  37. const SHOP_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-shop.json');
  38. // tslint:disable:no-console
  39. Promise.all([
  40. downloadIntrospectionSchema(ADMIN_API_PATH, ADMIN_SCHEMA_OUTPUT_FILE),
  41. downloadIntrospectionSchema(SHOP_API_PATH, SHOP_SCHEMA_OUTPUT_FILE),
  42. ])
  43. .then(([adminSchemaSuccess, shopSchemaSuccess]) => {
  44. if (!adminSchemaSuccess || !shopSchemaSuccess) {
  45. console.log('Attempting to generate types from existing schema json files...');
  46. }
  47. const adminSchemaJson = JSON.parse(fs.readFileSync(ADMIN_SCHEMA_OUTPUT_FILE, 'utf-8'));
  48. const shopSchemaJson = JSON.parse(fs.readFileSync(SHOP_SCHEMA_OUTPUT_FILE, 'utf-8'));
  49. const adminSchema = buildClientSchema(adminSchemaJson.data);
  50. const shopSchema = buildClientSchema(shopSchemaJson.data);
  51. const config = {
  52. namingConvention: {
  53. enumValues: 'keep',
  54. },
  55. strict: true,
  56. };
  57. const e2eConfig = {
  58. ...config,
  59. skipTypename: true,
  60. };
  61. const disableTsLintPlugin = { add: { content: '// tslint:disable' } };
  62. const graphQlErrorsPlugin = path.join(__dirname, './plugins/graphql-errors-plugin.js');
  63. const commonPlugins = [disableTsLintPlugin, 'typescript'];
  64. const clientPlugins = [...commonPlugins, 'typescript-operations', 'typescript-compatibility'];
  65. return generate({
  66. overwrite: true,
  67. generates: {
  68. [path.join(
  69. __dirname,
  70. '../../packages/core/src/common/error/generated-graphql-admin-errors.ts',
  71. )]: {
  72. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  73. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  74. },
  75. [path.join(
  76. __dirname,
  77. '../../packages/core/src/common/error/generated-graphql-shop-errors.ts',
  78. )]: {
  79. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  80. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  81. },
  82. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-admin-types.ts')]: {
  83. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  84. documents: E2E_ADMIN_QUERY_FILES,
  85. plugins: clientPlugins,
  86. config: e2eConfig,
  87. },
  88. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-shop-types.ts')]: {
  89. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  90. documents: E2E_SHOP_QUERY_FILES,
  91. plugins: clientPlugins,
  92. config: e2eConfig,
  93. },
  94. [path.join(
  95. __dirname,
  96. '../../packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts',
  97. )]: {
  98. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  99. documents: E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES,
  100. plugins: clientPlugins,
  101. config: e2eConfig,
  102. },
  103. [path.join(
  104. __dirname,
  105. '../../packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts',
  106. )]: {
  107. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  108. documents: E2E_ASSET_SERVER_PLUGIN_QUERY_FILES,
  109. plugins: clientPlugins,
  110. config: e2eConfig,
  111. },
  112. [path.join(
  113. __dirname,
  114. '../../packages/admin-ui/src/lib/core/src/common/generated-types.ts',
  115. )]: {
  116. schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  117. documents: CLIENT_QUERY_FILES,
  118. plugins: clientPlugins,
  119. config: {
  120. ...config,
  121. skipTypeNameForRoot: true,
  122. },
  123. },
  124. [path.join(
  125. __dirname,
  126. '../../packages/admin-ui/src/lib/core/src/common/introspection-result.ts',
  127. )]: {
  128. schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  129. documents: CLIENT_QUERY_FILES,
  130. plugins: [disableTsLintPlugin, 'fragment-matcher'],
  131. config: { ...config, apolloClientVersion: 3 },
  132. },
  133. [path.join(__dirname, '../../packages/common/src/generated-types.ts')]: {
  134. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  135. plugins: commonPlugins,
  136. config: {
  137. ...config,
  138. scalars: {
  139. ID: 'string | number',
  140. },
  141. maybeValue: 'T',
  142. },
  143. },
  144. [path.join(__dirname, '../../packages/common/src/generated-shop-types.ts')]: {
  145. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  146. plugins: commonPlugins,
  147. config: {
  148. ...config,
  149. scalars: {
  150. ID: 'string | number',
  151. },
  152. maybeValue: 'T',
  153. },
  154. },
  155. },
  156. });
  157. })
  158. .then(
  159. result => {
  160. process.exit(0);
  161. },
  162. err => {
  163. console.error(err);
  164. process.exit(1);
  165. },
  166. );