From 7af41373a75f89ed6bb7c324c0a8773cedd6643f Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 20 Nov 2015 11:19:22 +1100 Subject: [PATCH 1/4] Example of querying a Python object --- examples/field_example.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 examples/field_example.py diff --git a/examples/field_example.py b/examples/field_example.py new file mode 100644 index 00000000..f9218d0e --- /dev/null +++ b/examples/field_example.py @@ -0,0 +1,30 @@ +import graphene + +class Person(graphene.Interface): + name = graphene.String() + age = graphene.ID() + +class Patron(Person): + id = graphene.ID() + +class Query(graphene.ObjectType): + + patron = graphene.Field(Patron) + + def resolve_patron(self, args, info): + return Patron(id=1, name='Demo') + +schema = graphene.Schema(query=Query) +query = ''' + query something{ + patron { + id + name + } +} +''' +result = schema.execute(query) +# Print the result +print result.data['patron'] + + From 932e3d8b00768b1b3c103d3d44f714db5bb3a3e6 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 20 Nov 2015 11:47:10 +1100 Subject: [PATCH 2/4] Fix flake8 issues in field example --- examples/field_example.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/field_example.py b/examples/field_example.py index f9218d0e..f5ddada9 100644 --- a/examples/field_example.py +++ b/examples/field_example.py @@ -1,12 +1,15 @@ import graphene + class Person(graphene.Interface): name = graphene.String() age = graphene.ID() + class Patron(Person): id = graphene.ID() + class Query(graphene.ObjectType): patron = graphene.Field(Patron) @@ -26,5 +29,3 @@ query = ''' result = schema.execute(query) # Print the result print result.data['patron'] - - From 295fba37d16abfa437ef6886d93613258196d411 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 20 Nov 2015 11:55:32 +1100 Subject: [PATCH 3/4] field example: use print() to keep Python 3 flake 8 happy --- examples/field_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/field_example.py b/examples/field_example.py index f5ddada9..cc2d3eef 100644 --- a/examples/field_example.py +++ b/examples/field_example.py @@ -28,4 +28,4 @@ query = ''' ''' result = schema.execute(query) # Print the result -print result.data['patron'] +print(result.data['patron']) From f3f210b523f1733e48bb6316ecbb15e198dd503c Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 20 Nov 2015 16:25:18 +1100 Subject: [PATCH 4/4] Modify the field example to remove the use of interface --- examples/field_example.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/field_example.py b/examples/field_example.py index cc2d3eef..be41969d 100644 --- a/examples/field_example.py +++ b/examples/field_example.py @@ -1,15 +1,12 @@ import graphene -class Person(graphene.Interface): +class Patron(graphene.ObjectType): + id = graphene.ID() name = graphene.String() age = graphene.ID() -class Patron(Person): - id = graphene.ID() - - class Query(graphene.ObjectType): patron = graphene.Field(Patron) @@ -27,5 +24,4 @@ query = ''' } ''' result = schema.execute(query) -# Print the result print(result.data['patron'])