Skip to content

Commit 2df7e10

Browse files
committed
Add apollo to the reactotron example app.
1 parent e39a4ff commit 2df7e10

23 files changed

+1178
-78
lines changed

apps/example-app/app/app.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ import { useFonts } from "expo-font"
2222
import React from "react"
2323
import { initialWindowMetrics, SafeAreaProvider } from "react-native-safe-area-context"
2424
import * as Linking from "expo-linking"
25-
import { useInitialRootStore } from "app/mobxStateTree"
25+
import { useInitialRootStore } from "app/stores/mobxStateTree"
2626
import { AppNavigator, useNavigationPersistence } from "app/navigators"
2727
import { ErrorBoundary } from "app/screens/ErrorScreen/ErrorBoundary"
2828
import * as storage from "app/utils/storage"
2929
import { customFontsToLoad } from "app/theme"
3030
import Config from "app/config"
3131
import { GestureHandlerRootView } from "react-native-gesture-handler"
3232
import { StatusBar, ViewStyle } from "react-native"
33-
import { store } from "app/redux"
33+
import { store } from "app/stores/redux"
3434
import { Provider as ReduxProvider } from "react-redux"
35+
import { ApolloProvider } from "@apollo/client"
36+
import { client as apolloClient } from "app/stores/apollo"
3537

3638
export const NAVIGATION_PERSISTENCE_KEY = "NAVIGATION_STATE"
3739

@@ -101,17 +103,19 @@ function App(props: AppProps) {
101103
// otherwise, we're ready to render the app
102104
return (
103105
<ReduxProvider store={store}>
104-
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
105-
<ErrorBoundary catchErrors={Config.catchErrors}>
106-
<GestureHandlerRootView style={$container}>
107-
<AppNavigator
108-
linking={linking}
109-
initialState={initialNavigationState}
110-
onStateChange={onNavigationStateChange}
111-
/>
112-
</GestureHandlerRootView>
113-
</ErrorBoundary>
114-
</SafeAreaProvider>
106+
<ApolloProvider client={apolloClient}>
107+
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
108+
<ErrorBoundary catchErrors={Config.catchErrors}>
109+
<GestureHandlerRootView style={$container}>
110+
<AppNavigator
111+
linking={linking}
112+
initialState={initialNavigationState}
113+
onStateChange={onNavigationStateChange}
114+
/>
115+
</GestureHandlerRootView>
116+
</ErrorBoundary>
117+
</SafeAreaProvider>
118+
</ApolloProvider>
115119
</ReduxProvider>
116120
)
117121
}

apps/example-app/app/devtools/ReactotronConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import { ArgType } from "reactotron-core-client"
1010
import { mst } from "reactotron-mst"
1111
import apisaucePlugin from "reactotron-apisauce"
1212
import { reactotronRedux } from "reactotron-redux"
13+
import apolloPlugin from "reactotron-apollo-client"
1314

1415
import { clear } from "app/utils/storage"
1516
import { goBack, resetRoot, navigate } from "app/navigators/navigationUtilities"
1617

1718
import { Reactotron } from "./ReactotronClient"
19+
import { client } from "../stores/apollo" // <--- update this location
1820

