generate-graphql-types.ts 7.0 KB

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