🔏
Azure Serverless Quickstart
  • Introduction
  • Initial Setup
    • Workstation Installs
    • Codebase
      • Directory Structure
      • User Interface Project
        • Configuring StoryBook
        • Configure Tailwind
        • Configure Craco
        • -Architectural Decision Log
      • Data Access Project
        • DDD
      • Untitled
      • Full Stack Debugging
      • Creating GitHub Project
    • Infrastructure
      • Configure Session Behavior
      • Create AAD Tenant
      • Resource Group
      • Create AAD B2C Instance
        • Identity Experience Framework
        • Configure Session Behavior
      • Storage Account & CDN
        • CDN Rules
        • Configure Azure BLOB Storage
      • App Insights
        • Create AppInsight Account
        • Apollo GraphQL App Insights Configuration
      • CosmosDB
      • Twilio SendGrid
      • KeyVault
      • Function
      • Function App Settings
      • Front Door
      • DevOps
      • Optional Items
        • Azure Data Factory
      • Azure Event Hub
    • CICD and Source Control
      • Azure DevOps
      • SonarCloud
        • Incorporate into Yaml
      • Chromatic
      • User Interface YAML
      • CICD for Data Access
        • Create Pipeline
        • Data Access YAML
  • Application Structure
    • Connect Apollo
      • Apollo Overview
      • Create Apollo Component
    • MongoDB Integration
      • Mappings
      • Directory Structure
      • Apollo Connection
      • Models
      • Queries Mutations and Subscriptions
      • Caching Reponses
    • Integrating GraphQL Tools
      • GraphQL Code Generator
    • Feature Flags
      • Flag Structure & Storage
      • Website Integration
      • Apollo Integration
      • Tips and Techniques
      • Alternative Approaches
    • React Router
    • Adding Authentication
      • Create AAD Applications
      • Configure AAD For External Identities
      • Adding MSAL And React
      • Add MSAL to the build
      • Add MSAL to ApolloClient
      • Add MSAL to ApolloServer
    • Ant Design
    • Jest Tests
  • Azure Active Directory Business-to-Consumer (AD B2C)
    • Introduction
    • How to navigate through AD B2C documentation
    • Localization
    • Abbreviations
    • Azure AD B2C Extension
  • Cognitive Search
  • Cost Analysis
  • Technical Architecture
    • Identity and Access Control
  • Adding Functionality
    • Google Analytics
      • Create Analytics
    • DAPR
      • DAPR setup
      • DAPR Services (ignore for now)
        • Identity
  • Patterns and Practices
    • Idempotent Messages
    • Pathways
    • DDD
      • Initial Setup
        • Aggregate Root
        • Entity
        • Value Object
      • Field Types
        • Primitive Types
        • Non-Primitive Types
          • Types.DocumentArray
          • PopulatedDoc
          • Custom Types
      • Example Walkthrough
  • Open Items
    • Issue Tracking
  • Helpful Resources
  • DDD
    • Page 1
  • Experimental
    • StaticWebApp
    • Azure Maps
Powered by GitBook
On this page

Was this helpful?

  1. Application Structure
  2. MongoDB Integration

Apollo Connection

In terminal go to azure-quickstart/data-access and issue the following command:

npm install mongoose

in azure-quickstart/data-access create the following directory structure:

shared-code/data-sources/cosmos-db/

Add the a connection file for connecting to CosmosDB (MongoDB):

azure-quickstart/data-access/shared-code/data-sources/cosmos-db/connect.ts
import mongoose from 'mongoose';

async function connect() {
    try {
        await mongoose.connect("mongodb://"+process.env.COSMOSDB_HOST+":"+process.env.COSMOSDB_PORT+"/"+process.env.COSMOSDB_DBNAME+"?ssl=true&replicaSet=globaldb&retryWrites=false", {
            auth: {
                user: process.env.COSMODDB_USER,
                password: process.env.COSMOSDB_PASSWORD
            },
            tlsInsecure:   process.env.NODE_ENV === "development", //only true for local developent - required for Azure Cosmos DB emulator
            useNewUrlParser: true, 
            useUnifiedTopology: true,
            useFindAndModify: false,
            poolSize: Number(process.env.COSMOSDB_POOL_SIZE)
        }).then(() => console.log(`🗄️ Successfully connected Mongoose to ${mongoose.connection.name} 🗄️`));
    } catch (error) {
        console.log(`🔥 An error ocurred when trying to connect Mongoose with ${mongoose.connection.name} 🔥`)
        throw error;
    }
}

export default connect;

Add an import and start the cosmos connection in the main Apollo Function:

azure-quickstart/data-access/shared-code/data-sources/cosmos-db/connect.ts
.../*<<other import statements>>*/...
import cosmosdbconnect from '../sharedCode/datasources/cosmosDb/connect';


/* IIFE for connecting to CosmosDB */
(async () => {
  try {
    await cosmosdbconnect();
  } catch (cosmosDbConnectError) {
    appInsightsClient.trackEvent({name: "COSMOSDB CONNECTION", properties: {customProperty: "Cannot Connect to Cosmos", details: cosmosDbConnectError}});
    appInsightsClient.trackException(cosmosDbConnectError)
  }
})();

.../*<<ApploServer startup>> */..

Add the following the localSettings.json file, be sure to replace the values in <<>> with values from your Cosmos DB settings.

azure-quickstart/data-access/localSettings.json
{
  ...
  "Values": {
    ...
    "COSMODDB_USER": "sharethrift",
		"COSMOSDB_DBNAME": "sharethrift",
		"COSMOSDB_HOST": "sharethrift<<RANDOM NUMBER>>.mongo.cosmos.azure.com",
		"COSMOSDB_PASSWORD": "<<YOUR PASSWORD>>",
		"COSMOSDB_PORT": 10255,
		"COSMOSDB_POOL_SIZE" : 10,
    ...
  }
  ...
}

PreviousDirectory StructureNextModels

Last updated 3 years ago

Was this helpful?