python
56 lines · 2 tabs
Priya Sharma
Jan 2026
2 tabs
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment
class PostType(DjangoObjectType):
class Meta:
model = Post
fields = ('id', 'title', 'content', 'author', 'published_at')
class CommentType(DjangoObjectType):
class Meta:
model = Comment
fields = ('id', 'text', 'author', 'post', 'created_at')
class Query(graphene.ObjectType):
all_posts = graphene.List(PostType)
post = graphene.Field(PostType, id=graphene.Int())
def resolve_all_posts(self, info):
return Post.objects.filter(status='published')
def resolve_post(self, info, id):
return Post.objects.get(pk=id)
class CreatePost(graphene.Mutation):
class Arguments:
title = graphene.String(required=True)
content = graphene.String(required=True)
post = graphene.Field(PostType)
def mutate(self, info, title, content):
post = Post.objects.create(
title=title,
content=content,
author=info.context.user
)
return CreatePost(post=post)
class Mutation(graphene.ObjectType):
create_post = CreatePost.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
from django.urls import path
from graphene_django.views import GraphQLView
from .schema import schema
urlpatterns = [
path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)),
]
2 files · python
Explain with highlit
Graphene brings GraphQL to Django. I define types mapping to models and create resolvers for queries and mutations. Clients request exactly the data they need, reducing over-fetching. I use DjangoObjectType for automatic schema generation from models. For complex queries, custom resolvers fetch optimized data. GraphQL introspection provides automatic documentation. DataLoader prevents N+1 queries. For modern frontends or mobile apps, GraphQL offers flexibility traditional REST can't match. I expose GraphQL at /graphql endpoint.
Related snips
ruby
payload = {
sub: user.id,
iss: 'https://auth.example.com',
aud: 'codesnips-api',
exp: 15.minutes.from_now.to_i,
iat: Time.now.to_i,
JWT issuance and verification without common footguns
jwt
authentication
api
by Kai Nakamura
2 tabs
python
import os
import stat
for root, _dirs, files in os.walk('/etc'):
for name in files:
path = os.path.join(root, name)
Python security audit script for exposed risky filesystem state
python
auditing
host-security
by Kai Nakamura
1 tab
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
1 tab
python
class Product(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
cost = models.DecimalField(max_digits=10, decimal_places=2)
margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
Django model signals vs overriding save
django
python
models
by Priya Sharma
2 tabs
graphql
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
GraphQL API with Spring Boot
java
graphql
spring-boot
by David Kumar
3 tabs
python
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
Django URL namespacing and reverse lookups
django
python
urls
by Priya Sharma
3 tabs
Share this code
Here's the card — post it anywhere.