对现有REST API更新为GraphQL API [升级版]
前序
https://zhuanlan.zhihu.com/p/113911857
背景
老板让可以控制最小子序列的输入输出
不变内容
框架及语言
GraphQL API需求
需要新增安装的模块
基本知识
原始单条数据返回结构
测试方法
解决办法
import collections from graphene import ObjectType, String, Schema, Int, List, Field import json class Clsseven(ObjectType): xxx = String() xxxxx = String() class Image(ObjectType): large_image = String() medium_image = String() small_image = String() swatch_image = String() class PersonType(ObjectType): one = List(String) two = String() image = Field(Image) four = String() id = String() six = String() seven = Field(Clsseven) def _json_object_hook(d): return collections.namedtuple('X', d.keys())(*d.values()) def json2obj(data): return json.loads(data, object_hook=_json_object_hook) class Query(ObjectType): """ GraphQL API 方法有 search 输入参数为 id count """ search = List(PersonType, id=String(), count=Int(default_value=5), description="Get search details") def resolve_search(root, info, id, count): data = 原始获取数据方式(id) response = json2obj(json.dumps(data)) return response[:count] schema = Schema(query=Query) app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True, pretty=True)) if __name__ == '__main__': app.run()
请求结构
浏览器直接请求测试结构
{ search(id: "lslsl", count:3) { one two id image{ large_image small_image } seven{ xxx } } }

浙公网安备 33010602011771号