fix: Resolve GraphQL DateTime and JSON serialization errors
Fixed two critical GraphQL schema issues preventing dashboard data loading:
**Backend Changes:**
- Changed child.birthDate from DATE to TIMESTAMP type in entity and database
- Updated TypeORM entity (child.entity.ts:23)
- Migrated database column: ALTER TABLE children ALTER COLUMN birth_date TYPE TIMESTAMP
- Added JSON scalar support for activity metadata field
- Installed graphql-type-json package
- Created JSONScalar (src/graphql/scalars/json.scalar.ts)
- Updated Activity.metadata from String to GraphQLJSON type
- Auto-generated schema.gql with JSON scalar definition
**Frontend Changes:**
- Fixed Apollo Client token storage key mismatch
- Changed from 'access_token' to 'accessToken' to match tokenStorage utility
- Enhanced dashboard logging for debugging GraphQL queries
**Database Migration:**
- Converted children.birth_date: DATE → TIMESTAMP
- Preserves existing data (2023-06-01 → 2023-06-01 00:00:00)
Resolves errors:
- "Expected DateTime.serialize() to return non-nullable value, returned: null"
- "String cannot represent value: { ... }" for activity metadata
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -21,86 +21,76 @@ import {
|
||||
import { motion } from 'framer-motion';
|
||||
import { useAuth } from '@/lib/auth/AuthContext';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { trackingApi, DailySummary } from '@/lib/api/tracking';
|
||||
import { childrenApi, Child } from '@/lib/api/children';
|
||||
import { useQuery } from '@apollo/client/react';
|
||||
import { GET_DASHBOARD } from '@/graphql/queries/dashboard';
|
||||
import { format } from 'date-fns';
|
||||
import { useRealTimeActivities } from '@/hooks/useWebSocket';
|
||||
|
||||
export default function HomePage() {
|
||||
const { user, isLoading: authLoading } = useAuth();
|
||||
const router = useRouter();
|
||||
const [children, setChildren] = useState<Child[]>([]);
|
||||
const [selectedChild, setSelectedChild] = useState<Child | null>(null);
|
||||
const [dailySummary, setDailySummary] = useState<DailySummary | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [selectedChildId, setSelectedChildId] = useState<string | null>(null);
|
||||
|
||||
const familyId = user?.families?.[0]?.familyId;
|
||||
// GraphQL query for dashboard data
|
||||
const { data, loading, error, refetch } = useQuery(GET_DASHBOARD, {
|
||||
variables: { childId: selectedChildId },
|
||||
skip: authLoading || !user,
|
||||
fetchPolicy: 'cache-and-network',
|
||||
});
|
||||
|
||||
// Real-time activity handler to refresh daily summary
|
||||
const refreshDailySummary = useCallback(async () => {
|
||||
if (!selectedChild) return;
|
||||
// Log GraphQL errors and data for debugging
|
||||
useEffect(() => {
|
||||
console.log('[HomePage] === GraphQL Debug Info ===');
|
||||
console.log('[HomePage] Raw data:', data);
|
||||
console.log('[HomePage] Data type:', typeof data);
|
||||
console.log('[HomePage] Data keys:', data ? Object.keys(data) : 'null');
|
||||
|
||||
try {
|
||||
const today = format(new Date(), 'yyyy-MM-dd');
|
||||
const summary = await trackingApi.getDailySummary(selectedChild.id, today);
|
||||
console.log('[HomePage] Refreshed daily summary:', summary);
|
||||
setDailySummary(summary);
|
||||
} catch (error) {
|
||||
console.error('[HomePage] Failed to refresh summary:', error);
|
||||
if (error) {
|
||||
console.error('[HomePage] GraphQL error:', error);
|
||||
console.error('[HomePage] GraphQL error details:', {
|
||||
message: error.message,
|
||||
networkError: error.networkError,
|
||||
graphQLErrors: error.graphQLErrors,
|
||||
});
|
||||
}
|
||||
}, [selectedChild]);
|
||||
if (data) {
|
||||
console.log('[HomePage] GraphQL data:', data);
|
||||
console.log('[HomePage] Dashboard data:', data.dashboard);
|
||||
}
|
||||
console.log('[HomePage] Query state:', {
|
||||
loading,
|
||||
error: !!error,
|
||||
hasData: !!data,
|
||||
user: !!user,
|
||||
skip: authLoading || !user,
|
||||
childrenCount: data?.dashboard?.children?.length || 0,
|
||||
selectedChildId,
|
||||
});
|
||||
console.log('[HomePage] LocalStorage token:', typeof window !== 'undefined' ? localStorage.getItem('accessToken')?.substring(0, 20) : 'SSR');
|
||||
}, [error, data, loading, user, authLoading, selectedChildId]);
|
||||
|
||||
// Real-time activity handler to refetch dashboard data
|
||||
const refreshDashboard = useCallback(async () => {
|
||||
if (refetch) {
|
||||
console.log('[HomePage] Refreshing dashboard data...');
|
||||
await refetch();
|
||||
}
|
||||
}, [refetch]);
|
||||
|
||||
// Subscribe to real-time activity updates
|
||||
useRealTimeActivities(
|
||||
refreshDailySummary, // On activity created
|
||||
refreshDailySummary, // On activity updated
|
||||
refreshDailySummary // On activity deleted
|
||||
refreshDashboard, // On activity created
|
||||
refreshDashboard, // On activity updated
|
||||
refreshDashboard // On activity deleted
|
||||
);
|
||||
|
||||
// Load children and daily summary
|
||||
// Set the first child as selected when data loads
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
// Wait for auth to complete before trying to load data
|
||||
if (authLoading) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!familyId) {
|
||||
console.log('[HomePage] No familyId found');
|
||||
console.log('[HomePage] User object:', JSON.stringify(user, null, 2));
|
||||
console.log('[HomePage] User.families:', user?.families);
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[HomePage] Loading data for familyId:', familyId);
|
||||
|
||||
try {
|
||||
// Load children
|
||||
const childrenData = await childrenApi.getChildren(familyId);
|
||||
console.log('[HomePage] Children loaded:', childrenData.length);
|
||||
setChildren(childrenData);
|
||||
|
||||
if (childrenData.length > 0) {
|
||||
const firstChild = childrenData[0];
|
||||
setSelectedChild(firstChild);
|
||||
|
||||
// Load today's summary for first child
|
||||
const today = format(new Date(), 'yyyy-MM-dd');
|
||||
console.log('[HomePage] Fetching daily summary for child:', firstChild.id, 'date:', today);
|
||||
const summary = await trackingApi.getDailySummary(firstChild.id, today);
|
||||
console.log('[HomePage] Daily summary response:', summary);
|
||||
setDailySummary(summary);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[HomePage] Failed to load data:', error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadData();
|
||||
}, [familyId, authLoading, user]);
|
||||
if (data?.dashboard?.children && data.dashboard.children.length > 0 && !selectedChildId) {
|
||||
const firstChild = data.dashboard.children[0];
|
||||
setSelectedChildId(firstChild.id);
|
||||
}
|
||||
}, [data, selectedChildId]);
|
||||
|
||||
const quickActions = [
|
||||
{ icon: <Restaurant />, label: 'Feeding', color: '#E91E63', path: '/track/feeding' }, // Pink with 4.5:1 contrast
|
||||
@@ -123,6 +113,12 @@ export default function HomePage() {
|
||||
}
|
||||
};
|
||||
|
||||
// Extract data from GraphQL response
|
||||
const children = data?.dashboard?.children || [];
|
||||
const selectedChild = data?.dashboard?.selectedChild;
|
||||
const dailySummary = data?.dashboard?.todaySummary;
|
||||
const isLoading = authLoading || loading;
|
||||
|
||||
return (
|
||||
<ProtectedRoute>
|
||||
<AppShell>
|
||||
@@ -206,7 +202,7 @@ export default function HomePage() {
|
||||
isolate
|
||||
fallback={<DataErrorFallback error={new Error('Failed to load summary')} />}
|
||||
>
|
||||
{loading ? (
|
||||
{isLoading ? (
|
||||
<StatGridSkeleton count={3} />
|
||||
) : (
|
||||
<Paper sx={{ p: 3 }}>
|
||||
@@ -252,9 +248,9 @@ export default function HomePage() {
|
||||
}}
|
||||
>
|
||||
<Hotel sx={{ fontSize: 32, color: 'info.main', mb: 1 }} aria-hidden="true" />
|
||||
<Typography variant="h3" component="div" fontWeight="600" aria-label={`${dailySummary.sleepTotalMinutes ? formatSleepHours(dailySummary.sleepTotalMinutes) : '0 minutes'} sleep today`}>
|
||||
{dailySummary.sleepTotalMinutes
|
||||
? formatSleepHours(dailySummary.sleepTotalMinutes)
|
||||
<Typography variant="h3" component="div" fontWeight="600" aria-label={`${dailySummary.totalSleepDuration ? formatSleepHours(dailySummary.totalSleepDuration) : '0 minutes'} sleep today`}>
|
||||
{dailySummary.totalSleepDuration
|
||||
? formatSleepHours(dailySummary.totalSleepDuration)
|
||||
: '0m'}
|
||||
</Typography>
|
||||
<Typography variant="body2" sx={{ color: 'rgba(0, 0, 0, 0.7)' }}>
|
||||
|
||||
31
maternal-web/lib/apollo-client.ts
Normal file
31
maternal-web/lib/apollo-client.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
|
||||
import { setContext } from '@apollo/client/link/context';
|
||||
|
||||
const httpLink = new HttpLink({
|
||||
uri: process.env.NEXT_PUBLIC_GRAPHQL_URL || 'http://localhost:3020/graphql',
|
||||
});
|
||||
|
||||
const authLink = setContext((_, { headers }) => {
|
||||
// Get the authentication token from localStorage if it exists
|
||||
const token = typeof window !== 'undefined' ? localStorage.getItem('accessToken') : null;
|
||||
|
||||
return {
|
||||
headers: {
|
||||
...headers,
|
||||
authorization: token ? `Bearer ${token}` : '',
|
||||
'apollo-require-preflight': 'true', // Required for Apollo Server 4.0 CSRF protection
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const apolloClient = new ApolloClient({
|
||||
link: authLink.concat(httpLink),
|
||||
cache: new InMemoryCache(),
|
||||
defaultOptions: {
|
||||
watchQuery: {
|
||||
fetchPolicy: 'cache-and-network',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export default apolloClient;
|
||||
Reference in New Issue
Block a user