🔏
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. Adding Authentication

Add MSAL to ApolloClient

Begin by creating a new react component to wire in ApolloClient authenticated with MSAL

Start by creating a new component:

/azure-quickstart/ui/src/components/core/apollo-connection.tsx
import React, { FC,useEffect } from 'react';
import App from '../../App';
import {
  ApolloClient,  
  ApolloProvider,
  createHttpLink,
  InMemoryCache,
  from
} from '@apollo/client';
import {setContext} from '@apollo/client/link/context';
import useMsal from './msal/use-msal';

const ApolloConnection: FC<any> = () => {
  const { getAuthToken, isLoggedIn } = useMsal();

  const withToken = setContext(async(_,{headers}) =>{
    const token = await getAuthToken();
    return{
      headers: {
        ...headers,
        Authorization: token ? `Bearer ${token}` : null
      }
    }
  }) 
 
  const httpLink = createHttpLink({
    uri: `${process.env.REACT_APP_FUNCTION_ENDPOINT}/api/graphql`
  });

  const client = new ApolloClient({
    link: from([withToken, httpLink]),
    cache: new InMemoryCache(),
  });

  useEffect(() => {
    if(!isLoggedIn){
      (async () => {
        await client.resetStore() //clear Apollo cache when user loggs off
      })()
    }
  },[isLoggedIn]) // eslint-disable-line react-hooks/exhaustive-deps

  return (
    <ApolloProvider client={client}>
      <App />
    </ApolloProvider>
  );
};

export default ApolloConnection;

Add an import for the new ApolloConnection component:

/azure-quickstart/ui/src/index.tsx
/** other imports **/
import ApolloConnection from './components/core/apollo-connection';

Change the component: <App /> to <ApolloConnection /> so that it looks as it does below:

/azure-quickstart/ui/src/index.tsx
    <MsalProvider config={msalProviderConfig}>
      <ApolloConnection />
    </MsalProvider>

PreviousAdd MSAL to the buildNextAdd MSAL to ApolloServer

Last updated 4 years ago

Was this helpful?