generate-graphql-types.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { generate } from '@graphql-codegen/cli';
  2. import fs from 'fs';
  3. import { buildClientSchema, graphqlSync, introspectionQuery } from 'graphql';
  4. import { mergeSchemas } from 'graphql-tools';
  5. import path from 'path';
  6. import { ADMIN_API_PATH, API_PORT, SHOP_API_PATH } from '../../packages/common/src/shared-constants';
  7. import { downloadIntrospectionSchema } from './download-introspection-schema';
  8. const CLIENT_QUERY_FILES = path.join(
  9. __dirname,
  10. '../../packages/admin-ui/src/lib/core/src/data/definitions/**/*.ts',
  11. );
  12. const E2E_ADMIN_QUERY_FILES = path.join(
  13. __dirname,
  14. '../../packages/core/e2e/**/!(import.e2e-spec|plugin.e2e-spec|shop-definitions|custom-fields.e2e-spec|price-calculation-strategy.e2e-spec|list-query-builder.e2e-spec|shop-order.e2e-spec|database-transactions.e2e-spec).ts',
  15. );
  16. const E2E_SHOP_QUERY_FILES = [path.join(__dirname, '../../packages/core/e2e/graphql/shop-definitions.ts')];
  17. const E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES = path.join(
  18. __dirname,
  19. '../../packages/elasticsearch-plugin/e2e/**/*.ts',
  20. );
  21. const E2E_ASSET_SERVER_PLUGIN_QUERY_FILES = path.join(
  22. __dirname,
  23. '../../packages/asset-server-plugin/e2e/**/*.ts',
  24. );
  25. const ADMIN_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-admin.json');
  26. const SHOP_SCHEMA_OUTPUT_FILE = path.join(__dirname, '../../schema-shop.json');
  27. // tslint:disable:no-console
  28. Promise.all([
  29. downloadIntrospectionSchema(ADMIN_API_PATH, ADMIN_SCHEMA_OUTPUT_FILE),
  30. downloadIntrospectionSchema(SHOP_API_PATH, SHOP_SCHEMA_OUTPUT_FILE),
  31. ])
  32. .then(([adminSchemaSuccess, shopSchemaSuccess]) => {
  33. if (!adminSchemaSuccess || !shopSchemaSuccess) {
  34. console.log('Attempting to generate types from existing schema json files...');
  35. }
  36. const adminSchemaJson = JSON.parse(fs.readFileSync(ADMIN_SCHEMA_OUTPUT_FILE, 'utf-8'));
  37. const shopSchemaJson = JSON.parse(fs.readFileSync(SHOP_SCHEMA_OUTPUT_FILE, 'utf-8'));
  38. const adminSchema = buildClientSchema(adminSchemaJson.data);
  39. const shopSchema = buildClientSchema(shopSchemaJson.data);
  40. const config = {
  41. namingConvention: {
  42. enumValues: 'keep',
  43. },
  44. strict: true,
  45. };
  46. const e2eConfig = {
  47. ...config,
  48. skipTypename: true,
  49. };
  50. const disableTsLintPlugin = { add: { content: '// tslint:disable' } };
  51. const graphQlErrorsPlugin = path.join(__dirname, './plugins/graphql-errors-plugin.js');
  52. const commonPlugins = [disableTsLintPlugin, 'typescript'];
  53. const clientPlugins = [...commonPlugins, 'typescript-operations', 'typescript-compatibility'];
  54. return generate({
  55. overwrite: true,
  56. generates: {
  57. [path.join(
  58. __dirname,
  59. '../../packages/core/src/common/error/generated-graphql-admin-errors.ts',
  60. )]: {
  61. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  62. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  63. },
  64. [path.join(
  65. __dirname,
  66. '../../packages/core/src/common/error/generated-graphql-shop-errors.ts',
  67. )]: {
  68. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  69. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  70. },
  71. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-admin-types.ts')]: {
  72. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  73. documents: E2E_ADMIN_QUERY_FILES,
  74. plugins: clientPlugins,
  75. config: e2eConfig,
  76. },
  77. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-shop-types.ts')]: {
  78. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  79. documents: E2E_SHOP_QUERY_FILES,
  80. plugins: clientPlugins,
  81. config: e2eConfig,
  82. },
  83. [path.join(
  84. __dirname,
  85. '../../packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts',
  86. )]: {
  87. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  88. documents: E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES,
  89. plugins: clientPlugins,
  90. config: e2eConfig,
  91. },
  92. [path.join(
  93. __dirname,
  94. '../../packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts',
  95. )]: {
  96. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  97. documents: E2E_ASSET_SERVER_PLUGIN_QUERY_FILES,
  98. plugins: clientPlugins,
  99. config: e2eConfig,
  100. },
  101. // [path.join(
  102. // __dirname,
  103. // '../../packages/admin-ui/src/lib/core/src/common/generated-types.ts',
  104. // )]: {
  105. // schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  106. // documents: CLIENT_QUERY_FILES,
  107. // plugins: clientPlugins,
  108. // config,
  109. // },
  110. // [path.join(
  111. // __dirname,
  112. // '../../packages/admin-ui/src/lib/core/src/common/introspection-result.ts',
  113. // )]: {
  114. // schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  115. // documents: CLIENT_QUERY_FILES,
  116. // plugins: [disableTsLintPlugin, 'fragment-matcher'],
  117. // config,
  118. // },
  119. [path.join(__dirname, '../../packages/common/src/generated-types.ts')]: {
  120. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  121. plugins: commonPlugins,
  122. config: {
  123. ...config,
  124. scalars: {
  125. ID: 'string | number',
  126. },
  127. maybeValue: 'T',
  128. },
  129. },
  130. [path.join(__dirname, '../../packages/common/src/generated-shop-types.ts')]: {
  131. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  132. plugins: commonPlugins,
  133. config: {
  134. ...config,
  135. scalars: {
  136. ID: 'string | number',
  137. },
  138. maybeValue: 'T',
  139. },
  140. },
  141. },
  142. });
  143. })
  144. .then(
  145. result => {
  146. process.exit(0);
  147. },
  148. err => {
  149. console.error(err);
  150. process.exit(1);
  151. },
  152. );