Python graphql_relay.from_global_id() Examples
The following are 14
code examples of graphql_relay.from_global_id().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
graphql_relay
, or try the search function
.
Example #1
Source File: forms.py From graphene-django with MIT License | 6 votes |
def clean(self, value): if not value and not self.required: return None try: _type, _id = from_global_id(value) except (TypeError, ValueError, UnicodeDecodeError, binascii.Error): raise ValidationError(self.error_messages["invalid"]) try: CharField().clean(_id) CharField().clean(_type) except ValidationError: raise ValidationError(self.error_messages["invalid"]) return value
Example #2
Source File: test_node.py From graphql-relay-py with MIT License | 6 votes |
def describe_convert_global_ids(): def to_global_id_converts_unicode_strings_correctly(): my_unicode_id = "ûñö" g_id = to_global_id("MyType", my_unicode_id) assert g_id == "TXlUeXBlOsO7w7HDtg==" my_unicode_id = "\u06ED" g_id = to_global_id("MyType", my_unicode_id) assert g_id == "TXlUeXBlOtut" def from_global_id_converts_unicode_strings_correctly(): my_unicode_id = "ûñö" my_type, my_id = from_global_id("TXlUeXBlOsO7w7HDtg==") assert my_type == "MyType" assert my_id == my_unicode_id my_unicode_id = "\u06ED" my_type, my_id = from_global_id("TXlUeXBlOtut") assert my_type == "MyType" assert my_id == my_unicode_id
Example #3
Source File: node.py From graphene with MIT License | 5 votes |
def get_node_from_global_id(cls, info, global_id, only_type=None): try: _type, _id = cls.from_global_id(global_id) except Exception as e: raise Exception( ( f'Unable to parse global ID "{global_id}". ' 'Make sure it is a base64 encoded string in the format: "TypeName:id". ' f"Exception message: {str(e)}" ) ) graphene_type = info.schema.get_type(_type) if graphene_type is None: raise Exception(f'Relay Node "{_type}" not found in schema') graphene_type = graphene_type.graphene_type if only_type: assert ( graphene_type == only_type ), f"Must receive a {only_type._meta.name} id." # We make sure the ObjectType implements the "Node" interface if cls not in graphene_type._meta.interfaces: raise Exception( f'ObjectType "{_type}" does not implement the "{cls}" interface.' ) get_node = getattr(graphene_type, "get_node", None) if get_node: return get_node(info, _id)
Example #4
Source File: node.py From graphene with MIT License | 5 votes |
def from_global_id(cls, global_id): return from_global_id(global_id)
Example #5
Source File: schema.py From graphene-permissions with MIT License | 5 votes |
def mutate_and_get_payload(cls, root, info, **input): if cls.has_permission(root, info, input): owner = User.objects.get(pk=from_global_id(input['owner'])[1]) pet = Pet.objects.create(name=input['name'], race=input['race'], owner=owner) return SuperUserAddPet(pet=pet, status=HTTPStatus.CREATED) return SuperUserAddPet(pet=None, status=HTTPStatus.BAD_REQUEST)
Example #6
Source File: schema.py From graphene-permissions with MIT License | 5 votes |
def mutate_and_get_payload(cls, root, info, **input): if cls.has_permission(root, info, input): owner = User.objects.get(pk=from_global_id(input['owner'])[1]) pet = Pet.objects.create(name=input['name'], race=input['race'], owner=owner) return StaffAddPet(pet=pet, status=HTTPStatus.CREATED) return StaffAddPet(pet=None, status=HTTPStatus.BAD_REQUEST)
Example #7
Source File: schema.py From graphene-permissions with MIT License | 5 votes |
def mutate_and_get_payload(cls, root, info, **input): if cls.has_permission(root, info, input): owner = User.objects.get(pk=from_global_id(input['owner'])[1]) pet = Pet.objects.create(name=input['name'], race=input['race'], owner=owner) return AuthenticatedAddPet(pet=pet, status=HTTPStatus.CREATED) return AuthenticatedAddPet(pet=None, status=HTTPStatus.BAD_REQUEST)
Example #8
Source File: schema.py From graphene-permissions with MIT License | 5 votes |
def mutate_and_get_payload(cls, root, info, **input): if cls.has_permission(root, info, input): owner = User.objects.get(pk=from_global_id(input['owner'])[1]) pet = Pet.objects.create(name=input['name'], race=input['race'], owner=owner) return AddPet(pet=pet, status=HTTPStatus.CREATED) return AddPet(pet=None, status=HTTPStatus.BAD_REQUEST)
Example #9
Source File: fields.py From graphene-mongo with MIT License | 5 votes |
def default_resolver(self, _root, info, **args): args = args or {} if _root is not None: args["pk__in"] = [r.pk for r in getattr(_root, info.field_name, [])] connection_args = { "first": args.pop("first", None), "last": args.pop("last", None), "before": args.pop("before", None), "after": args.pop("after", None), } _id = args.pop('id', None) if _id is not None: args['pk'] = from_global_id(_id)[-1] if callable(getattr(self.model, "objects", None)): iterables = self.get_queryset(self.model, info, **args) list_length = iterables.count() else: iterables = [] list_length = 0 connection = connection_from_list_slice( list_slice=iterables, args=connection_args, list_length=list_length, list_slice_length=list_length, connection_type=self.type, edge_type=self.type.Edge, pageinfo_type=graphene.PageInfo, ) connection.iterable = iterables connection.list_length = list_length return connection
Example #10
Source File: utils.py From graphene-django-plus with MIT License | 5 votes |
def _resolve_nodes(ids, graphene_type=None): pks = [] invalid_ids = [] used_type = graphene_type for graphql_id in ids: if not graphql_id: continue try: node_type, _id = from_global_id(graphql_id) except Exception: invalid_ids.append(graphql_id) continue if used_type: if str(used_type) != node_type: raise AssertionError( "Must receive a {} id.".format(str(used_type)) ) used_type = node_type pks.append(_id) if invalid_ids: # pragma: no cover raise GraphQLError( "Could not resolve to a node with the id list of '{}'.".format( invalid_ids, ), ) return used_type, pks
Example #11
Source File: utils.py From graphene-django-plus with MIT License | 5 votes |
def get_node(id, graphene_type=None): """Get a node given the relay id.""" node_type, _id = from_global_id(id) if not graphene_type: graphene_type = _resolve_graphene_type(node_type) return graphene_type._meta.model.objects.get(pk=_id)
Example #12
Source File: types.py From django-graphql-extensions with MIT License | 5 votes |
def parse_value(cls, value): try: _type, _id = from_global_id(value) except (TypeError, ValueError): raise exceptions.ValidationError(_('ID cannot be resolved')) return int(_id)
Example #13
Source File: relay.py From caluma with GNU General Public License v3.0 | 5 votes |
def extract_global_id(id): """ Extract id from global id throwing away type. In case it is not a base64 encoded value it will return id as is. This way it can be used as input type by simply defining id without its specific type. """ try: _, result = from_global_id(id) except ValueError: result = id return result
Example #14
Source File: fields.py From graphql-pynamodb with MIT License | 4 votes |
def connection_resolver(cls, resolver, connection, model, root, info, **args): iterable = resolver(root, info, **args) first = args.get('first') last = args.get('last') (_, after) = from_global_id(args.get('after')) if args.get('after') else (None, None) (_, before) = from_global_id(args.get('before')) if args.get('before') else (None, None) has_previous_page = bool(after) page_size = first if first else last if last else None # get a full scan query since we have no resolved iterable from relationship or resolver function if not iterable and not root: query = cls.get_query(model, info, **args) iterable = query() if first or last or after or before: raise NotImplementedError( "DynamoDB scan operations have no predictable sort. Arguments first, last, after " + "and before will have unpredictable results") iterable = iterable if isinstance(iterable, list) else list(iterable) if iterable else [] if last: iterable = iterable[-last:] (has_next, edges) = cls.get_edges_from_iterable(iterable, model, info, edge_type=connection.Edge, after=after, page_size=page_size) key_name = get_key_name(model) try: start_cursor = getattr(edges[0].node, key_name) end_cursor = getattr(edges[-1].node, key_name) except IndexError: start_cursor = None end_cursor = None optional_args = {} total_count = len(iterable) if 'total_count' in connection._meta.fields: optional_args["total_count"] = total_count # Construct the connection return connection( edges=edges, page_info=PageInfo( start_cursor=start_cursor if start_cursor else '', end_cursor=end_cursor if end_cursor else '', has_previous_page=has_previous_page, has_next_page=has_next ), **optional_args )