Improved get_node API

This commit is contained in:
Syrus Akbary 2017-07-27 20:06:48 -07:00
parent 0002d42e38
commit 6a85507325
7 changed files with 42 additions and 17 deletions

View File

@ -165,15 +165,40 @@ class Query(ObjectType):
user_connection = relay.ConnectionField(UserConnection) 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 ## Mutation.mutate
Now only receive (`root`, `info`, `**args`) Now only receives (`root`, `info`, `**args`)
## ClientIDMutation.mutate_and_get_payload ## ClientIDMutation.mutate_and_get_payload
Now only receive (`root`, `info`, `**input`) Now only receives (`root`, `info`, `**input`)
## New Features ## New Features
@ -218,7 +243,7 @@ class UserInput(InputObjectType):
class Query(ObjectType): class Query(ObjectType):
user = graphene.Field(User, input=UserInput()) user = graphene.Field(User, input=UserInput())
def resolve_user(self, info, id): def resolve_user(self, info, input):
if input.is_valid: if input.is_valid:
return get_user(input.id) return get_user(input.id)
``` ```

View File

@ -21,7 +21,7 @@ subclass of ``relay.ClientIDMutation``.
faction = graphene.Field(Faction) faction = graphene.Field(Faction)
@classmethod @classmethod
def mutate_and_get_payload(cls, input, context, info): def mutate_and_get_payload(cls, root, info, **input):
ship_name = input.ship_name ship_name = input.ship_name
faction_id = input.faction_id faction_id = input.faction_id
ship = create_ship(ship_name, 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() success = graphene.String()
@classmethod @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 # When using it in Django, context will be the request
files = context.FILES files = context.FILES
# Or, if used in Flask, context will be the flask global request # Or, if used in Flask, context will be the flask global request

View File

@ -22,7 +22,7 @@ Example usage (taken from the `Starwars Relay example`_):
name = graphene.String(description='The name of the ship.') name = graphene.String(description='The name of the ship.')
@classmethod @classmethod
def get_node(cls, id, context, info): def get_node(cls, info, id):
return get_ship(id) return get_ship(id)
The ``id`` returned by the ``Ship`` type when you query it will be a 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) return '{}:{}'.format(type, id)
@staticmethod @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(':') type, id = global_id.split(':')
if only_node: if only_node:
# We assure that the node type that we want to retrieve # We assure that the node type that we want to retrieve

View File

@ -13,7 +13,7 @@ class Ship(graphene.ObjectType):
name = graphene.String(description='The name of the ship.') name = graphene.String(description='The name of the ship.')
@classmethod @classmethod
def get_node(cls, id, info): def get_node(cls, info, id):
return get_ship(id) return get_ship(id)
@ -37,7 +37,7 @@ class Faction(graphene.ObjectType):
return [get_ship(ship_id) for ship_id in self.ships] return [get_ship(ship_id) for ship_id in self.ships]
@classmethod @classmethod
def get_node(cls, id, info): def get_node(cls, info, id):
return get_faction(id) return get_faction(id)

View File

@ -56,7 +56,7 @@ class NodeField(Field):
) )
def get_resolver(self, parent_resolver): 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): class AbstractNode(Interface):
@ -81,11 +81,11 @@ class Node(AbstractNode):
return NodeField(cls, *args, **kwargs) return NodeField(cls, *args, **kwargs)
@classmethod @classmethod
def node_resolver(cls, root, info, id, only_type=None): def node_resolver(cls, only_type, root, info, id):
return cls.get_node_from_global_id(id, info, only_type) return cls.get_node_from_global_id(info, id, only_type=only_type)
@classmethod @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: try:
_type, _id = cls.from_global_id(global_id) _type, _id = cls.from_global_id(global_id)
graphene_type = info.schema.get_type(_type).graphene_type graphene_type = info.schema.get_type(_type).graphene_type
@ -103,7 +103,7 @@ class Node(AbstractNode):
get_node = getattr(graphene_type, 'get_node', None) get_node = getattr(graphene_type, 'get_node', None)
if get_node: if get_node:
return get_node(_id, info) return get_node(info, _id)
@classmethod @classmethod
def from_global_id(cls, global_id): def from_global_id(cls, global_id):

View File

@ -22,7 +22,7 @@ class MyNode(ObjectType):
name = String() name = String()
@staticmethod @staticmethod
def get_node(id, *_): def get_node(info, id):
return MyNode(name=str(id)) return MyNode(name=str(id))
@ -36,7 +36,7 @@ class MyOtherNode(SharedNodeFields, ObjectType):
return 'extra field info.' return 'extra field info.'
@staticmethod @staticmethod
def get_node(id, *_): def get_node(info, id):
return MyOtherNode(shared=str(id)) return MyOtherNode(shared=str(id))

View File

@ -15,7 +15,7 @@ class CustomNode(Node):
return id return id
@staticmethod @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 assert info.schema == schema
if id in user_data: if id in user_data:
return user_data.get(id) return user_data.get(id)