BlueBase
  • 💧Introduction
  • Tutorial
    • 1. Getting Started
      • 1.1 Setup
      • 1.2 Add Plugins
      • 1.3 Create Custom Plugin
    • 2. Backend API
      • 2.1 Create Backend API
      • 2.2 Setup Apollo Client
      • 2.3 Generate Typescript Interfaces
    • 3. Create Screens
      • 3.1 Pending Tasks Screen
      • 3.2 Edit Task Screen
      • 3.3 Task Create Screen
      • 3.4 Tab Navigation
    • 4. CRUD Operations
      • 4.1 Creating Tasks
      • 4.2 Reading Tasks
      • 4.3 Updating Tasks
      • 4.4 Deleting Tasks
    • 5. Enhancements
      • 5.1 Internationalisation
      • 5.2 Theming
      • 5.3 Dynamic Images
      • 5.4 Settings & Configurations
      • User Management
  • Key Concepts
    • 🎡Lifecycle Events
    • â›Šī¸Main App Layout
    • 🔌Plugins
      • Plugin API
      • Register a Plugin
      • Making a Plugin Configurable
      • Developing an Analytics Plugin
      • Developing a Logger Plugin
      • Developing a Theme Plugin
    • 🚇Filters
    • 🎁Components
      • Components API
      • Registering a Component
      • Accessing Components
      • Higher Order Components
    • 🎨Themes
      • Consuming Selected Theme
      • Customise Themes
      • Customise Components
      • Theme Configs
      • Theme Structure
    • đŸŽ›ī¸Configs
  • API
    • 📈Analytics
    • 📔Logger
    • đŸ“ĻBlueBase Modules
    • Registry
  • Guides
    • âœ‚ī¸Code Splitting
    • đŸ‘ŊMigrating to V8
  • Components
    • ActivityIndicator
    • BlueBase
      • BlueBaseApp 📌
      • BlueBaseConsumer 📌
      • BlueBaseFilter 📌
      • ThemeConsumer 📌
    • Button
    • ComponentState 📌
    • EmptyState 📌
    • ErrorState 📌
    • Icons
      • Icon
      • DynamicIcon 📌
      • PluginIcon 📌
    • JsonSchema 📌
    • LoadingState 📌
    • Noop 📌
    • Observers
      • DataObserver 📌
      • ErrorObserver 📌
      • HoverObserver 📌
      • WaitObserver 📌
    • StatefulComponent 📌
    • Typography
    • View
Powered by GitBook
On this page
  • Step 1: Install the BlueBase Apollo plugin
  • Step 2: Use the plugin
  • Step 3: Create Apollo Configs
  • Step 4: Use the configs
  • Step 5: Bypass Apollo Bug

Was this helpful?

Export as PDF
  1. Tutorial
  2. 2. Backend API

2.2 Setup Apollo Client

Previous2.1 Create Backend APINext2.3 Generate Typescript Interfaces

Last updated 3 years ago

Was this helpful?

To consume our GraphQL API on the app, we will be using the popular Graphql Client. Luckily, we already have an Apollo plugin available for BlueBase that takes care of all the setup.

Step 1: Install the BlueBase Apollo plugin

yarn add --dev @apollo/client @bluebase/plugin-apollo graphql

Step 2: Use the plugin

Same as the last time, we import the apollo plugin:

plugins.ts
import BlueBasePluginApollo from '@bluebase/plugin-apollo';
import BlueBasePluginJsonSchemaComponents from '@bluebase/plugin-json-schema-components';
import BlueBasePluginLauncher from '@bluebase/plugin-launcher';
import BlueBasePluginMaterialUI from '@bluebase/plugin-material-ui';
import BlueBasePluginReactRouter from '@bluebase/plugin-react-router';
import BlueBasePluginResponsiveGrid from '@bluebase/plugin-responsive-grid';
import BlueBasePluginSettingsApp from '@bluebase/plugin-settings-app';
import { MaterialCommunityIcons } from '@bluebase/plugin-vector-icons';

import Plugin from './src';

export const plugins = [
	BlueBasePluginApollo,
	BlueBasePluginJsonSchemaComponents,
	BlueBasePluginLauncher,
	BlueBasePluginMaterialUI,
	BlueBasePluginReactRouter,
	BlueBasePluginResponsiveGrid,
	MaterialCommunityIcons,

	Plugin,
	BlueBasePluginSettingsApp,
];
plugins.native.ts
import BlueBasePluginApollo from '@bluebase/plugin-apollo';
import BlueBasePluginJsonSchemaComponents from '@bluebase/plugin-json-schema-components';
import BlueBasePluginLauncher from '@bluebase/plugin-launcher';
import BlueBasePluginReactNativePaper from '@bluebase/plugin-react-native-paper';
import BlueBasePluginReactNavigation from '@bluebase/plugin-react-navigation';
import BlueBasePluginResponsiveGrid from '@bluebase/plugin-responsive-grid';
import BlueBasePluginSettingsApp from '@bluebase/plugin-settings-app';
import { MaterialCommunityIcons } from '@bluebase/plugin-vector-icons';

import Plugin from './src';

export const plugins = [
	BlueBasePluginApollo,
	BlueBasePluginJsonSchemaComponents,
	BlueBasePluginLauncher,
	BlueBasePluginReactNativePaper,
	BlueBasePluginReactNavigation,
	BlueBasePluginResponsiveGrid,
	MaterialCommunityIcons,

	Plugin,
	BlueBasePluginSettingsApp,
];

Step 3: Create Apollo Configs

Create the following file in the root folder:

configs.ts
export const configs = {

	// Apollo Graphql Configs
	'plugin.apollo.httpLinkOptions': {
		uri: [[API_URL]] + '/graphql/v1',
		headers: {
			apiKey: [[API_KEY]]
		}
	},

};

Replace [[API_URL]] and [[API_KEY]] with the values relevant to your project.

Step 4: Use the configs

Similar to how we use the plugins, we need to pass our configs object as a prop to the BlueBaseApp component.

Change the contents of the App.tsx to match below:

App.tsx
import 'react-native-gesture-handler';

import { BlueBaseApp } from '@bluebase/core';
import React from 'react';

import { configs } from './configs';
import { plugins } from './plugins';

export default function App() {
	return (
		<BlueBaseApp configs={configs} plugins={plugins} />
	);
}

Step 5: Bypass Apollo Bug

metro.config.js
// Learn more https://docs.expo.io/guides/customizing-metro

const { getDefaultConfig } = require('@expo/metro-config');

const config = getDefaultConfig(__dirname);

// https://github.com/facebook/metro/issues/535
// https://github.com/apollographql/apollo-client/releases/tag/v3.5.4
config.resolver.sourceExts = process.env.RN_SRC_EXT
	? [...process.env.RN_SRC_EXT.split(',').concat(config.resolver.sourceExts), 'cjs'] // <-- cjs added here
	: [...config.resolver.sourceExts, 'cjs']; // <-- cjs added here

// https://docs.expo.dev/bare/installing-updates/
config.transformer.assetPlugins = [
	...config.transformer.assetPlugins,
	'expo-asset/tools/hashAssetFiles',
];
module.exports = config;

This is it. Our App is configured to use the GraphQL API through the Apollo client.

Now we need to input our API URL as well as the API Token into the Apollo client. We do this by using .

At the time of writing this tutorial, the latest version of Apollo Client (v3.5.4) has compatibility an with react-native. Add the following file in the root folder to bypass it.

Apollo
BlueBase Configs
issue