mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-31 16:07:27 +03:00 
			
		
		
		
	Improved get_node API
This commit is contained in:
		
							parent
							
								
									0002d42e38
								
							
						
					
					
						commit
						6a85507325
					
				|  | @ -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) | ||||
| ``` | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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 | ||||
|  |  | |||
|  | @ -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) | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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): | ||||
|  |  | |||
|  | @ -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)) | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user