mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Added reset_counter to ordered types. Improved node implementation
This commit is contained in:
parent
ab72393e66
commit
449b8c67d1
|
@ -1,3 +1,4 @@
|
||||||
|
import copy
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import six
|
import six
|
||||||
from graphql_relay import node_definitions, from_global_id
|
from graphql_relay import node_definitions, from_global_id
|
||||||
|
@ -20,9 +21,17 @@ class NodeMeta(InterfaceTypeMeta):
|
||||||
field_class=Field
|
field_class=Field
|
||||||
)
|
)
|
||||||
cls._meta.graphql_type = node_interface
|
cls._meta.graphql_type = node_interface
|
||||||
cls.Field = node_field
|
cls._Field = node_field
|
||||||
return constructed
|
return constructed
|
||||||
|
|
||||||
|
@property
|
||||||
|
def Field(cls):
|
||||||
|
# We put as a property for reset the field counter each time is setted up, so
|
||||||
|
# it will be order correctly wherever is mounted
|
||||||
|
field = copy.copy(cls._Field)
|
||||||
|
field.reset_counter()
|
||||||
|
return field
|
||||||
|
|
||||||
|
|
||||||
class Node(six.with_metaclass(NodeMeta, Interface)):
|
class Node(six.with_metaclass(NodeMeta, Interface)):
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ class MyNode(ObjectType):
|
||||||
|
|
||||||
|
|
||||||
class RootQuery(ObjectType):
|
class RootQuery(ObjectType):
|
||||||
|
first = String()
|
||||||
node = Node.Field
|
node = Node.Field
|
||||||
|
|
||||||
schema = Schema(query=RootQuery, types=[MyNode])
|
schema = Schema(query=RootQuery, types=[MyNode])
|
||||||
|
@ -59,3 +60,24 @@ def test_node_query_incorrect_id():
|
||||||
)
|
)
|
||||||
assert not executed.errors
|
assert not executed.errors
|
||||||
assert executed.data == {'node': None}
|
assert executed.data == {'node': None}
|
||||||
|
|
||||||
|
def test_str_schema():
|
||||||
|
assert str(schema) == """
|
||||||
|
schema {
|
||||||
|
query: RootQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
type MyNode implements Node {
|
||||||
|
id: ID!
|
||||||
|
name: String
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Node {
|
||||||
|
id: ID!
|
||||||
|
}
|
||||||
|
|
||||||
|
type RootQuery {
|
||||||
|
first: String
|
||||||
|
node(id: ID!): Node
|
||||||
|
}
|
||||||
|
""".lstrip()
|
|
@ -14,6 +14,9 @@ class OrderedType(object):
|
||||||
OrderedType.creation_counter += 1
|
OrderedType.creation_counter += 1
|
||||||
return counter
|
return counter
|
||||||
|
|
||||||
|
def reset_counter(self):
|
||||||
|
self.creation_counter = self.gen_counter()
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
# Needed for @total_ordering
|
# Needed for @total_ordering
|
||||||
if isinstance(self, type(other)):
|
if isinstance(self, type(other)):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user