Skip to content

Commit cc31f0e

Browse files
author
RobJellinghaus
committed
FINALLY got Relay test passing.
1 parent 1178071 commit cc31f0e

13 files changed

Lines changed: 332 additions & 521 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ node_modules/
1818
# Logs
1919
fullstack.log
2020
playwright-server.log
21+
22+
# Debug screenshots
23+
*-debug.png

frontend/src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { RelayEnvironmentProvider } from 'react-relay'
33
import { GraphQLPage } from './containers/GraphQLPage'
44
import { useAuth, useAuthCheck } from './hooks/useAuth'
55
import { useAuthenticatedRelayEnvironment } from './hooks/useAuthenticatedRelayEnvironment'
6+
import { RelayEnvironmentContext } from './hooks/useRelayEnvironmentContext'
67
import { AccountPage } from './containers/AccountPage'
78
import { LoginPage } from './containers/LoginPage'
89
import { OauthLoginResultPage } from './containers/OauthLoginResultPage'
@@ -14,7 +15,6 @@ import React, { Suspense } from 'react'
1415
import './App.css'
1516
import { Home } from './containers/Home'
1617
import { Todos } from './containers/Todo'
17-
import { TodosGraphQL } from './containers/TodoGraphQL'
1818
import { TodoGraphQLRelay } from './containers/TodoGraphQLRelay'
1919
import { Files } from './containers/Files'
2020
import { Route, useNavigate, Routes } from 'react-router-dom'
@@ -31,12 +31,12 @@ const App = () => {
3131
// @ts-ignore
3232
return (
3333
<RelayEnvironmentProvider environment={relayEnvironment}>
34-
<div className="App">
34+
<RelayEnvironmentContext.Provider value={relayEnvironment}>
35+
<div className="App">
3536
<div className="App-nav-header">
3637
<div style={{ display: 'flex', flex: 1 }}>
3738
<a className="NavButton" onClick={() => navigate('/')}>Home</a>
3839
<a className="NavButton" onClick={() => navigate('/todos')}>Todos (REST)</a>
39-
<a className="NavButton" onClick={() => navigate('/todos-graphql')}>Todos (GraphQL)</a>
4040
<a className="NavButton" onClick={() => navigate('/todos-relay')}>Todos (Relay)</a>
4141
<a className="NavButton" onClick={() => navigate('/files')}>Files</a>
4242
{/* CRA: left-aligned nav buttons */}
@@ -54,7 +54,6 @@ const App = () => {
5454
<Routes>
5555
<Route path="/" element={<Home />} />
5656
<Route path="/todos" element={<Todos />} />
57-
<Route path="/todos-graphql" element={<TodosGraphQL />} />
5857
{/*
5958
CRITICAL: Relay components that use useLazyLoadQuery MUST be wrapped in <Suspense>
6059
@@ -90,6 +89,7 @@ const App = () => {
9089
</Routes>
9190
</div>
9291
</div>
92+
</RelayEnvironmentContext.Provider>
9393
</RelayEnvironmentProvider>
9494
)
9595
}

frontend/src/containers/Todo.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useState } from 'react'
2+
import { useRelayEnvironment, invalidateRelayTodoCache } from '../hooks/useRelayEnvironmentContext'
23

34
const TodoAPI = {
45
get: async (page: number, size: number) =>
@@ -34,6 +35,9 @@ export const Todos = () => {
3435
const [page, setPage] = useState<number>(0)
3536
const [numPages, setPages] = useState<number>(1)
3637
const [processing, setProcessing] = useState<boolean>(false)
38+
39+
// Get Relay environment for cache invalidation
40+
const relayEnvironment = useRelayEnvironment()
3741

3842
const createTodo = async (todo: string) => {
3943
setProcessing(true)
@@ -42,6 +46,10 @@ export const Todos = () => {
4246
setTodos(todos)
4347
setCreatedTodo(createdTodo)
4448
setText('')
49+
50+
// Invalidate Relay cache so the Relay component picks up the changes
51+
invalidateRelayTodoCache(relayEnvironment)
52+
4553
setProcessing(false)
4654
}
4755

@@ -51,13 +59,21 @@ export const Todos = () => {
5159
setTodos(await TodoAPI.get(page, pageSize))
5260
setText('')
5361
editTodo(null)
62+
63+
// Invalidate Relay cache so the Relay component picks up the changes
64+
invalidateRelayTodoCache(relayEnvironment)
65+
5466
setProcessing(false)
5567
}
5668

5769
const deleteTodo = async (todo: Todo) => {
5870
setProcessing(true)
5971
await TodoAPI.delete(todo.id)
6072
setTodos(await TodoAPI.get(page, pageSize))
73+
74+
// Invalidate Relay cache so the Relay component picks up the changes
75+
invalidateRelayTodoCache(relayEnvironment)
76+
6177
setProcessing(false)
6278
}
6379

frontend/src/containers/TodoGraphQL.tsx

Lines changed: 0 additions & 217 deletions
This file was deleted.

0 commit comments

Comments
 (0)