Adding Apollo
To view this content, buy the book! 😃🙏
Or if you’ve already purchased.
Adding Apollo
If you’re jumping in here,
git checkout 0_1.0.0
(tag 0_1.0.0, or compare 0...1)
Just like we did the React chapter, we create a client instance and wrap our app in an <ApolloProvider>
:
import { ApolloClient, InMemoryCache, ApolloProvider } from '@apollo/client'
const client = new ApolloClient({
uri: 'https://api.graphql.guide/graphql',
cache: new InMemoryCache(),
})
export default function App() {
return (
<ApolloProvider client={client}>
<NavigationContainer>
...
</NavigationContainer>
</ApolloProvider>
)
}
Now in our HomeScreen
component, we can make a query for the chapter list and display it:
import { Text, FlatList, Pressable } from 'react-native'
import { gql, useQuery } from '@apollo/client'
import styles from './styles'
const CHAPTERS_QUERY = gql`
query Chapters {
chapters {
id
number
title
}
}
`
const ChapterItem = ({ chapter }) => {
const { number, title } = chapter
let header, subheader
if (number) {
header = `Chapter ${number}`
subheader = title
} else {
header = title
}
return (
<Pressable style={styles.item}>
<Text style={styles.header}>{header}</Text>
{subheader && <Text style={styles.subheader}>{subheader}</Text>}
</Pressable>
)
}
export default () => {
const { data, loading } = useQuery(CHAPTERS_QUERY)
if (loading) {
return <Loading />
}
return (
<FlatList
data={data.chapters}
renderItem={({ item }) => (
<ChapterItem chapter={item} />
)}
keyExtractor={(chapter) => chapter.id.toString()}
/>
)
}
FlatList is a core React Native component for displaying lists. renderItem
is called to render each item in the data
array prop, and keyExtractor
returns a unique string to use for each item’s key
prop.
When we edit our code with the app open, it live reloads. However, the query gets stuck in a loading state, so we just see the spinner. To do a full reload, open the developer menu and select “Reload.”
How to open the developer menu:
- iOS Device: Shake the device a little bit.
- iOS Simulator: Hit
Ctrl+Cmd+Z
on a Mac in the emulator to simulate the shake gesture, or pressCmd+D
. - Android Device: Shake the device vertically a little bit.
- Android Emulator: Either hit
Cmd+M
, or runadb shell input keyevent 82
in your terminal window.
When we do a full reload, we briefly see the loading indicator, and then: