generate-graphql-types.ts 9.0 KB

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