Architecture Overview
The project uses a GraphQL layer that wraps the AnimeThemes REST API:src/lib/common/animethemes/type-defs.ts- Type definitionssrc/lib/common/animethemes/resolvers.ts- Resolvers that fetch from APIsrc/lib/common/animethemes/api.ts- API utilities and includes system
Creating a GraphQL Query
Define your query
Create a GraphQL query using
gql from graphql-tag:src/pages/anime/[animeSlug]/index.tsx
Use fragments for reusability
Define fragments on components to keep queries modular:Then use the fragment in your query:
src/components/card/ThemeDetailCard.tsx
src/pages/anime/[animeSlug]/index.tsx
The Includes System
The AnimeThemes API uses an “includes” system to control which related data is returned. The GraphQL layer automatically manages this.How Includes Work
When you query nested fields, the GraphQL layer automatically adds the necessary includes:Include Mappings
The includes are defined insrc/lib/common/animethemes/api.ts:17:
Allowed Includes
Not all include combinations are allowed by the API. The allowed includes are defined in theALLOWED_INCLUDES object at src/lib/common/animethemes/api.ts:117:
API Resolvers
Resolvers fetch data from the API and transform it for GraphQL.Creating a Simple Resolver
src/lib/common/animethemes/resolvers.ts
Resolver Types
createApiResolver- Returns data ornullif not foundcreateApiResolverNotNull- Throws error if data not foundcreateApiResolverPaginated- Fetches all pages automatically
Real Example: Home Page
Here’s how the home page fetches its data:src/pages/index.tsx
src/pages/index.tsx:225.
Environment Variables
Configure the API URLs using environment variables:.env.local
src/utils/config.ts:28.
Caching
The GraphQL layer includes request caching to prevent duplicate API calls during the same request:src/lib/common/animethemes/api.ts:413.
Best Practices
- Use fragments to keep queries modular and reusable
- Always run
npm run graphql-codegenafter modifying queries - Use TypeScript types generated from your queries
- Keep fragments close to the components that use them
- Use
getSharedPageProps(apiRequests)to track API usage - Test queries with different include combinations
- Check the API documentation for available includes
- Use
createApiResolverNotNullfor required fields - Use
createApiResolverPaginatedfor lists
Debugging
Enable verbose logging to see API requests:- GraphQL query paths
- Generated API URLs with includes
- Disallowed includes (if any)
- Cached requests
Next Steps
- Learn about adding pages
- Understand creating components
- Read about deployment