Prisma GraphQL Generator
Generate GraphQL fragments, queries, and mutations for Prisma models based on the schema. This generator creates complete GraphQL operations for CRUD operations on Prisma models.
Overview
The Prisma GraphQL Generator creates client-side GraphQL operations that work seamlessly with Prisma-based GraphQL APIs. It generates:
- GraphQL fragments for reusable field selections
- Complete CRUD queries (findUnique, findMany, findCount)
- Complete CRUD mutations (create, update, delete, updateMany, deleteMany)
- Properly formatted .graphql files ready for use with GraphQL code generators
Configuration Options
The generator accepts these configuration options:
Generation Pattern
For each Prisma model, the generator creates the following structure:
1. Field Fragment
2. Complete Fragment with Relations
3. Query Operations
Find Unique Query
Find Many Query
Count Query
4. Mutation Operations
Create Mutation
Update Mutation
Delete Mutation
Delete Many Mutation
Update Many Mutation
Real-World Example
Let's generate GraphQL operations for a blog application with the following Prisma schema:
Sample Prisma Schema
AI Prompt
Generated Output
File Structure:
User.graphql:
Post.graphql:
Usage in Frontend Applications
With GraphQL Code Generator
-
Install dependencies:
bash -
Configure codegen.yml:
yaml -
Use generated hooks in React:
tsx
With Apollo Client
Field Exclusion Logic
The generator follows this logic for excluding fields:
- Check global
excludeFields
array - Fields listed here are excluded from all models - Check model-specific
excludeFieldsByModel[modelName]
array - Fields specific to a model - Exclude object fields from the Fields fragment - Relations go in the main fragment
- Include only scalar fields in the Fields fragment - Boolean, String, Int, DateTime, etc.
Operation Exclusion Logic
For excluding specific operations:
- Check global
excludeQueriesAndMutations
array - Operations excluded across all models - Check model-specific
excludeQueriesAndMutationsByModel[modelName]
array - Model-specific exclusions - Check if queries/mutations are globally disabled - Using
disableQueries
ordisableMutations
- Check
excludeModels
configuration - For selective query/mutation disabling per model
Best Practices
Field Selection Strategy
- Use fragments consistently - Always use the generated fragments for consistency
- Exclude sensitive fields globally - Add fields like
password
,hash
to global exclusions - Exclude audit fields - Fields like
createdAt
,updatedAt
are often not needed in fragments - Include relation fields selectively - Only include relations that are commonly queried together
Performance Considerations
- Use pagination variables - Always provide
take
andskip
for large datasets - Implement cursor-based pagination - Use
cursor
for efficient pagination - Add proper WHERE filters - Use the
where
parameter to filter data at the database level - Order results consistently - Use
orderBy
to ensure predictable results
Error Handling
- Handle loading states - Show loading indicators during queries
- Display error messages - Provide user-friendly error messages
- Implement retry logic - Handle network failures gracefully
- Validate inputs - Use TypeScript types for compile-time safety
Common Use Cases
1. User Management System
2. Blog Post Listing
3. Comment Thread
Next Steps
- Learn about the Prisma Admin Pages Generator for building admin interfaces
- Explore the Prisma Nexus Generator for backend GraphQL API development
- Check out the configuration examples on GitHub