本文整理汇总了Python中graphql_relay.from_global_id方法的典型用法代码示例。如果您正苦于以下问题:Python graphql_relay.from_global_id方法的具体用法?Python graphql_relay.from_global_id怎么用?Python graphql_relay.from_global_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graphql_relay
的用法示例。
在下文中一共展示了graphql_relay.from_global_id方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
示例2: describe_convert_global_ids
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
示例3: get_node_from_global_id
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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)
示例4: from_global_id
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
def from_global_id(cls, global_id):
return from_global_id(global_id)
示例5: mutate_and_get_payload
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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)
示例6: default_resolver
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
示例7: _resolve_nodes
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
示例8: get_node
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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)
示例9: parse_value
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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)
示例10: extract_global_id
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
示例11: connection_resolver
# 需要导入模块: import graphql_relay [as 别名]
# 或者: from graphql_relay import from_global_id [as 别名]
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
)