Last active
May 28, 2018 06:49
-
-
Save ocavue/3d1d812a6c828af231f057e923f3e0af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for https://github.com/graphql-python/graphene/issues/729 | |
import json | |
from graphene import ( | |
Boolean, | |
Field, | |
Interface, | |
List, | |
NonNull, | |
ObjectType, | |
Schema, | |
String, | |
) | |
class AnnotationObjectType(ObjectType): | |
def __init_subclass__(cls, *args, **kwargs): | |
fields = [] | |
for name, func in cls.__dict__.items(): | |
if name != "Meta" and not name.startswith("__"): # __init__ etc ... | |
fields.append((name, func)) | |
for name, func in fields: | |
setattr(cls, "resolve_{}".format(name), func) | |
setattr( | |
cls, | |
name, | |
Field(func.__annotations__.pop("return"), **func.__annotations__), | |
) | |
super().__init_subclass__(*args, **kwargs) | |
class Persion(Interface): | |
name = Field(List(String), only_first_name=Boolean()) | |
class Hero(ObjectType): | |
class Meta: | |
interfaces = (Persion,) | |
wife = Field(lambda: Heroine) | |
best_friend = Field(Persion) | |
def resolve_name(self, info, only_first_name=False): | |
if only_first_name: | |
return ["Scott"] | |
return ["Scott", "Lang"] | |
def resolve_wife(self, info): | |
return Heroine() | |
def resolve_best_friend(self, info): | |
return Heroine() | |
class Heroine(AnnotationObjectType): | |
class Meta: | |
interfaces = (Persion,) | |
def name(self, info, only_first_name: Boolean() = False) -> List(String): | |
if only_first_name: | |
return ["Hope"] | |
return ["Hope", "Van", "Dyne"] | |
def husband(self, info) -> Hero: | |
return Hero() | |
def best_friend(self, info) -> Persion: | |
return Hero() | |
class Query(ObjectType): | |
hero = Field(NonNull(Hero)) | |
heroine = Field(NonNull(Heroine)) | |
def resolve_hero(self, info): | |
return Hero() | |
def resolve_heroine(self, info): | |
return Heroine() | |
schema = Schema(Query, auto_camelcase=False) | |
result = schema.execute( | |
""" | |
query { | |
hero { | |
name | |
wife { # AnnotationObjectType | |
name ( only_first_name: true ) | |
} | |
best_friend { # Interface | |
name ( only_first_name: true ) | |
} | |
} | |
heroine { | |
name | |
husband { # normal ObjectType | |
name ( only_first_name: true ) | |
} | |
best_friend { # Interface | |
name ( only_first_name: true ) | |
} | |
} | |
} | |
""" | |
) | |
if result.data: | |
# from pprint import pprint | |
# pprint(json.loads(json.dumps(result.data))) | |
print(json.dumps(result.data, indent=4)) | |
""" | |
result: | |
{'hero': {'best_friend': {'name': ['Hope']}, | |
'name': ['Scott', 'Lang'], | |
'wife': {'name': ['Hope']}}, | |
'heroine': {'best_friend': {'name': ['Scott']}, | |
'husband': {'name': ['Scott']}, | |
'name': ['Hope', 'Van', 'Dyne']}} | |
""" | |
else: | |
print(result.errors) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment