| name | graphql-schema |
| description | GraphQL Schema Definition Language (SDL) generation. Use when creating GraphQL types, queries, mutations, or writing .graphql schema files. |
GraphQL Schema
Generating GraphQL schema definitions using Schema Definition Language (SDL).
Quick Start
# Basic type definition
type_def = """
type User {
id: ID!
name: String!
email: String
}
"""
# Write to file
with open('schema.graphql', 'w') as f:
f.write(type_def)
Type Definitions
# Scalar types: String, Int, Float, Boolean, ID
# Object type
"type User { id: ID! username: String! posts: [Post!]! }"
# Input type (for mutations)
"input CreateUserInput { username: String! email: String! }"
# Enum type
"enum UserRole { ADMIN USER GUEST }"
Queries and Mutations
# Queries (read operations)
query_type = """
type Query {
user(id: ID!): User
users: [User!]!
}
"""
# Mutations (write operations)
mutation_type = """
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User!
deleteUser(id: ID!): Boolean!
}
"""
Field Modifiers
# Type! - Required (non-null)
# Type - Optional (nullable)
# [Type] - Array (can be null)
# [Type]! - Required array (can be empty)
# [Type!]! - Required array of required items
# Examples
"""
type Example {
required: String!
optional: String
list: [String]
requiredList: [String]!
strictList: [String!]!
}
"""
Schema Generation
def generate_schema(types, queries, mutations):
"""Generate complete GraphQL schema"""
parts = list(types)
if queries:
parts.append(f"type Query {{\n " + '\n '.join(queries) + "\n}")
if mutations:
parts.append(f"type Mutation {{\n " + '\n '.join(mutations) + "\n}")
return '\n\n'.join(parts)
Resolver Stubs
# Python resolver template
def create_resolver_stub(field_name, args, return_type):
args_str = ', '.join(args.keys()) if args else ''
return f"""
def resolve_{field_name}(parent, info{', ' + args_str if args_str else ''}):
# TODO: Implement {field_name} resolver
# Returns: {return_type}
pass
"""