download-introspection-schema.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* eslint-disable no-console */
  2. import { GraphQLTypesLoader } from '@nestjs/graphql';
  3. import { AdminUiPlugin } from '@vendure/admin-ui-plugin';
  4. import {
  5. DefaultLogger,
  6. getConfig,
  7. getFinalVendureSchema,
  8. LogLevel,
  9. runPluginConfigurations,
  10. setConfig,
  11. VendureConfig,
  12. } from '@vendure/core';
  13. import { writeFileSync } from 'fs';
  14. import { getIntrospectionQuery, graphqlSync } from 'graphql';
  15. import path from 'path';
  16. const VENDURE_SHOP_API_TYPE_PATHS = ['shop-api', 'common'].map(p =>
  17. path.join(__dirname, '../../packages/core/src/api/schema', p, '*.graphql'),
  18. );
  19. const VENDURE_ADMIN_API_TYPE_PATHS = ['admin-api', 'common'].map(p =>
  20. path.join(__dirname, '../../packages/core/src/api/schema', p, '*.graphql'),
  21. );
  22. export const config: VendureConfig = {
  23. apiOptions: {
  24. port: 3355,
  25. adminApiPath: 'admin-api',
  26. shopApiPath: 'shop-api',
  27. },
  28. authOptions: {
  29. tokenMethod: ['bearer', 'cookie'],
  30. superadminCredentials: {
  31. identifier: 'superadmin',
  32. password: 'superadmin',
  33. },
  34. },
  35. dbConnectionOptions: {
  36. type: 'sqljs',
  37. synchronize: true,
  38. logging: false,
  39. },
  40. paymentOptions: {
  41. paymentMethodHandlers: [],
  42. },
  43. plugins: [AdminUiPlugin],
  44. logger: new DefaultLogger({ level: LogLevel.Verbose }),
  45. };
  46. /**
  47. * Makes an introspection query to the Vendure server and writes the result to a
  48. * schema.json file.
  49. *
  50. * If there is an error connecting to the server, the promise resolves to false.
  51. */
  52. export async function downloadIntrospectionSchema(apiType: 'shop' | 'admin'): Promise<boolean> {
  53. try {
  54. await setConfig(config ?? {});
  55. const runtimeConfig = await runPluginConfigurations(getConfig());
  56. const typesLoader = new GraphQLTypesLoader();
  57. const schema = await getFinalVendureSchema({
  58. config: runtimeConfig,
  59. typePaths: apiType === 'admin' ? VENDURE_ADMIN_API_TYPE_PATHS : VENDURE_SHOP_API_TYPE_PATHS,
  60. typesLoader,
  61. apiType: apiType,
  62. });
  63. const fileName = `schema-${apiType}.json`;
  64. const outFile = path.join(process.cwd(), fileName);
  65. const jsonSchema = graphqlSync({
  66. schema,
  67. source: getIntrospectionQuery({ inputValueDeprecation: true }),
  68. });
  69. writeFileSync(outFile, JSON.stringify(jsonSchema));
  70. console.log(`Generated schema: ${outFile}`);
  71. return true;
  72. } catch (error) {
  73. console.error('An error occured when generating Introspection Schema');
  74. throw error;
  75. }
  76. }