1921
const reactotron = Reactotron.configure({
2022
name: require("../../package.json").name,
@@ -29,8 +31,9 @@ const reactotron = Reactotron.configure({
2931
mst({
3032
/** ignore some chatty `mobx-state-tree` actions */
3133
filter: (event) => /postProcessSnapshot|@APPLY_SNAPSHOT/.test(event.name) === false,
32-
}),
34+
})
3335
)
36+
.use(apolloPlugin({ apolloClient: client }))
3437

3538
if (Platform.OS !== "web") {
3639
reactotron.setAsyncStorageHandler?.(AsyncStorage)

apps/example-app/app/navigators/AppNavigator.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type AppStackParamList = {
3737
MobxStateTree: undefined
3838
AsyncStorage: undefined
3939
Redux: undefined
40+
Apollo: undefined
4041
}
4142

4243
/**
@@ -103,6 +104,7 @@ const AppStack = function AppStack() {
103104
options={{ title: "Async Storage" }}
104105
/>
105106
<Stack.Screen name="Redux" component={Screens.ReduxScreen} />
107+
<Stack.Screen name="Apollo" component={Screens.ApolloScreen} />
106108
</Stack.Group>
107109
</Stack.Navigator>
108110
)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import React from "react"
2+
import { FlatList, TextStyle, View, ViewStyle } from "react-native"
3+
import { ListItem, Text } from "app/components"
4+
import { AppStackScreenProps } from "app/navigators"
5+
import { colors, spacing } from "app/theme"
6+
import { useSafeAreaInsetsStyle } from "app/utils/useSafeAreaInsetsStyle"
7+
import { gql, useQuery } from "@apollo/client"
8+
9+
const CHAPTERS_QUERY = gql`
10+
query Chapters {
11+
chapters {
12+
id
13+
number
14+
title
15+
}
16+
}
17+
`
18+
19+
const ChapterItem = ({
20+
chapter,
21+
onPress,
22+
}: {
23+
chapter: { id: number; number: number; title: string }
24+
onPress?: () => void
25+
}) => {
26+
const { number, title } = chapter
27+
let header, subheader
28+
29+
if (number) {
30+
header = `Chapter ${number}`
31+
subheader = ` - ${title}`
32+
} else {
33+
header = title
34+
subheader = ""
35+
}
36+
37+
return (
38+
<ListItem
39+
text={`${header}${subheader}`}
40+
onPress={onPress}
41+
containerStyle={{ paddingHorizontal: spacing.sm }}
42+
/>
43+
)
44+
}
45+
46+
interface ApolloScreenProps extends AppStackScreenProps<"Apollo"> {}
47+
48+
export const ApolloScreen: React.FC<ApolloScreenProps> = function ApolloScreen() {
49+
const { data, loading } = useQuery(CHAPTERS_QUERY)
50+
51+
const $bottomContainerInsets = useSafeAreaInsetsStyle(["bottom"])
52+
53+
return (
54+
<FlatList
55+
style={$container}
56+
contentContainerStyle={$bottomContainerInsets}
57+
data={loading ? [] : data.chapters}
58+
renderItem={({ item }) => (
59+
<ChapterItem
60+
chapter={item}
61+
// onPress={() => navigation.navigate("Chapter", { chapter: item })}
62+
/>
63+
)}
64+
ListHeaderComponent={() => {
65+
return (
66+
<View>
67+
<Text
68+
text="Reactotron can intercept and inspect Apollo client queries"
69+
style={$subheading}
70+
preset="subheading"
71+
/>
72+
<Text text="Like this example from the GraphQL example app:" style={$subheading} />
73+
<View style={$bottomBorder} />
74+
</View>
75+
)
76+
}}
77+
keyExtractor={(chapter) => chapter.id.toString()}
78+
/>
79+
)
80+
}
81+
82+
const $container: ViewStyle = {
83+
flex: 1,
84+
backgroundColor: colors.background,
85+
}
86+
87+
const $text: TextStyle = {
88+
color: colors.text,
89+
}
90+
const $subheading: TextStyle = {
91+
...$text,
92+
margin: spacing.sm,
93+
}
94+
95+
const $bottomBorder: ViewStyle = {
96+
borderBottomWidth: 1,
97+
borderBottomColor: colors.text,
98+
marginHorizontal: spacing.sm,
99+
}

apps/example-app/app/screens/ErrorGeneratorScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { useDispatch } from "react-redux"
44
import { Button, Text } from "app/components"
55
import type { AppStackScreenProps } from "app/navigators"
66
import { colors, spacing } from "app/theme"
7-
import type { AppDispatch } from "app/redux"
8-
import { throwAnError, throwErrorAsync } from "app/redux/errorSlice"
7+
import type { AppDispatch } from "app/stores/redux"
8+
import { throwAnError, throwErrorAsync } from "app/stores/redux/errorSlice"
99
import { useSafeAreaInsetsStyle } from "app/utils/useSafeAreaInsetsStyle"
1010

1111
interface ErrorGeneratorScreenProps extends AppStackScreenProps<"ErrorGenerator"> {}

apps/example-app/app/screens/MobxStateTreeScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ScrollView, TextStyle, View, ViewStyle } from "react-native"
44
import { Button, Text } from "app/components"
55
import { AppStackScreenProps } from "app/navigators"
66
import { colors, spacing } from "app/theme"
7-
import { useStores } from "app/mobxStateTree"
7+
import { useStores } from "app/stores/mobxStateTree"
88
import { Repo } from "app/components/Repo"
99
import { useSafeAreaInsetsStyle } from "app/utils/useSafeAreaInsetsStyle"
1010

apps/example-app/app/screens/ReduxScreen.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { Button, Text } from "app/components"
55
import { Repo } from "app/components/Repo"
66
import { AppStackScreenProps } from "app/navigators"
77
import { colors, spacing } from "app/theme"
8-
import type { AppDispatch, RootState } from "app/redux"
9-
import { fetchAsync, reset as repoReset } from "app/redux/repoSlice"
10-
import { changeSize, changeSpeed, reset as logoReset } from "app/redux/logoSlice"
8+
import type { AppDispatch, RootState } from "app/stores/redux"
9+
import { fetchAsync, reset as repoReset } from "app/stores/redux/repoSlice"
10+
import { changeSize, changeSpeed, reset as logoReset } from "app/stores/redux/logoSlice"
1111
import { useSafeAreaInsetsStyle } from "app/utils/useSafeAreaInsetsStyle"
1212

1313
interface ReduxScreenProps extends AppStackScreenProps<"Redux"> {}

apps/example-app/app/screens/WelcomeScreen.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ export const WelcomeScreen: React.FC<WelcomeScreenProps> = function WelcomeScree
7272
navigation.navigate("Redux")
7373
}}
7474
/>
75+
<ListItem
76+
text="Apollo Client"
77+
onPress={() => {
78+
navigation.navigate("Apollo")
79+
}}
80+
/>
7581
</View>
7682
</ScrollView>
7783
</SafeAreaView>

apps/example-app/app/screens/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from "./MobxStateTreeScreen"
77
export * from "./ReduxScreen"
88
export * from "./ErrorGeneratorScreen"
99
export * from "./AsyncStorageScreen"
10+
export * from "./ApolloScreen"
1011

1112
export * from "./ErrorScreen/ErrorBoundary"
1213
// export other screens here
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { ApolloClient, InMemoryCache } from "@apollo/client"
2+
3+
const cache = new InMemoryCache()
4+
5+
export const client = new ApolloClient({
6+
uri: "https://api.graphql.guide/graphql",
7+
cache,
8+
defaultOptions: { watchQuery: { fetchPolicy: "cache-and-network" } },
9+
})

0 commit comments

Comments
 (0)