从Python数据结构进行GraphQL查询。
graphql-from-struct的Python项目详细描述
来自结构的GraphQL
一个简单的单方法库生成一个GraphQL 从Python数据结构进行查询。在
目录
安装
pip install graphql_from_struct
用法
^{pr2}$你应该看到这样的结果:
query{ hero{ name } }
例外情况
如果为空或错误,模块将引发GqlFromStructException 数据结构输入。在
参数
GqlFromStruct()构造函数和.from\_struct()方法有2个参数: struct(默认无)和minimize(默认为False)标志。 代码:
foo = GqlFromStruct({'hero':{'@fields':['name']}}, True) # or foo = GqlFromStruct(struct = {'hero':{'@fields':['name']}}, minimize = True) gql = foo.query() # or gql = GqlFromStruct.from_struct({'hero':{'@fields':['name']}}, True) print (gql)
给你:
query{hero{name}}
保留关键字
文字 @alias, @args, @fields, @fragments, @fragment_name, @directives, @include, @mutations, @operation_name, @queries, @query, @skip, @variables 保留并用于查询构造。在
示例
示例的显示顺序与中的顺序相同 GraphQL文档。在
字段
使用@fields关键字:
struct = {'hero':{'@fields':['name']}} print (GqlFromStruct.from_struct(struct))
输出:
query{ hero{ name } }
可以使用任意字段嵌套:
struct = {'hero':{'@fields':['name', {'friends':{'@fields':['name']}}]}} print (GqlFromStruct.from_struct(struct))
输出:
query{ hero{ name friends{ name } } }
参数
使用@args关键字:
struct = {'human':{'@fields':['name', 'height'], '@args':{'id':'"1000"'}}} print (GqlFromStruct.from_struct(struct))
输出:
query{ human( id : "1000" ){ name height } }
或者:
struct = { 'human': { '@fields': ['name', { 'height': { '@args': { 'unit': 'FOOT' } } }], '@args': { 'id': "1000" } } } print (GqlFromStruct.from_struct(struct))
输出:
query{ human( id : 1000 ){ name height( unit : FOOT ) } }
注意:GraphQL From Struct默认情况下只对值使用双引号 有空格。像这样:
query = {'human':{'@fields':['name', 'height'], '@args':{'id':'1000 meters'}}}
输出:
query{ human( id : "1000 meters" ){ name height } }
单个单词或数值以 超过了他们。在
query = {'human':{'@fields':['name', 'height'], '@args':{'id':1000}}} query{ human( id : 1000 ){ name height } }
默认参数
可以设置参数的默认值:
struct = {'human':{'@fields':['name', 'height'], '@args':{'$first': {'Int':'3'}}} print (GqlFromStruct.from_struct(struct))
输出:
query{ human( $first : Int = 3 ){ name height } }
别名
使用@alias关键字:
struct = [{ 'hero': { '@alias': 'empireHero', '@args': { 'episode': "EMPIRE" }, '@fields': ['name'] } }, { 'hero': { '@alias': 'jediHero', '@args': { 'episode': "JEDI" }, '@fields': ['name'] } }] print (GqlFromStruct.from_struct(struct))
输出:
query{ empireHero : hero( episode : EMPIRE ){ name } jediHero : hero( episode : JEDI ){ name } }
碎片
使用@fragments和@fragment_name关键字进行片段设置 向上。使用@query和@queries将一些查询连接到一个查询中。在
^{pr21}$输出:
query{ leftComparison : hero( episode : EMPIRE ){ ...comparisonFields } rightComparison : hero( episode : JEDI ){ ...comparisonFields } } fragment comparisonFields on Character{ name appearsIn friends{ name } }
在片段中使用变量
struct = { "@queries": [{ '@args': { '$first': { 'Int': '3' } }, '@operation_name': 'HeroComparison', '@query': [{ 'hero': { '@alias': 'leftComparison', '@args': { 'episode': "EMPIRE" }, '@fields': ['...comparisonFields'] } }, { 'hero': { '@alias': 'rightComparison', '@args': { 'episode': "JEDI" }, '@fields': ['...comparisonFields'] } } ] }], "@fragments": [{ 'Character': { '@fragment_name': 'comparisonFields', '@fields': ['name', { 'friendsConnection': { '@args': { 'first': '$first' }, '@fields': ['totalCount', { 'edges': { '@fields': [{ 'node': { '@fields': ['name'] } }] } }] } }] } }] } print (GqlFromStruct.from_struct(struct))
输出:
query HeroComparison ( $first : Int = 3 ){ leftComparison : hero( episode : EMPIRE ){ ...comparisonFields } rightComparison : hero( episode : JEDI ){ ...comparisonFields } } fragment comparisonFields on Character{ name friendsConnection( first : $first ){ totalCount edges{ node{ name } } } }
操作名称
使用@operation_name关键字:
struct = { '@queries': [{ '@operation_name': 'HeroNameAndFriends', '@query': { 'hero': { '@fields': ['name', { 'friends': { '@fields': ['name'] } }] } } }] } print (GqlFromStruct.from_struct(struct))
输出:
query HeroNameAndFriends{ hero{ name friends{ name } } }
变量
在与@queries相同的高级嵌套中使用@variables块:
struct = { '@queries': [{ '@operation_name': 'HeroNameAndFriends', '@query': { 'hero': { '@fields': ['name', { 'friends': { '@fields': ['name'] } }] } } }], '@variables': { "episode": "JEDI" } } print (GqlFromStruct.from_struct(struct))
输出:
query HeroNameAndFriends{ hero{ name friends{ name } } } { "episode": "JEDI" }
默认变量
使用@fields关键字:
struct = { '@queries': [{ '@operation_name': 'HeroNameAndFriends', '@args': { '$episode': { 'Episode': 'JEDI' } }, '@query': { 'hero': { '@fields': ['name', { 'friends': { '@fields': ['name'] } }] } } }], '@variables': { "episode": "JEDI" } } print (GqlFromStruct.from_struct(struct))
输出:
query HeroNameAndFriends ( $episode : Episode = JEDI ){ hero{ name friends{ name } } } { "episode": "JEDI" }
指令
使用@directives关键字和@skip或@include作为指令:
^{pr31}$输出:
query Hero ( $episode : Episode, $withFriends : Boolean! ){ hero( episode : $episode ){ name friends @include (if : $withFriends){ name } } } { "episode": "JEDI" }
突变
使用@mutations关键字:
struct = { '@mutations': [{ '@operation_name': 'CreateReviewForEpisode', '@args': { '$episode': 'Episode!', '$review': 'ReviewInput!' }, '@query': { 'createReview': { '@args': { 'episode': '$ep', 'review': '$review' }, '@fields': ['stars', 'commentary'] } } }], '@variables': { "episode": "JEDI", "review": { "stars": 5, "commentary": "This is a great movie!" } } } print (GqlFromStruct.from_struct(struct))
输出:
mutation CreateReviewForEpisode ( $episode : Episode!, $review : ReviewInput! ){ createReview( episode : $ep, review : $review ){ stars commentary } } { "episode": "JEDI", "review": { "stars": 5, "commentary": "This is a great movie!" } }
内联片段
没什么特别需要的。在
^{pr35}$输出:
query HeroForEpisode ( $ep : Episode! ){ hero( episode : $ep ){ name ... on Droid{ primaryFunction } ... on Human{ height } } }
元字段
像普通字段一样使用元字段:
struct = { 'search': { '@args': { 'text': 'an' }, '@fields': ['__typename', { '... on Human': { '@fields': ['name'] } }, { '... on Droid': { '@fields': ['name'] } }, { '... on Starship': { '@fields': ['name'] } } ] } } print (GqlFromStruct.from_struct(struct))
输出:
query{ search( text : an ){ __typename ... on Human{ name } ... on Droid{ name } ... on Starship{ name } } }
- 项目
标签: