generate-graphql-types.ts 5.6 KB

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