From 6a85507325a078c354e76ce104d786d87038f0e0 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 27 Jul 2017 20:06:48 -0700 Subject: [PATCH] Improved get_node API --- UPGRADE-v2.0.md | 31 +++++++++++++++++++++--- docs/relay/mutations.rst | 4 +-- docs/relay/nodes.rst | 4 +-- examples/starwars_relay/schema.py | 4 +-- graphene/relay/node.py | 10 ++++---- graphene/relay/tests/test_node.py | 4 +-- graphene/relay/tests/test_node_custom.py | 2 +- 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/UPGRADE-v2.0.md b/UPGRADE-v2.0.md index a03a5e76..0e961bc3 100644 --- a/UPGRADE-v2.0.md +++ b/UPGRADE-v2.0.md @@ -165,15 +165,40 @@ class Query(ObjectType): user_connection = relay.ConnectionField(UserConnection) ``` +## Node.get_node + +The method `get_node` in `ObjectTypes` that have `Node` as interface, changes it's api. +From `def get_node(cls, id, context, info)` to `def get_node(cls, info, id)`. + +```python +class MyObject(ObjectType): + class Meta: + interfaces = (Node, ) + + @classmethod + def get_node(cls, id, context, info): + return ... +``` + +To: +```python +class MyObject(ObjectType): + class Meta: + interfaces = (Node, ) + + @classmethod + def get_node(cls, info, id): + return ... +``` ## Mutation.mutate -Now only receive (`root`, `info`, `**args`) +Now only receives (`root`, `info`, `**args`) ## ClientIDMutation.mutate_and_get_payload -Now only receive (`root`, `info`, `**input`) +Now only receives (`root`, `info`, `**input`) ## New Features @@ -218,7 +243,7 @@ class UserInput(InputObjectType): class Query(ObjectType): user = graphene.Field(User, input=UserInput()) - def resolve_user(self, info, id): + def resolve_user(self, info, input): if input.is_valid: return get_user(input.id) ``` diff --git a/docs/relay/mutations.rst b/docs/relay/mutations.rst index 25c2fd54..89bf89b3 100644 --- a/docs/relay/mutations.rst +++ b/docs/relay/mutations.rst @@ -21,7 +21,7 @@ subclass of ``relay.ClientIDMutation``. faction = graphene.Field(Faction) @classmethod - def mutate_and_get_payload(cls, input, context, info): + def mutate_and_get_payload(cls, root, info, **input): ship_name = input.ship_name faction_id = input.faction_id ship = create_ship(ship_name, faction_id) @@ -46,7 +46,7 @@ Mutations can also accept files, that's how it will work with different integrat success = graphene.String() @classmethod - def mutate_and_get_payload(cls, input, context, info): + def mutate_and_get_payload(cls, root, info, **input): # When using it in Django, context will be the request files = context.FILES # Or, if used in Flask, context will be the flask global request diff --git a/docs/relay/nodes.rst b/docs/relay/nodes.rst index 5f470055..74f42094 100644 --- a/docs/relay/nodes.rst +++ b/docs/relay/nodes.rst @@ -22,7 +22,7 @@ Example usage (taken from the `Starwars Relay example`_): name = graphene.String(description='The name of the ship.') @classmethod - def get_node(cls, id, context, info): + def get_node(cls, info, id): return get_ship(id) The ``id`` returned by the ``Ship`` type when you query it will be a @@ -55,7 +55,7 @@ Example of a custom node: return '{}:{}'.format(type, id) @staticmethod - def get_node_from_global_id(global_id, context, info, only_type=None): + def get_node_from_global_id(info global_id, only_type=None): type, id = global_id.split(':') if only_node: # We assure that the node type that we want to retrieve diff --git a/examples/starwars_relay/schema.py b/examples/starwars_relay/schema.py index 2d004e42..beb291c3 100644 --- a/examples/starwars_relay/schema.py +++ b/examples/starwars_relay/schema.py @@ -13,7 +13,7 @@ class Ship(graphene.ObjectType): name = graphene.String(description='The name of the ship.') @classmethod - def get_node(cls, id, info): + def get_node(cls, info, id): return get_ship(id) @@ -37,7 +37,7 @@ class Faction(graphene.ObjectType): return [get_ship(ship_id) for ship_id in self.ships] @classmethod - def get_node(cls, id, info): + def get_node(cls, info, id): return get_faction(id) diff --git a/graphene/relay/node.py b/graphene/relay/node.py index bc6a0870..d762a110 100644 --- a/graphene/relay/node.py +++ b/graphene/relay/node.py @@ -56,7 +56,7 @@ class NodeField(Field): ) def get_resolver(self, parent_resolver): - return partial(self.node_type.node_resolver, only_type=get_type(self.field_type)) + return partial(self.node_type.node_resolver, get_type(self.field_type)) class AbstractNode(Interface): @@ -81,11 +81,11 @@ class Node(AbstractNode): return NodeField(cls, *args, **kwargs) @classmethod - def node_resolver(cls, root, info, id, only_type=None): - return cls.get_node_from_global_id(id, info, only_type) + def node_resolver(cls, only_type, root, info, id): + return cls.get_node_from_global_id(info, id, only_type=only_type) @classmethod - def get_node_from_global_id(cls, global_id, info, only_type=None): + def get_node_from_global_id(cls, info, global_id, only_type=None): try: _type, _id = cls.from_global_id(global_id) graphene_type = info.schema.get_type(_type).graphene_type @@ -103,7 +103,7 @@ class Node(AbstractNode): get_node = getattr(graphene_type, 'get_node', None) if get_node: - return get_node(_id, info) + return get_node(info, _id) @classmethod def from_global_id(cls, global_id): diff --git a/graphene/relay/tests/test_node.py b/graphene/relay/tests/test_node.py index e0f65cdd..315f2e39 100644 --- a/graphene/relay/tests/test_node.py +++ b/graphene/relay/tests/test_node.py @@ -22,7 +22,7 @@ class MyNode(ObjectType): name = String() @staticmethod - def get_node(id, *_): + def get_node(info, id): return MyNode(name=str(id)) @@ -36,7 +36,7 @@ class MyOtherNode(SharedNodeFields, ObjectType): return 'extra field info.' @staticmethod - def get_node(id, *_): + def get_node(info, id): return MyOtherNode(shared=str(id)) diff --git a/graphene/relay/tests/test_node_custom.py b/graphene/relay/tests/test_node_custom.py index 273f89fd..cc4e910c 100644 --- a/graphene/relay/tests/test_node_custom.py +++ b/graphene/relay/tests/test_node_custom.py @@ -15,7 +15,7 @@ class CustomNode(Node): return id @staticmethod - def get_node_from_global_id(id, info, only_type=None): + def get_node_from_global_id(info, id, only_type=None): assert info.schema == schema if id in user_data: return user_data.get(id)