Skip to content

Commit f5eb9bc

Browse files
committed
Add schema.graphql
1 parent c447ceb commit f5eb9bc

File tree

3 files changed

+109
-12
lines changed

3 files changed

+109
-12
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ Here is the list of commits in chronological order:
2929
[`f9a4d26`](https://github.com/zdzislaw-s/howtographql/commit/f9a4d269abd92fe9bd389fc640c34edfdad15540)` Filter Links`
3030
[`8d8a278`](https://github.com/zdzislaw-s/howtographql/commit/8d8a2788d5752bf9e842a70d9da775e5aac2dc54)` Paginate Links`
3131
[`662d31f`](https://github.com/zdzislaw-s/howtographql/commit/662d31fd237320edc1bde7fd9c7499857008a8cd)` Fix missed s/Vote/VoteModel/`
32+
33+
## schema.graphql
34+
35+
The [`schema.graphql`](hackernews/schema.graphql) file was created with the following procedure:
36+
37+
```python
38+
>>> import hackernews.wsgi
39+
>>> from hackernews.schema import schema
40+
>>> with open("schema.graphql", "w") as fo:
41+
... fo.write(str(schema))
42+
```

hackernews/hackernews/schema.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from graphql import GraphQLError
12
import graphene
2-
import graphql_jwt
3+
4+
from graphql_jwt import ObtainJSONWebToken as ObtainToken
5+
from graphql_jwt import Verify as VerifyToken
6+
from graphql_jwt import Refresh as RefreshToken
37
from graphene_django import DjangoObjectType
48
from django.contrib.auth import get_user_model
59
from django.db.models import Q
6-
from graphql import GraphQLError
710

811
from links.models import Link as LinkModel
912
from links.models import Vote as VoteModel
@@ -34,7 +37,7 @@ class Arguments:
3437
url = graphene.String()
3538
description = graphene.String()
3639

37-
def mutate(self, info, url, description):
40+
def mutate(parent, info, url, description):
3841
user = info.context.user
3942
if user.is_anonymous:
4043
raise GraphQLError("Anonymous users can't create links!")
@@ -56,7 +59,7 @@ class CreateVote(graphene.Mutation):
5659
class Arguments:
5760
linkId = graphene.Int()
5861

59-
def mutate(self, info, linkId):
62+
def mutate(parent, info, linkId):
6063
user = info.context.user
6164
if user.is_anonymous:
6265
raise GraphQLError("You must be logged in to vote!")
@@ -77,7 +80,7 @@ class Arguments:
7780
password = graphene.String(required=True)
7881
email = graphene.String(required=True)
7982

80-
def mutate(self, info, username, password, email):
83+
def mutate(parent, info, username, password, email):
8184
user = get_user_model()(
8285
username=username,
8386
email=email,
@@ -89,9 +92,9 @@ def mutate(self, info, username, password, email):
8992

9093

9194
class Mutation(graphene.ObjectType):
92-
obtainToken = graphql_jwt.ObtainJSONWebToken.Field()
93-
verifyToken = graphql_jwt.Verify.Field()
94-
refreshToken = graphql_jwt.Refresh.Field()
95+
obtainToken = ObtainToken.Field()
96+
verifyToken = VerifyToken.Field()
97+
refreshToken = RefreshToken.Field()
9598
createLink = CreateLink.Field()
9699
createVote = CreateVote.Field()
97100
createUser = CreateUser.Field()
@@ -105,7 +108,7 @@ class Query(graphene.ObjectType):
105108
users = graphene.List(User)
106109
whoami = graphene.Field(User)
107110

108-
def resolve_links(self, info, search=None, first=None, skip=None, **kwargs):
111+
def resolve_links(parent, info, search=None, first=None, skip=None, **kwargs):
109112
objects = LinkModel.objects
110113
qs = []
111114
if search is None:
@@ -122,13 +125,13 @@ def resolve_links(self, info, search=None, first=None, skip=None, **kwargs):
122125

123126
return qs
124127

125-
def resolve_votes(self, info, **kwargs):
128+
def resolve_votes(parent, info, **kwargs):
126129
return VoteModel.objects.all()
127130

128-
def resolve_users(self, info):
131+
def resolve_users(parent, info):
129132
return get_user_model().objects.all()
130133

131-
def resolve_whoami(self, info):
134+
def resolve_whoami(parent, info):
132135
user = info.context.user
133136
if user.is_anonymous:
134137
raise GraphQLError("Not logged in!")

hackernews/schema.graphql

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
schema {
2+
query: Query
3+
mutation: Mutation
4+
}
5+
6+
type CreateLink {
7+
id: Int
8+
url: String
9+
description: String
10+
postedBy: User
11+
}
12+
13+
type CreateUser {
14+
user: User
15+
}
16+
17+
type CreateVote {
18+
user: User
19+
link: Link
20+
}
21+
22+
scalar DateTime
23+
24+
scalar GenericScalar
25+
26+
type Link {
27+
id: ID!
28+
url: String!
29+
description: String!
30+
postedBy: User
31+
votes: [Vote]
32+
}
33+
34+
type Mutation {
35+
obtainToken(username: String!, password: String!): ObtainJSONWebToken
36+
verifyToken(token: String): Verify
37+
refreshToken(token: String): Refresh
38+
createLink(description: String, url: String): CreateLink
39+
createVote(linkId: Int): CreateVote
40+
createUser(email: String!, password: String!, username: String!): CreateUser
41+
}
42+
43+
type ObtainJSONWebToken {
44+
token: String
45+
}
46+
47+
type Query {
48+
links(search: String, first: Int, skip: Int): [Link]
49+
votes: [Vote]
50+
users: [User]
51+
whoami: User
52+
}
53+
54+
type Refresh {
55+
token: String
56+
payload: GenericScalar
57+
}
58+
59+
type User {
60+
id: ID!
61+
password: String!
62+
lastLogin: DateTime
63+
isSuperuser: Boolean!
64+
username: String!
65+
firstName: String!
66+
lastName: String!
67+
email: String!
68+
isStaff: Boolean!
69+
isActive: Boolean!
70+
dateJoined: DateTime!
71+
linkSet: [Link]
72+
voteSet: [Vote]
73+
}
74+
75+
type Verify {
76+
payload: GenericScalar
77+
}
78+
79+
type Vote {
80+
id: ID!
81+
user: User!
82+
link: Link!
83+
}

0 commit comments

Comments
 (0)