Skip to content
This repository was archived by the owner on Mar 27, 2023. It is now read-only.

Commit b8fc66c

Browse files
author
Brian Caffey
committed
wip graphql hn clone
1 parent 4e36424 commit b8fc66c

File tree

26 files changed

+407
-4
lines changed

26 files changed

+407
-4
lines changed

backend/apps/accounts/schema.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from django.contrib.auth import get_user_model
2+
3+
import graphene
4+
from graphene_django import DjangoObjectType
5+
6+
7+
class UserType(DjangoObjectType):
8+
class Meta:
9+
model = get_user_model()
10+
11+
12+
class Query(graphene.ObjectType):
13+
current_user = graphene.Field(UserType)
14+
users = graphene.List(UserType)
15+
16+
def resolve_users(self, info):
17+
return get_user_model().objects.all()
18+
19+
def resolve_current_user(self, info):
20+
user = info.context.user
21+
print(dir(info.context))
22+
if user.is_anonymous:
23+
raise Exception("Not logged in...")
24+
25+
return user
26+
27+
28+
class CreateUser(graphene.Mutation):
29+
user = graphene.Field(UserType)
30+
31+
class Arguments:
32+
password = graphene.String(required=True)
33+
email = graphene.String(required=True)
34+
35+
def mutate(self, info, password, email):
36+
user = get_user_model()(
37+
email=email
38+
)
39+
user.set_password(password)
40+
user.save()
41+
42+
return CreateUser(user=user)
43+
44+
45+
class Mutation(graphene.ObjectType):
46+
create_user = CreateUser.Field()

backend/apps/hn/__init__.py

Whitespace-only changes.

backend/apps/hn/admin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
4+
from .models import Link
5+
6+
admin.site.register(Link)

backend/apps/hn/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class HnConfig(AppConfig):
5+
name = 'hn'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Generated by Django 2.2 on 2020-02-11 04:02
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Link',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('url', models.URLField()),
19+
('description', models.TextField(blank=True)),
20+
],
21+
),
22+
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 2.2 on 2020-02-11 17:06
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('hn', '0001_initial'),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name='link',
18+
name='posted_by',
19+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
20+
),
21+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 2.2 on 2020-02-11 18:51
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('hn', '0002_link_posted_by'),
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Vote',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('link', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='votes', to='hn.Link')),
21+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
22+
],
23+
),
24+
]

backend/apps/hn/migrations/__init__.py

Whitespace-only changes.

backend/apps/hn/models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.conf import settings
2+
from django.db import models
3+
4+
# Create your models here.
5+
6+
class Link(models.Model):
7+
url = models.URLField()
8+
description = models.TextField(blank=True)
9+
posted_by = models.ForeignKey(
10+
settings.AUTH_USER_MODEL,
11+
null=True,
12+
on_delete=models.CASCADE
13+
)
14+
15+
16+
class Vote(models.Model):
17+
user = models.ForeignKey(
18+
settings.AUTH_USER_MODEL,
19+
on_delete=models.CASCADE
20+
)
21+
link = models.ForeignKey(
22+
Link,
23+
related_name="votes",
24+
on_delete=models.CASCADE
25+
)

backend/apps/hn/schema.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import graphene
2+
from django.db.models import Q
3+
from graphene_django import DjangoObjectType
4+
5+
from apps.accounts.schema import UserType
6+
7+
from .models import Link, Vote
8+
9+
class LinkType(DjangoObjectType):
10+
class Meta:
11+
model = Link
12+
13+
class VoteType(DjangoObjectType):
14+
class Meta:
15+
model = Vote
16+
17+
18+
class Query(graphene.ObjectType):
19+
links = graphene.List(
20+
LinkType,
21+
search=graphene.String(),
22+
first=graphene.Int(),
23+
skip=graphene.Int()
24+
)
25+
votes = graphene.List(VoteType)
26+
27+
def resolve_links(self, info, search=None, first=None, skip=None, **kwargs):
28+
qs = Link.objects.all()
29+
if search:
30+
filter = (
31+
Q(url__icontains=search) |
32+
Q(description__icontains=search)
33+
)
34+
qs = qs.filter(filter)
35+
36+
if skip:
37+
qs = qs[skip:]
38+
39+
if first:
40+
qs = qs[:first]
41+
42+
return qs
43+
44+
def resolve_votes(self, info, **kwargs):
45+
return Vote.objects.all()
46+
47+
48+
class CreateLink(graphene.Mutation):
49+
id = graphene.Int()
50+
url = graphene.String()
51+
description = graphene.String()
52+
posted_by = graphene.Field(UserType)
53+
54+
class Arguments:
55+
url = graphene.String()
56+
description = graphene.String()
57+
58+
def mutate(self, info, url, description):
59+
user = info.context.user or None
60+
link = Link(
61+
url=url,
62+
description=description,
63+
posted_by=user
64+
)
65+
66+
link.save()
67+
68+
return CreateLink(
69+
id=link.id,
70+
url=link.url,
71+
description=link.description,
72+
posted_by=link.posted_by
73+
)
74+
75+
class CreateVote(graphene.Mutation):
76+
user = graphene.Field(UserType)
77+
link = graphene.Field(LinkType)
78+
79+
class Arguments:
80+
link_id = graphene.Int()
81+
82+
def mutate(self, info, link_id):
83+
user = info.context.user
84+
if user.is_anonymous:
85+
raise Exception("Must be logged in to vote")
86+
87+
link = Link.objects.filter(id=link_id).first()
88+
89+
if not link:
90+
raise Exception("Invalid link")
91+
92+
Vote.objects.create(
93+
user=user,
94+
link=link
95+
)
96+
97+
return CreateVote(user=user, link=link)
98+
99+
class Mutation(graphene.ObjectType):
100+
create_link = CreateLink.Field()
101+
create_vote = CreateVote.Field()

0 commit comments

Comments
 (0)