generate-graphql-types.ts 9.0 KB

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