graphene-python学习笔记(1)入门

1、需求

  • Python (2.7, 3.4, 3.5, 3.6, pypy)
  • Graphene (2.0)

2、安装

pip install "graphene>=2.0"

  

3、创建第一个schema

schema用于描述你的数据,并且为服务器提供了一系列对应的解析函数用于获取数据或更新数据。

创建一个简单的schema,她仅包含一个Query,仅有一个字段hello,hello字段有一个参数name,当查询hello(name)的时候返回hello name.

import graphene

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

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

schema = graphene.Schema(query=Query)

  现在我们可以根据schema进行查询了

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

  现在第一个graphene schema已经可以工作了

posted @ 2018-08-30 09:44  tutu_python  阅读(1451)  评论(0)    收藏  举报