Skip to content

Instantly share code, notes, and snippets.

@Desttro
Last active March 11, 2022 02:22
Show Gist options
  • Save Desttro/bd894fb03dd097e0b798db6c9d3d9831 to your computer and use it in GitHub Desktop.
Save Desttro/bd894fb03dd097e0b798db6c9d3d9831 to your computer and use it in GitHub Desktop.
django + strawberry
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=128)
slug = models.SlugField(unique=True, editable=False)
content = models.TextField() # markdown
thumbnail = models.ImageField(upload_to='%Y/%m/%d/', width_field='width', height_field='height')
width = models.PositiveIntegerField(editable=False)
height = models.PositiveIntegerField(editable=False)
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
from strawberry_django_plus import gql
from .types import PostType, PostInput
@gql.type
class Mutation:
create_post: PostType = gql.django.create_mutation(PostInput)
# update_post: PostType = gql.django.update_mutation(PostInputPartial)
update_post: PostType = gql.django.update_mutation(PostInput)
delete_post: PostType = gql.django.delete_mutation(gql.NodeInput)
from typing import Optional, List
from strawberry_django_plus import gql
from .types import PostType
@gql.type
class Query:
post: Optional[PostType] = gql.django.field()
posts: List[PostType] = gql.django.field()
from strawberry_django_plus import gql
from . import models
@gql.django.type(models.Post)
class PostType:
# id: gql.auto
title: gql.auto
slug: gql.auto
content: gql.auto
thumbnail: gql.auto
width: gql.auto
height: gql.auto
created: gql.auto
modified: gql.auto
@gql.django.input(models.Post)
class PostInput:
title: gql.auto
content: gql.auto
thumbnail: gql.auto
# @gql.django.partial(models.Post)
# class PostInputPartial(gql.NodeInput, PostInput):
# title: gql.auto
# content: gql.auto
# thumbnail: gql.auto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment