Skip to content

Commit 0839294

Browse files
committed
Add Apollo and display chapters on the home screen
1 parent e526142 commit 0839294

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

‎App.js‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ import React from 'react'
22
import{StatusBar}from'expo-status-bar'
33
import{NavigationContainer}from'@react-navigation/native'
44
import{createStackNavigator}from'@react-navigation/stack'
5+
import{ApolloClient,InMemoryCache,ApolloProvider}from'@apollo/client'
56

67
importHomeScreenfrom'./src/HomeScreen'
78
import{screenOptions}from'./src/styles'
89

910
constStack=createStackNavigator()
1011

12+
constclient=newApolloClient({
13+
uri: 'https://api.graphql.guide/graphql',
14+
cache: newInMemoryCache(),
15+
})
16+
1117
exportdefaultfunctionApp(){
1218
return(
13-
<NavigationContainer>
14-
<Stack.NavigatorinitialRouteName="Home"screenOptions={screenOptions}>
15-
<Stack.Screen
16-
name="Home"
17-
component={HomeScreen}
18-
options={{title: '📖 The GraphQL Guide'}}
19-
/>
20-
</Stack.Navigator>
21-
<StatusBarstyle="light"/>
22-
</NavigationContainer>
19+
<ApolloProviderclient={client}>
20+
<NavigationContainer>
21+
<Stack.NavigatorinitialRouteName="Home"screenOptions={screenOptions}>
22+
<Stack.Screen
23+
name="Home"
24+
component={HomeScreen}
25+
options={{title: '📖 The GraphQL Guide'}}
26+
/>
27+
</Stack.Navigator>
28+
<StatusBarstyle="light"/>
29+
</NavigationContainer>
30+
</ApolloProvider>
2331
)
2432
}

‎src/HomeScreen.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
importReactfrom'react'
2+
import{Text,FlatList,Pressable}from'react-native'
3+
import{gql,useQuery}from'@apollo/client'
24

35
importLoadingfrom'./Loading'
6+
importstylesfrom'./styles'
7+
8+
constCHAPTERS_QUERY=gql`
9+
query Chapters{
10+
chapters{
11+
id
12+
number
13+
title
14+
}
15+
}
16+
`
17+
18+
constChapterItem=({ chapter })=>{
19+
const{ number, title }=chapter
20+
letheader,subheader
21+
22+
if(number){
23+
header=`Chapter ${number}`
24+
subheader=title
25+
}else{
26+
header=title
27+
}
28+
29+
return(
30+
<Pressablestyle={styles.item}>
31+
<Textstyle={styles.header}>{header}</Text>
32+
{subheader&&<Textstyle={styles.subheader}>{subheader}</Text>}
33+
</Pressable>
34+
)
35+
}
436

537
exportdefault()=>{
6-
return<Loading/>
38+
const{ data, loading }=useQuery(CHAPTERS_QUERY)
39+
40+
if(loading){
41+
return<Loading/>
42+
}
43+
44+
return(
45+
<FlatList
46+
data={data.chapters}
47+
renderItem={({ item })=><ChapterItemchapter={item}/>}
48+
keyExtractor={(chapter)=>chapter.id.toString()}
49+
/>
50+
)
751
}

0 commit comments

Comments
(0)