generate-graphql-types.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. 'entity-hydrator.e2e-spec',
  25. ];
  26. const E2E_ADMIN_QUERY_FILES = path.join(
  27. __dirname,
  28. `../../packages/core/e2e/**/!(${specFileToIgnore.join('|')}).ts`,
  29. );
  30. const E2E_SHOP_QUERY_FILES = [path.join(__dirname, '../../packages/core/e2e/graphql/shop-definitions.ts')];
  31. const E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES = path.join(
  32. __dirname,
  33. '../../packages/elasticsearch-plugin/e2e/**/*.ts',
  34. );
  35. const E2E_ASSET_SERVER_PLUGIN_QUERY_FILES = path.join(
  36. __dirname,
  37. '../../packages/asset-server-plugin/e2e/**/*.ts',
  38. );
  39. const ADMIN_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-admin.json');
  40. const SHOP_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-shop.json');
  41. // tslint:disable:no-console
  42. Promise.all([
  43. downloadIntrospectionSchema(ADMIN_API_PATH, ADMIN_SCHEMA_OUTPUT_FILE),
  44. downloadIntrospectionSchema(SHOP_API_PATH, SHOP_SCHEMA_OUTPUT_FILE),
  45. ])
  46. .then(([adminSchemaSuccess, shopSchemaSuccess]) => {
  47. if (!adminSchemaSuccess || !shopSchemaSuccess) {
  48. console.log('Attempting to generate types from existing schema json files...');
  49. }
  50. const adminSchemaJson = JSON.parse(fs.readFileSync(ADMIN_SCHEMA_OUTPUT_FILE, 'utf-8'));
  51. const shopSchemaJson = JSON.parse(fs.readFileSync(SHOP_SCHEMA_OUTPUT_FILE, 'utf-8'));
  52. const adminSchema = buildClientSchema(adminSchemaJson.data);
  53. const shopSchema = buildClientSchema(shopSchemaJson.data);
  54. const config = {
  55. namingConvention: {
  56. enumValues: 'keep',
  57. },
  58. strict: true,
  59. };
  60. const e2eConfig = {
  61. ...config,
  62. skipTypename: true,
  63. };
  64. const disableTsLintPlugin = { add: { content: '// tslint:disable' } };
  65. const graphQlErrorsPlugin = path.join(__dirname, './plugins/graphql-errors-plugin.js');
  66. const commonPlugins = [disableTsLintPlugin, 'typescript'];
  67. const clientPlugins = [...commonPlugins, 'typescript-operations'];
  68. return generate({
  69. overwrite: true,
  70. generates: {
  71. [path.join(
  72. __dirname,
  73. '../../packages/core/src/common/error/generated-graphql-admin-errors.ts',
  74. )]: {
  75. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  76. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  77. },
  78. [path.join(
  79. __dirname,
  80. '../../packages/core/src/common/error/generated-graphql-shop-errors.ts',
  81. )]: {
  82. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  83. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  84. },
  85. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-admin-types.ts')]: {
  86. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  87. documents: E2E_ADMIN_QUERY_FILES,
  88. plugins: clientPlugins,
  89. config: e2eConfig,
  90. },
  91. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-shop-types.ts')]: {
  92. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  93. documents: E2E_SHOP_QUERY_FILES,
  94. plugins: clientPlugins,
  95. config: e2eConfig,
  96. },
  97. [path.join(
  98. __dirname,
  99. '../../packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts',
  100. )]: {
  101. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  102. documents: E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES,
  103. plugins: clientPlugins,
  104. config: e2eConfig,
  105. },
  106. [path.join(
  107. __dirname,
  108. '../../packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts',
  109. )]: {
  110. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  111. documents: E2E_ASSET_SERVER_PLUGIN_QUERY_FILES,
  112. plugins: clientPlugins,
  113. config: e2eConfig,
  114. },
  115. [path.join(__dirname, '../../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. [path.join(__dirname, '../../packages/payments-plugin/e2e/graphql/generated-admin-types.ts')]:
  157. {
  158. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  159. documents: path.join(
  160. __dirname,
  161. '../../packages/payments-plugin/e2e/graphql/admin-queries.ts',
  162. ),
  163. plugins: clientPlugins,
  164. config: e2eConfig,
  165. },
  166. [path.join(__dirname, '../../packages/payments-plugin/e2e/graphql/generated-shop-types.ts')]:
  167. {
  168. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  169. documents: path.join(
  170. __dirname,
  171. '../../packages/payments-plugin/e2e/graphql/shop-queries.ts',
  172. ),
  173. plugins: clientPlugins,
  174. config: e2eConfig,
  175. },
  176. [path.join(__dirname, '../../packages/payments-plugin/src/mollie/graphql/generated-shop-types.ts')]:
  177. {
  178. schema: [SHOP_SCHEMA_OUTPUT_FILE, path.join(__dirname, '../../packages/payments-plugin/src/mollie/mollie-shop-schema.ts')],
  179. plugins: clientPlugins,
  180. config,
  181. },
  182. },
  183. });
  184. })
  185. .then(
  186. result => {
  187. process.exit(0);
  188. },
  189. err => {
  190. console.error(err);
  191. process.exit(1);
  192. },
  193. );