温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Python 中使用 Graphql

2018-02-23

GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。



  • 安装
  • Hello World
  • SQLAlchemy
  • Flask

安装

1
$ pip install graphene

Hello World

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)

import graphene

class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))

def resolve_hello(self, info, name):
print(info)
return 'Hello ' + name

schema = graphene.Schema(query=Query)

result = schema.execute('{ hello }')
print(result.data)
print(result.data['hello']) # "Hello stranger"

SQLAlchemy

想在项目中使用 Graphql 链接数据库是必备的,现在 Python 中链接数据库普遍使用 SQLAlchemy 框架,将两者整合起来也非常简单

安装

1
$ pip install "graphene-sqlalchemy>=2.0"

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)

from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import graphene
from graphene_sqlalchemy import SQLAlchemyObjectType
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:wxnacy@127.0.0.1:3306/study?charset=utf8mb4'

engine = create_engine(SQLALCHEMY_DATABASE_URI, echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
class Book(Base):
__tablename__ = 'book'
id = Column(Integer, primary_key=True)
name = Column(String(132))

def __str__(self):
return f"Book[id:{self.id}, name:{self.name}]"


class BookQuery(SQLAlchemyObjectType):
class Meta:
model = Book

class Query(graphene.ObjectType):
books = graphene.List(BookQuery)
book = BookQuery

def resolve_books(self, info):
print(info)
query = BookQuery.get_query(info) # SQLAlchemy query
return query.all()

def resolve_book(self, info):
query = BookQuery.get_query(info) # SQLAlchemy query
return query.filter(Book.id==2).first()

schema = graphene.Schema(query=Query)


query = '''
query {
books {
name
}
book
}
'''.strip()
result = schema.execute(query, context_value={'session': session})
print(result.data)

Flask

终归我们是要在 web 接口中使用的,Graphql 也提供了 Flask 相应的框架

下载

1
$ pip install Flask-GraphQL

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)

from flask import Flask
from flask_graphql import GraphQLView
import graphene

app = Flask(__name__)

class Query(graphene.ObjectType):
hello = graphene.String(name=graphene.String(default_value="stranger"))

def resolve_hello(self, info, name):
print(info)
return 'Hello ' + name

schema = graphene.Schema(query=Query)

app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql',
schema=schema, graphiql=True))

app.run(port=4901)

在执行上述代码时,会启动一个端口 4901 的服务,访问 http://localhost:4901/graphql 并在左侧输入查询语句,会看到如下界面
/images/pygraphql1.png
这就是 Graphql 的调试页面了,在查询后,地址栏的地址会变成

1
http://localhost:4901/graphql?query=%7Bhello%7D

使用 curl 访问会直接得到 JSON 数据

1
{"data":{"hello":"Hello stranger"}}

在调试到想要数据后,地址直接用到客户端即可

同时在 Flask 中直接使用 Flask-SQLAlchemy 模块会更加方便操作数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_graphql import GraphQLView
from graphene_sqlalchemy import SQLAlchemyObjectType
import graphene
app = Flask(__name__)
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:wxnacy@127.0.0.1:3306/study?charset=utf8mb4'
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)


class Book(db.Model):
__tablename__ = 'book'
id = db.Column(db.INT, primary_key=True)
name = db.Column(db.String, default="")
def format(self):
return dict(id=self.id, name=self.name)

class BookQuery(SQLAlchemyObjectType):
class Meta:
model = Book

class Query(graphene.ObjectType):
books = graphene.List(BookQuery)

def resolve_books(self, info):
print(info)
query = BookQuery.get_query(info) # SQLAlchemy query
return query.all()

schema = graphene.Schema(query=Query)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql',
schema=schema, graphiql=True))

app.run(port=4901)
  • graphene
  • Graphene-SQLAlchemy
  • flask-graphql
  • 查询和变更
最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • python
  • graphql
Chrome Driver 下载
Python super() 继承方法
  1. 1. 安装
  2. 2. Hello World
  3. 3. SQLAlchemy
  4. 4. Flask
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量3254次 访客数3206人次 本文总阅读量2次 3.14.3.180
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