Package: graphene-resolver
Graphene resolversUsing mongoose-like schema to write apollo-like resolver.Installpip install graphene-resolverUsagesimple example:importgrapheneimportgraphene_resolverasresolverclassFoo(resolver.Resolver):schema={"args":{"key":'String!',"value":'String!',},"type":'String!',}defresolve(self,**kwargs):self.parent# parent fieldself.info# resolve infoself.context# resolve contextreturnkwargs['value']classQuery(graphene.ObjectType):foo=Foo.as_field(){foo(key:"k",value:"v")}{"foo":"v"}relay node:pets=[dict(id=1,name='pet1',age=1,)]classPet(resolver.Resolver):schema={'type':{'name':'String','age':'Int',},'interfaces':(graphene.Node,)}defget_node(self,id_):returnnext(iforiinpetsifi['id']==int(id_))defvalidate(self,value):return(isinstance(value,typing.Mapping)andisinstance(value.get('name'),str)andisinstance(value.get('age'),int))classQuery(graphene.ObjectType):node=graphene.Node.Field()schema=graphene.Schema(query=Query,types=[Pet.as_type()]){node(id:"UGV0OjE="){id__typename...onPet{nameage}}}{"node":{"id":"UGV0OjE=","__typename":"Pet","name":"pet1","age":1}}relay connection:classItem(resolver.Resolver):schema={'name':'String!'}classItems(resolver.Resolver):schema=resolver.connection.get_type(Item)defresolve(self,**kwargs):returnresolver.connection.resolve([{'name':'a'},{'name':'b'}],**kwargs){items{edges{node{name}cursor}pageInfo{totalhasNextPagehasPreviousPagestartCursorendCursor}}}{"items":{"edges":[{"node":{"name":"a"},"cursor":"YXJyYXljb25uZWN0aW9uOjA="},{"node":{"name":"b"},"cursor":"YXJyYXljb25uZWN0aW9uOjE="}],"pageInfo":{"total":2,"hasNextPage":false,"hasPreviousPage":false,"startCursor":"YXJyYXljb25uZWN0aW9uOjA=","endCursor":"YXJyYXljb25uZWN0aW9uOjE="}}}enum:classFoo(resolver.Resolver):schema=('a','b')defresolve(self,**kwargs):return'a'classQuery(graphene.ObjectType):foo=Foo.as_field()schema=graphene.Schema(query=Query)assertstr(schema)=='''\schema {query: Query}enum Foo {ab}type Query {foo: Foo}'''enum with description:classFoo(resolver.Resolver):schema={'type':[('a','this is a'),('b','this is b'),'c'],'description':'A enum',}defresolve(self,**kwargs):return'a'classQuery(graphene.ObjectType):foo=Foo.as_field()schema=graphene.Schema(query=Query)enum_type=schema.get_type('Foo')assertenum_type.description=='A enum'assertenum_type.get_value('a').value=='a'assertenum_type.get_value('a').description=='this is a'assertenum_type.get_value('b').value=='b'assertenum_type.get_value('b').description=='this is b'assertenum_type.get_value('c').value=='c'assertenum_type.get_value('c').descriptionisNoneunion:classFoo(resolver.Resolver):schema=({'a':'String'},{'b':'Int'})defresolve(self,**kwargs):return{'__typename':'Foo0','a':'a'}classQuery(graphene.ObjectType):foo=Foo.as_field()schema=graphene.Schema(query=Query)assertstr(schema)=='''\schema {query: Query}union Foo = Foo0 | Foo1type Foo0 {a: String}type Foo1 {b: Int}type Query {foo: Foo}'''{foo{__typename...onFoo0{a}}}{"foo":{"__typename":"Foo0","a":"a"}}complicated example:classFoo(resolver.Resolver):_input_schema={"type":{"type":'String'},"data":[{"type":{"key":{"type":'String',"required":True,"description":"<description>",},"value":'Int',"extra":{"type":['String!'],"deprecation_reason":"<deprecated>"},},"required":True},],}schema={"args":{"input":_input_schema},"type":_input_schema,"description":"description","deprecation_reason":None}defresolve(self,**kwargs):returnkwargs['input']{foo(input:{type:"type",data:[{key:"key",value:42,extra:["extra"]}]}){typedata{keyvalueextra}}}{"foo":{"type":"type","data":[{"key":"key","value":42,"extra":["extra"]}]}}