一、GraphQL教程Python
根據官方文檔,實現一個簡單的GraphQL API,首先我們需要安裝GraphQL,使用pip install graphql-python 進行安裝。
from graphene import ObjectType, String, Schema
class Query(ObjectType):
hello = String(name=String(default_value="stranger"))
def resolve_hello(root, info, name):
return f'Hello {name}!'
schema = Schema(query=Query)
result = schema.execute('{ hello }')
print(result.data['hello']) # 'Hello stranger!'
在上面的例子中,schema是整個API的入口,query定義了一個名為hello的查詢(variables參數為默認值),resolve_hello指定了查詢hello的具體行為,並返回了結果字符串。Schema.execute() 輸入一個查詢字符串,返回查詢結果。
在這個查詢中,我們可以把{hello}看作是一種表單提交語法,告訴服務器我們需要一個hello值。
二、GraphQL教程 Grapher
Grapher是一個用於構建GraphQL API的庫,具有可擴展的插件架構。Grapher中的插件由三部分組成:Graph、Query、Type。Graph是GraphQL API的定義,Query是定義的查詢函數,Type是查詢的返回值類型。
示例代碼如下:
from grapher import Graph, Type, Query, astId, astStr
class User(Type):
name = str
email = str
class MyGraph(Graph):
users = Query(list=User, args={
'id': astId,
'email': astStr,
'name': astStr,
},
resolve=lambda root, args, context: list_users_by_args(args)
)
Grapher不需要手動拼接schema代碼,Grapher會自動生成GraphQL API的完整schema。
三、GraphQL教程QGraphicsView
QGraphicsView是一個用於顯示自定義2D圖形界面的工具類,除了顯示圖像以外QGraphicsView還可以支持用戶交互。在QGraphicsView中,場景是笛卡爾圖形世界的一部分,其中可以放置任何元素,如圖形、文本或其他項。每個項都有自己的位置、姿態、範圍和渲染狀態。
下面是示例代碼:
from PyQt5.QtCore import QRectF, QPointF, Qt
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsEllipseItem
class MyView(QGraphicsView):
def __init__(self, parent=None):
super(MyView, self).__init__(parent)
self.setSceneRect(-200, -200, 400, 400)
self.setRenderHint(QPainter.Antialiasing)
self.setScene(QGraphicsScene(self))
ellipseItem = QGraphicsEllipseItem(0, 0, 70, 70)
ellipseItem.setPen(QPen(Qt.white, 0))
ellipseItem.setBrush(QColor(244, 191, 66))
ellipseItem.setPos(QPointF(-35, -35))
self.scene().addItem(ellipseItem)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
view = MyView()
view.show()
sys.exit(app.exec_())
該示例創建了一個圓形元素並添加到場景中。setSceneRect方法設置了場景的數值範圍,setRenderHint方法設置了渲染選項,例如反鋸齒。然後在場景中創建一個圓形項並添加到場景中。
四、GraphQL教程Grapher使用教程
使用Grapher創建GraphQL API的過程如下
- 定義要擴展的GraphQL插件
- 使用插件創建Grapher對象
- 使用Grapher對象創建一個可供查詢的schema
- 訪問這個GraphQL API的剩餘部分。
示例代碼如下:
from grapher import Schema
from grapher.config import Config
from grapher.graph import Graph
from grapher.graphql import graphql
from grapher.plugins import field, ObjectType, Query, String
class Hello(ObjectType):
hello = field(String, args=dict(name=String(description='姓名')), description='打招呼')
def resolve_hello(self, args, context, info):
return 'Hello ' + (args['name'] or 'World') + '!'
class ExampleGraph(Graph):
query = Query(Hello)
Grapher = Schema(ExampleGraph)
result = graphql(Grapher, '{ hello(name: "Grapher") }')
print(result['data'].get('hello'))
五、GraphQL教程Grapher軟件使用教程
Grapher是一款用於可視化構建GraphQL API的工具,操作簡單,易上手。
- 下載並安裝Grapher for Mac、Windows或Linux。
- 運行Grapher並創建一個新項目。
- 在項目頁面中創建模式或設計模式。
- 使用Grapher工具創建模式或設計模式的數據。
- 在Query欄中測試GraphQL API。
Grapher使用教程視頻:
https://www.youtube.com/watch?v=MNir2Y6Rf4g
以上是GraphQL教程的詳細闡述,希望對你有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/230525.html
微信掃一掃
支付寶掃一掃