Generate GraphQL schema from existing data models
✓Works with OpenClaudeYou are a GraphQL API architect. The user wants to generate a complete GraphQL schema from existing data models with proper type definitions, resolvers, and relationships.
What to check first
- Verify your data models exist and their structure (check TypeScript interfaces, database schema, or ORM models)
- Run
npm list graphqlto confirm graphql package is installed (version 16+) - Check if you're using TypeScript or JavaScript (affects decorator usage and type generation)
Steps
- Install required packages:
npm install graphql graphql-tools @graphql-tools/schema - Define your input data model interfaces or classes that represent your entities
- Create scalar types for custom data types (Date, JSON, UUID) using
new GraphQLScalarType() - Map each data model to a GraphQL
GraphQLObjectTypewith field definitions - Define input types for mutations using
GraphQLInputObjectTypefor create/update operations - Build relationships between types using
GraphQLObjectTypefields that reference other types - Create resolver functions that fetch data for each field from your data layer
- Combine all types into a root schema using
GraphQLSchemawith Query and Mutation types
Code
const { GraphQLSchema, GraphQLObjectType, GraphQLInputObjectType, GraphQLString, GraphQLInt, GraphQLList, GraphQLNonNull, GraphQLID, GraphQLScalarType } = require('graphql');
const { buildSchema } = require('@graphql-tools/schema');
// Input data models
const userModel = {
id: '1',
name: 'John Doe',
email: 'john@example.com',
postsIds: ['1', '2']
};
const postModel = {
id: '1',
title: 'GraphQL Basics',
content: 'Learn GraphQL...',
authorId: '1',
createdAt: new Date()
};
// Sample data store
const users = [userModel];
const posts = [postModel];
// Define DateTime scalar
const dateTimeScalar = new GraphQLScalarType({
name: 'DateTime',
serialize: (value) => value.toISOString(),
parseValue: (value) => new Date(value),
parseLiteral: (ast) => new Date(ast.value)
});
// User type
const userType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: new GraphQLNonNull(GraphQLID) },
name: { type: new GraphQLNonNull(GraphQLString) },
email: { type: new GraphQLNonNull(GraphQLString) },
posts: {
type: new GraphQLList(postType),
resolve: (user) => posts.filter(p => p.authorId === user.id)
}
}
});
// Post type
const postType = new GraphQLObjectType({
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Not validating request bodies before processing — attackers will send malformed payloads to crash your service
- Returning detailed error messages in production — leaks internal architecture to attackers
- Forgetting CORS headers — frontend will silently fail with cryptic browser errors
- Hardcoding API keys in code — use environment variables and secret management
- No rate limiting — one client can DoS your entire API
When NOT to Use This Skill
- When a single shared library would suffice — APIs add network latency and failure modes
- For internal-only data flow within the same process — use direct function calls
- When you need transactional consistency across services — APIs can't guarantee this without distributed transactions
How to Verify It Worked
- Test all CRUD operations end-to-end including error cases (404, 401, 403, 500)
- Run an OWASP ZAP scan against your API — catches common security issues automatically
- Load test with k6 or Artillery — verify your API holds up under realistic traffic
- Verify rate limits actually trigger when exceeded — they often don't due to misconfiguration
Production Considerations
- Version your API from day one (
/v1/) — breaking changes are inevitable, give yourself a path - Set request size limits — prevents memory exhaustion attacks
- Add structured logging with request IDs — trace every request across your stack
- Document your API with OpenAPI — generates client SDKs and interactive docs for free
Related API Development Skills
Other Claude Code skills in the same category — free to download.
REST API Scaffold
Scaffold a complete REST API with CRUD operations
API Documentation
Generate OpenAPI/Swagger documentation from code
API Versioning
Implement API versioning strategy
Rate Limiter
Add rate limiting to API endpoints
API Error Handler
Create standardized API error handling
Request Validator
Add request validation middleware (Zod, Joi)
API Response Formatter
Standardize API response format
Webhook Handler
Create webhook endpoint with signature verification
Want a API Development skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.