mongoose#disconnect TypeScript Examples

The following examples show how to use mongoose#disconnect. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.ts    From graphql-mesh with MIT License 4 votes vote down vote up
async getMeshSource(): Promise<MeshSource> {
    if (this.config.connectionString) {
      connect(this.config.connectionString, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      } as ConnectOptions).catch(e => console.error(e));

      await this.pubsub.subscribe('destroy', () => disconnect());
    }

    const schemaComposer = new SchemaComposer();
    await Promise.all([
      Promise.all(
        this.config.models?.map(async modelConfig => {
          const model = await loadFromModuleExportExpression<Model<Document<any, any, any>>>(modelConfig.path, {
            defaultExportName: modelConfig.name,
            cwd: this.baseDir,
            importFn: this.importFn,
          });
          if (!model) {
            throw new Error(`Model ${modelConfig.name} cannot be imported ${modelConfig.path}!`);
          }
          const modelTC = composeWithMongoose(model, modelConfig.options as any);
          await Promise.all([
            Promise.all(
              modelQueryOperations.map(async queryOperation =>
                schemaComposer.Query.addFields({
                  [`${modelConfig.name}_${queryOperation}`]: modelTC.getResolver(queryOperation),
                })
              )
            ),
            Promise.all(
              modelMutationOperations.map(async mutationOperation =>
                schemaComposer.Mutation.addFields({
                  [`${modelConfig.name}_${mutationOperation}`]: modelTC.getResolver(mutationOperation),
                })
              )
            ),
          ]);
          if (this.config.autoTypeMerging) {
            modelTC.setDirectiveByName('key', {
              selectionSet: /* GraphQL */ `
                {
                  id
                }
              `,
            });
            modelTC.setFieldDirectiveByName(`${modelConfig.name}_dataLoaderMany`, 'merge');
          }
        }) || []
      ),
      Promise.all(
        this.config.discriminators?.map(async discriminatorConfig => {
          const discriminator = await loadFromModuleExportExpression<any>(discriminatorConfig.path, {
            defaultExportName: discriminatorConfig.name,
            cwd: this.baseDir,
            importFn: this.importFn,
          });
          const discriminatorTC = composeWithMongooseDiscriminators(discriminator, discriminatorConfig.options as any);
          await Promise.all([
            Promise.all(
              modelQueryOperations.map(async queryOperation =>
                schemaComposer.Query.addFields({
                  [`${discriminatorConfig.name}_${queryOperation}`]: discriminatorTC.getResolver(queryOperation),
                })
              )
            ),
            Promise.all(
              modelMutationOperations.map(async mutationOperation =>
                schemaComposer.Mutation.addFields({
                  [`${discriminatorConfig.name}_${mutationOperation}`]: discriminatorTC.getResolver(mutationOperation),
                })
              )
            ),
          ]);
          if (this.config.autoTypeMerging) {
            discriminatorTC.setDirectiveByName('key', {
              selectionSet: /* GraphQL */ `
                {
                  id
                }
              `,
            });
            discriminatorTC.setFieldDirectiveByName(`${discriminatorConfig.name}_dataLoaderMany`, 'merge');
          }
        }) || []
      ),
    ]);

    // graphql-compose doesn't add @defer and @stream to the schema
    specifiedDirectives.forEach(directive => schemaComposer.addDirective(directive));

    if (this.config.autoTypeMerging) {
      const defaultStitchingDirectives = stitchingDirectives();
      defaultStitchingDirectives.allStitchingDirectives.forEach(directive => schemaComposer.addDirective(directive));
    }

    const schema = schemaComposer.buildSchema();

    return {
      schema,
    };
  }