graphene-python学习笔记(8)schema

1、Schema

通过提供根类型query和mutation来创建schema

示例:

my_schema = Schema(
    query=MyRootQuery,
    mutation=MyRootMutation,
)

  (1)types

有时schema无法获取所有的types,例如一个字段返回的是一个interface.这时我们就需要使用types参数来指定返回的类型。

my_schema = Schema(
    query=MyRootQuery,
    types=[SomeExtraObjectType, ]
)

  (2)查询schema

查询schema可以使用excute方法

my_schema.execute('{ lastName }')

  (3)自动将字段名转换为驼峰标志

class Person(graphene.ObjectType):
    last_name = graphene.String()
    other_name = graphene.String(name='_other_Name')

  上面定义的字段last_name在schema中会被自动转变为lastName。如果不想被转换可以增加name参数,如other_name在schema中还是other_name

  如果想禁止自动转换:可以设置auto_camelcase为false.

my_schema = Schema(
    query=MyRootQuery,
    auto_camelcase=False,
)

  

posted @ 2018-08-30 11:34  tutu_python  阅读(264)  评论(0)    收藏  举报