Skip to content

Commit d211b96

Browse files
committed
Nicer stringification of GraphQLFields
1 parent 628c996 commit d211b96

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ a query language for APIs created by Facebook.
1313
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
1414

1515
The current version 1.0.2 of GraphQL-core-next is up-to-date with GraphQL.js version
16-
14.2.1. All parts of the API are covered by an extensive test suite of currently 1742
16+
14.2.1. All parts of the API are covered by an extensive test suite of currently 1746
1717
unit tests.
1818

1919

graphql/type/definition.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,12 @@ def __init__(
446446
self.description = description
447447
self.ast_node = ast_node
448448

449+
def __repr__(self):
450+
return f"<{self.__class__.__name__} {self.type!r}>"
451+
452+
def __str__(self):
453+
return f"Field: {self.type}"
454+
449455
def __eq__(self, other):
450456
return self is other or (
451457
isinstance(other, GraphQLField)

tests/type/test_definition.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
GraphQLField,
1111
GraphQLInputField,
1212
GraphQLInputObjectType,
13+
GraphQLInt,
1314
GraphQLInterfaceType,
1415
GraphQLList,
1516
GraphQLNonNull,
1617
GraphQLObjectType,
1718
GraphQLScalarType,
19+
GraphQLString,
1820
GraphQLUnionType,
1921
)
2022

@@ -628,3 +630,17 @@ def simple_types_have_repr():
628630
repr(GraphQLList(ListOfScalarsType))
629631
== "<GraphQLList <GraphQLList <GraphQLScalarType 'Scalar'>>>"
630632
)
633+
634+
def stringifies_fields():
635+
assert str(GraphQLField(GraphQLNonNull(GraphQLString))) == "Field: String!"
636+
assert str(GraphQLField(GraphQLList(GraphQLInt))) == "Field: [Int]"
637+
638+
def fields_have_repr():
639+
assert (
640+
repr(GraphQLField(GraphQLNonNull(GraphQLString)))
641+
== "<GraphQLField <GraphQLNonNull <GraphQLScalarType 'String'>>>"
642+
)
643+
assert (
644+
repr(GraphQLField(GraphQLList(GraphQLInt)))
645+
== "<GraphQLField <GraphQLList <GraphQLScalarType 'Int'>>>"
646+
)

0 commit comments

Comments
 (0)