Create a GraphQL Resolver with Data Loading and Authorization
Generate a full GraphQL resolver implementation with DataLoader, authorization, pagination, and proper error handling.
๐ The Prompt
Write a complete GraphQL resolver for a [ENTITY_NAME] type using [GRAPHQL_FRAMEWORK] (e.g., Apollo Server, GraphQL Yoga, Strawberry, Ariadne) in [PROGRAMMING_LANGUAGE].
Schema context:
- Parent type: [PARENT_TYPE]
- The [ENTITY_NAME] has these fields: [FIELD_LIST_WITH_TYPES]
- Relationships: [ENTITY_NAME] has [RELATIONSHIP_DESCRIPTION] (e.g., "a User has many Posts and belongs to an Organization")
Please implement the following resolvers and patterns:
1. **Type Definition**: Write the GraphQL SDL (Schema Definition Language) for [ENTITY_NAME] including input types for mutations.
2. **Query Resolvers**:
- `[ENTITY_NAME_LOWERCASE](id: ID!)` โ fetch single entity
- `[ENTITY_NAME_LOWERCASE_PLURAL](filter: [FILTER_INPUT], pagination: PaginationInput)` โ fetch list with cursor-based pagination
3. **Mutation Resolvers**:
- `create[ENTITY_NAME]` with input validation
- `update[ENTITY_NAME]` with partial update support
- `delete[ENTITY_NAME]` with soft delete option
4. **Field Resolvers**: Implement resolvers for relationship fields ([RELATIONSHIP_DESCRIPTION]) using DataLoader pattern to solve the N+1 query problem. Show the DataLoader factory function and batch loading logic.
5. **Authorization**: Add middleware/directive-based authorization checking [ROLE_OR_PERMISSION] before resolver execution. Handle unauthorized access gracefully.
6. **Error Handling**: Use custom GraphQL errors with extensions for error codes: NOT_FOUND, UNAUTHORIZED, VALIDATION_ERROR, and INTERNAL_ERROR.
7. **Context Setup**: Show how the resolver accesses database layer, authenticated user, and DataLoader instances from context.
Include TypeScript/type annotations where applicable and add comments explaining the DataLoader batching strategy.
๐ก Tips for Better Results
Clearly describe your entity relationships in [RELATIONSHIP_DESCRIPTION] so the DataLoader batching logic is accurate. Specify whether you want relay-style cursor pagination or simple offset pagination for different output. Include your actual role/permission model in [ROLE_OR_PERMISSION] to get realistic auth guards.
๐ฏ Use Cases
Full-stack and backend developers use this when adding new types and resolvers to a GraphQL API. Especially useful when dealing with relational data that requires optimized data fetching patterns.
๐ Related Prompts
๐ป Coding
beginner
Explain Code Like Im a Beginner
Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.
๐ป Coding
beginner
Debug My Code and Explain the Fix
Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.
๐ป Coding
intermediate
Write Unit Tests for My Code
Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.
๐ป Coding
intermediate
Convert Code Between Languages
Convert code between any programming languages while maintaining idiomatic patterns and best practices.
๐ป Coding
intermediate
Write a REST API Endpoint
Generate production-ready REST API endpoints with validation, error handling, and documentation.
๐ป Coding
advanced
Refactor Code for Better Performance
Get your code refactored for better performance with Big O analysis and design pattern suggestions.