Use Apollo Client with Flex UI
Flex UI uses Apollo Client and an ApolloProvider
to internally communicate with Twilio's GraphQL API. This architecture allows both native and custom Flex UI components to exchange data using GraphQL.
Sometimes, you might want your custom Flex UI components to call a different GraphQL API, instead of Twilio's GraphQL API.
If you attempt to wrap the Flex UI RootContainer around a custom ApolloProvider
, Flex still defaults to Twilio's GraphQL API. That's because Flex's ApolloProvider
is already in place, and anything inside it uses Twilio's API as the default client. In this case, your customization won't work as expected.
If you want to use your own custom GraphQL service inside a Flex UI component, use the client
option in Apollo hooks, like useQuery
. This solution lets you specify which Apollo Client instance to use for that request. For more details, see Apollo's documentation on the useQuery
API.
If you're using a custom GraphQL API inside a Flex UI plugin, use the following approach. Inside your plugin component, you can create and use a custom Apollo Client.
1// MyCustomGraphQLComponent.js23import { ApolloClient, ApolloProvider, gql, InMemoryCache, useQuery } from "@apollo/client";4import * as React from "react";56const customerGraphQLClient = new ApolloClient({7uri: 'https://my-custom-graphql-api.com/',8cache: new InMemoryCache()9});1011const CUSTOMER_QUERY = gql`12query GetLocations {13locations {14id15name16description17}18}19`;2021const CustomGraphQLChildComponent = () => {22const { data, error, loading } = useQuery(CUSTOMER_QUERY);2324if (loading) {25return <div>Loading...</div>;26}27if (error) {28return <div>Error: {(error as any).message}</div>;29}3031return (32<div>33<pre>data: {JSON.stringify(data, null, 2)}</pre>34</div>35);36};3738export const MyCustomGraphQLComponent = () => (39<ApolloProvider client={customerGraphQLClient}>40<CustomGraphQLChildComponent />41</ApolloProvider>42);
1// GraphQLComponentPlugin.js2import { FlexPlugin } from 'flex-plugin';3import React from 'react';4import { MyCustomGraphQLComponent } from './MyCustomGraphQLComponent';56const PLUGIN_NAME = 'GraphQLComponent';78export default class GraphQLComponentPlugin extends FlexPlugin {9constructor() {10super(PLUGIN_NAME);11}1213/**14* This code runs when your plugin starts15* Use this to modify any UI components or attach to the actions framework16*17* @param flex { typeof import('@twilio/flex-ui') }18* @param manager { import('@twilio/flex-ui').Manager }19*/20init(flex, manager) {21flex.TaskInfoPanel.Content.add(<MyCustomGraphQLComponent' key="gql-component"/>);22}23}24
If you're hosting Flex UI yourself, you can define a custom Apollo Client and use it directly inside your components.
1import React from 'react';2import * as Flex from '@twilio/flex-ui';3import { ApolloClient, InMemoryCache, gql } from '@apollo/client';4import { ApolloProvider, useQuery } from '@apollo/client/react';56const customerGraphQLClient = new ApolloClient({7uri: 'https://my-custom-graphql-api.com/',8cache: new InMemoryCache(),9});1011const CUSTOMER_QUERY = gql`12query GetLocations {13locations {14id15name16description17}18}19`;2021const MyCustomGraphQLComponent = () => {22const { loading, error, data } = useQuery(CUSTOMER_QUERY, {23client: customerGraphQLClient, // This code is required so the component queries your custom GraphQL endpoint instead of Twilio's24});2526if (loading) {27return <div>Loading...</div>;28}29if (error) {30return <div>Error: {(error as any).message}</div>;31}3233return (34<div>35<pre>data: {JSON.stringify(data, null, 2)}</pre>36</div>37);38};3940export const App = ({ manager }) => {41Flex.TaskInfoPanel.Content.add(<MyCustomGraphQLComponent key="gql-component"/>);4243return (44<ApolloProvider client={customerGraphQLClient}>45<Flex.ContextProvider manager={manager}>46<Flex.RootContainer />47</Flex.ContextProvider>48</ApolloProvider>49);50}51}5253export default App;