schemaManager.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SchemaManager = void 0;
  4. class SchemaManager {
  5. constructor(options) {
  6. this.onSchemaLoadOrUpdateListeners = new Set();
  7. this.isStopped = false;
  8. this.logger = options.logger;
  9. this.schemaDerivedDataProvider = options.schemaDerivedDataProvider;
  10. if ('gateway' in options) {
  11. this.modeSpecificState = {
  12. mode: 'gateway',
  13. gateway: options.gateway,
  14. apolloConfig: options.apolloConfig,
  15. };
  16. }
  17. else {
  18. this.modeSpecificState = {
  19. mode: 'schema',
  20. apiSchema: options.apiSchema,
  21. schemaDerivedData: options.schemaDerivedDataProvider(options.apiSchema),
  22. };
  23. }
  24. }
  25. async start() {
  26. if (this.modeSpecificState.mode === 'gateway') {
  27. const gateway = this.modeSpecificState.gateway;
  28. if (gateway.onSchemaLoadOrUpdate) {
  29. this.modeSpecificState.unsubscribeFromGateway =
  30. gateway.onSchemaLoadOrUpdate((schemaContext) => {
  31. this.processSchemaLoadOrUpdateEvent(schemaContext);
  32. });
  33. }
  34. else {
  35. throw new Error("Unexpectedly couldn't find onSchemaLoadOrUpdate on gateway");
  36. }
  37. const config = await this.modeSpecificState.gateway.load({
  38. apollo: this.modeSpecificState.apolloConfig,
  39. });
  40. return config.executor;
  41. }
  42. else {
  43. this.processSchemaLoadOrUpdateEvent({
  44. apiSchema: this.modeSpecificState.apiSchema,
  45. }, this.modeSpecificState.schemaDerivedData);
  46. return null;
  47. }
  48. }
  49. onSchemaLoadOrUpdate(callback) {
  50. if (!this.schemaContext) {
  51. throw new Error('You must call start() before onSchemaLoadOrUpdate()');
  52. }
  53. if (!this.isStopped) {
  54. try {
  55. callback(this.schemaContext);
  56. }
  57. catch (e) {
  58. throw new Error(`An error was thrown from an 'onSchemaLoadOrUpdate' listener: ${e.message}`);
  59. }
  60. }
  61. this.onSchemaLoadOrUpdateListeners.add(callback);
  62. return () => {
  63. this.onSchemaLoadOrUpdateListeners.delete(callback);
  64. };
  65. }
  66. getSchemaDerivedData() {
  67. if (!this.schemaDerivedData) {
  68. throw new Error('You must call start() before getSchemaDerivedData()');
  69. }
  70. return this.schemaDerivedData;
  71. }
  72. async stop() {
  73. this.isStopped = true;
  74. if (this.modeSpecificState.mode === 'gateway') {
  75. this.modeSpecificState.unsubscribeFromGateway?.();
  76. await this.modeSpecificState.gateway.stop?.();
  77. }
  78. }
  79. processSchemaLoadOrUpdateEvent(schemaContext, schemaDerivedData) {
  80. if (!this.isStopped) {
  81. this.schemaDerivedData =
  82. schemaDerivedData ??
  83. this.schemaDerivedDataProvider(schemaContext.apiSchema);
  84. this.schemaContext = schemaContext;
  85. this.onSchemaLoadOrUpdateListeners.forEach((listener) => {
  86. try {
  87. listener(schemaContext);
  88. }
  89. catch (e) {
  90. this.logger.error("An error was thrown from an 'onSchemaLoadOrUpdate' listener");
  91. this.logger.error(e);
  92. }
  93. });
  94. }
  95. }
  96. }
  97. exports.SchemaManager = SchemaManager;
  98. //# sourceMappingURL=schemaManager.js.map