generate-graphql-types.ts 9.1 KB

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