generate-graphql-types.ts 6.9 KB

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