generate-graphql-types.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 disableTsLintPlugin = { add: { content: '// tslint:disable' } };
  47. const graphQlErrorsPlugin = path.join(__dirname, './plugins/graphql-errors-plugin.js');
  48. const commonPlugins = [disableTsLintPlugin, 'typescript'];
  49. const clientPlugins = [...commonPlugins, 'typescript-operations', 'typescript-compatibility'];
  50. return generate({
  51. overwrite: true,
  52. generates: {
  53. [path.join(
  54. __dirname,
  55. '../../packages/core/src/common/error/generated-graphql-admin-errors.ts',
  56. )]: {
  57. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  58. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  59. },
  60. [path.join(
  61. __dirname,
  62. '../../packages/core/src/common/error/generated-graphql-shop-errors.ts',
  63. )]: {
  64. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  65. plugins: [disableTsLintPlugin, graphQlErrorsPlugin],
  66. },
  67. /*[path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-admin-types.ts')]: {
  68. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  69. documents: E2E_ADMIN_QUERY_FILES,
  70. plugins: clientPlugins,
  71. config,
  72. },
  73. [path.join(__dirname, '../../packages/core/e2e/graphql/generated-e2e-shop-types.ts')]: {
  74. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  75. documents: E2E_SHOP_QUERY_FILES,
  76. plugins: clientPlugins,
  77. config,
  78. },
  79. [path.join(
  80. __dirname,
  81. '../../packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts',
  82. )]: {
  83. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  84. documents: E2E_ELASTICSEARCH_PLUGIN_QUERY_FILES,
  85. plugins: clientPlugins,
  86. config,
  87. },
  88. [path.join(
  89. __dirname,
  90. '../../packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts',
  91. )]: {
  92. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  93. documents: E2E_ASSET_SERVER_PLUGIN_QUERY_FILES,
  94. plugins: clientPlugins,
  95. config,
  96. },
  97. [path.join(
  98. __dirname,
  99. '../../packages/admin-ui/src/lib/core/src/common/generated-types.ts',
  100. )]: {
  101. schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  102. documents: CLIENT_QUERY_FILES,
  103. plugins: clientPlugins,
  104. config,
  105. },
  106. [path.join(
  107. __dirname,
  108. '../../packages/admin-ui/src/lib/core/src/common/introspection-result.ts',
  109. )]: {
  110. schema: [ADMIN_SCHEMA_OUTPUT_FILE, path.join(__dirname, 'client-schema.ts')],
  111. documents: CLIENT_QUERY_FILES,
  112. plugins: [disableTsLintPlugin, 'fragment-matcher'],
  113. config,
  114. },
  115. [path.join(__dirname, '../../packages/common/src/generated-types.ts')]: {
  116. schema: [ADMIN_SCHEMA_OUTPUT_FILE],
  117. plugins: commonPlugins,
  118. config: {
  119. ...config,
  120. scalars: {
  121. ID: 'string | number',
  122. },
  123. maybeValue: 'T',
  124. },
  125. },
  126. [path.join(__dirname, '../../packages/common/src/generated-shop-types.ts')]: {
  127. schema: [SHOP_SCHEMA_OUTPUT_FILE],
  128. plugins: commonPlugins,
  129. config: {
  130. ...config,
  131. scalars: {
  132. ID: 'string | number',
  133. },
  134. maybeValue: 'T',
  135. },
  136. },*/
  137. },
  138. });
  139. })
  140. .then(
  141. result => {
  142. process.exit(0);
  143. },
  144. err => {
  145. console.error(err);
  146. process.exit(1);
  147. },
  148. );