generate-graphql-types.ts 7.0 KB

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