Skip to content

Commit 338511c

Browse files
author
何全
committed
v0.3.0 增加 GraphQL 支持
1 parent 584287b commit 338511c

9 files changed

Lines changed: 182 additions & 8 deletions

File tree

README.md

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
![语言](https://img.shields.io/badge/language-python3.6-blue.svg)
44
![语言](https://img.shields.io/badge/env-django2.1.7-red.svg)
55
![bootstrap4](https://img.shields.io/badge/model-bootstrap4-mauve.svg)
6+
![RESETful](https://img.shields.io/badge/env-RESETful-red.svg)
7+
![GraphQL](https://img.shields.io/badge/env-GraphQL-red.svg)
68

79
> django-base-templastes
810
911
> 因本项目开始时间为3月1日,是 国际海豹日,故项目起名为 海豹 seal
1012
11-
> 主要为 django 基础开发平台, MVC 模式 开发.支持 非前后端分离 和 前后端分离模式,可以拿来参考 开发 django项目
13+
> 主要为 django 基础开发平台, MVC 模式 开发.支持 非前后端分离 和 前后端分离模式,可以拿来参考 开发 django项目
14+
15+
> 支持 RESETful 和 GraphQL
1216
1317
> vue 前端地址 https://github.com/hequan2017/seal-vue 持续开发中
1418
@@ -21,11 +25,15 @@
2125
* 采用cbv开发方式
2226
* drf api 例子
2327
* 前端 Vue版本
28+
* GraphQL
2429

2530

2631
## DEMO
2732
![列表](document/demo/1.jpg)
2833
![添加](document/demo/2.jpg)
34+
![API](document/demo/3.jpg)
35+
![API](document/demo/4.jpg)
36+
![API](document/demo/5.jpg)
2937

3038

3139
## templates
@@ -36,6 +44,46 @@
3644
* document 代码规范
3745

3846

47+
## GraphQL
48+
> 具体代码 请参考 seal/schema.py
49+
50+
> 请求地址 : http://localhost/graphql
51+
52+
> GraphQL 请求参数
53+
```
54+
query{
55+
users{
56+
id,
57+
username,
58+
email
59+
}
60+
}
61+
62+
mutation createUser {
63+
createUser (username: "test1") {
64+
info {
65+
id,
66+
},
67+
ok
68+
}
69+
}
70+
71+
mutation updateUser {
72+
updateUser (pk:2,username: "test2") {
73+
info {
74+
id,
75+
},
76+
ok
77+
}
78+
}
79+
80+
mutation deleteUser {
81+
deleteUser (pk:2) {
82+
ok
83+
}
84+
}
85+
```
86+
3987

4088
## 部署
4189

@@ -54,7 +102,7 @@ python manage.py runserver 0.0.0.0:80
54102

55103
```
56104

57-
## 其他
105+
## 异步任务
58106
* 扩展功能-异步1 推荐 定时任务用celery
59107
```bash
60108
#需要安装redis
@@ -87,13 +135,16 @@ for i in Users.objects.all():
87135
```
88136

89137

90-
### 售后服务
138+
## 售后服务
91139

92140
* bootstrap4 中文文档 https://code.z01.com/v4/
93141
* cbv 中文文档 http://ccbv.co.uk/projects/Django/2.1/django.views.generic.edit/
142+
* GraphQL 中文参考文档 https://passwo.gitbook.io/graphql/index/drf
143+
144+
### 其他
94145
* 有问题 可以加QQ群: 620176501 <a target="_blank" href="//shang.qq.com/wpa/qunwpa?idkey=bbe5716e8bd2075cb27029bd5dd97e22fc4d83c0f61291f47ed3ed6a4195b024"><img border="0" src="https://github.com/hequan2017/cmdb/blob/master/static/img/group.png" alt="django开发讨论群" title="django开发讨论群"></a>
95146
* 欢迎提出你的需求和意见,或者来加入到本项目中一起开发。
96147

97-
## 作者
148+
### 作者
98149
* 何全
99150

assets/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ class Ecs(models.Model):
88
('华为云', '华为云'),
99
('亚马逊', '亚马逊'),
1010
('其他', '其他'),
11+
(None,None),
1112
)
1213
hostname = models.CharField(max_length=96, verbose_name='主机名', blank=True, null=True, )
13-
type = models.CharField(choices=TYPE_CHOICES, max_length=16, verbose_name='主机类型')
14+
type = models.CharField(choices=TYPE_CHOICES, max_length=16, verbose_name='主机类型', blank=True, null=True, )
1415
instance_id = models.CharField(max_length=64, verbose_name='实例ID', unique=True)
1516
instance_name = models.CharField(max_length=96, verbose_name='标签', blank=True, null=True, )
1617
os_name = models.CharField(max_length=64, verbose_name='系统版本', blank=True, null=True, )

document/demo/3.jpg

97 KB
Loading

document/demo/4.jpg

39.2 KB
Loading

document/demo/5.jpg

57.7 KB
Loading

requirements.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
amqp==2.4.2
2+
aniso8601==3.0.2
23
argh==0.26.2
34
backcall==0.1.0
45
billiard==3.5.0.5
@@ -8,7 +9,7 @@ chardet==3.0.4
89
coreapi==2.3.3
910
coreschema==0.0.4
1011
decorator==4.3.0
11-
Django==2.1.7
12+
Django==2.2rc1
1213
django-bootstrap4==0.0.7
1314
django-celery-beat==1.4.0
1415
django-celery-results==1.0.4
@@ -20,6 +21,10 @@ django-timezone-field==3.0
2021
djangorestframework==3.9.2
2122
dramatiq==1.5.0
2223
gevent==1.4.0
24+
graphene==2.1.3
25+
graphene-django==2.2.0
26+
graphql-core==2.1
27+
graphql-relay==0.4.5
2328
greenlet==0.4.15
2429
idna==2.8
2530
ipython==6.4.0
@@ -37,6 +42,7 @@ pathtools==0.1.2
3742
pexpect==4.6.0
3843
pickleshare==0.7.4
3944
prometheus-client==0.2.0
45+
promise==2.2.1
4046
prompt-toolkit==1.0.15
4147
ptyprocess==0.5.2
4248
Pygments==2.2.0
@@ -46,13 +52,16 @@ pytz==2018.4
4652
PyYAML==5.1
4753
redis==3.2.0
4854
requests==2.21.0
55+
Rx==1.6.1
4956
simplegeneric==0.8.1
5057
simplejson==3.16.0
58+
singledispatch==3.4.0.3
5159
six==1.11.0
60+
sqlparse==0.3.0
5261
traitlets==4.3.2
5362
uritemplate==3.0.0
5463
urllib3==1.24.1
5564
vine==1.2.0
5665
watchdog==0.8.3
5766
watchdog-gevent==0.1.0
58-
wcwidth==0.1.7
67+
wcwidth==0.1.7

seal/schema.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from system.models import Users
2+
from graphene_django import DjangoObjectType
3+
import graphene
4+
5+
6+
# 相关文档 https://passwo.gitbook.io/graphql/index/drf
7+
class UserType(DjangoObjectType):
8+
class Meta:
9+
model = Users
10+
11+
12+
class Query(graphene.ObjectType):
13+
users = graphene.List(UserType)
14+
15+
# List == Field:
16+
# List 返回结果会是遍历所有查询结果
17+
# Field 返回结果只存在单个 (其中可添加参数, ex. pk)
18+
single_user = graphene.Field(UserType, pk=graphene.Int())
19+
20+
# 定义函数名的格式: resolve_字段
21+
# **kwargs 传递参数
22+
# pk: 如果在字段中定义, 则方法参数中必含
23+
def resolve_users(self, info, **kwargs):
24+
return Users.objects.all()
25+
26+
def resolve_single_user(self, info, pk):
27+
return Users.objects.get(id=pk)
28+
29+
30+
class TQuery(Query, graphene.ObjectType):
31+
pass
32+
33+
34+
class CreateUser(graphene.Mutation):
35+
class Arguments:
36+
username = graphene.String(required=True)
37+
38+
info = graphene.Field(UserType)
39+
ok = graphene.Boolean()
40+
41+
def mutate(self, info, **kwargs):
42+
# print(info.context.user, '==当前用户==')
43+
# kwargs 是传递参数中的变量
44+
# user = info.context.user
45+
user_obj = Users(**kwargs)
46+
try:
47+
user_obj.save()
48+
ok = True
49+
except Exception as e:
50+
print(e)
51+
ok = False
52+
return CreateUser(ok=ok, info=user_obj)
53+
54+
55+
class CMutation(object):
56+
create_user = CreateUser.Field()
57+
58+
59+
class UpdateUser(graphene.Mutation):
60+
class Arguments:
61+
username = graphene.String()
62+
pk = graphene.Int(required=True)
63+
64+
info = graphene.Field(UserType)
65+
ok = graphene.Boolean()
66+
67+
def mutate(self, info, **kwargs):
68+
pk = kwargs.get('pk')
69+
user_obj = Users.objects.get(id=pk)
70+
if not user_obj:
71+
return UpdateUser(ok=False)
72+
user_obj.__dict__.update(**kwargs)
73+
user_obj.save()
74+
ok = True
75+
return UpdateUser(ok=ok, info=user_obj)
76+
77+
78+
class UMutation(object):
79+
update_user = UpdateUser.Field()
80+
81+
82+
class DeleteUser(graphene.Mutation):
83+
class Arguments:
84+
pk = graphene.Int()
85+
86+
ok = graphene.Boolean()
87+
88+
def mutate(self, info, **kwargs):
89+
pk = kwargs.get('pk')
90+
91+
user = Users.objects.get(id=pk)
92+
user.delete()
93+
return DeleteUser(ok=True)
94+
95+
96+
class DMutation(object):
97+
delete_user = DeleteUser.Field()
98+
99+
100+
class Mutations(CMutation, UMutation,DMutation,graphene.ObjectType):
101+
pass
102+
103+
104+
schema = graphene.Schema(query=TQuery, mutation=Mutations)
105+

seal/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,13 @@
4646
'rest_framework.authtoken',
4747
'corsheaders',
4848
'django_filters',
49+
'graphene_django',
4950
]
5051

52+
GRAPHENE = {
53+
'SCHEMA': 'seal.schema.schema'
54+
}
55+
5156
MIDDLEWARE = [
5257
'django.middleware.security.SecurityMiddleware',
5358
'django.contrib.sessions.middleware.SessionMiddleware',

seal/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from system.views import index
55
from rest_framework.authtoken import views
66
from rest_framework.documentation import include_docs_urls
7+
from graphene_django.views import GraphQLView
8+
from seal.schema import schema
79

810
API_TITLE = '海豹 API 文档'
911
API_DESCRIPTION = '海豹 API 文档'
@@ -16,5 +18,6 @@
1618
path('admin/', admin.site.urls, ),
1719
path('api/token', views.obtain_auth_token),
1820
path('api/docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION, authentication_classes=[],
19-
permission_classes=[]))
21+
permission_classes=[])),
22+
path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)),
2023
]

0 commit comments

Comments
 (0)