Files
maternal-app/maternal-web/lib/apollo-client.ts
Andrei d8211cd573
Some checks failed
CI/CD Pipeline / Build Application (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
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>
2025-10-03 06:52:34 +00:00

32 lines
890 B
TypeScript

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;