Skip to content

Commit 3521ac5

Browse files
Consolidate query result generation
1 parent cca0ea2 commit 3521ac5

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

labelbox/orm/query.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def attr_type(attr):
4141
for param, (_, attr) in params.items()) + ")"
4242

4343

44+
def results_query_part(entity):
45+
""" Generates the results part of the query. The results contain
46+
all the entity's fields as well as prefetched relationships.
47+
48+
Note that this is a recursive function. If there is a cycle in the
49+
prefetched relationship graph, this function will recurse infinitely.
50+
51+
Args:
52+
entity (type): The entity which needs fetching.
53+
"""
54+
return " ".join(field.graphql_name for field in entity.fields())
55+
56+
4457
class Query:
4558
""" A data structure used during the construction of a query. Supports
4659
subquery (also Query object) nesting for relationship. """
@@ -52,8 +65,8 @@ def __init__(self, what, subquery, where=None, paginate=False,
5265
what (str): What is being queried. Typically an object type in
5366
singular or plural (i.e. "project" or "projects").
5467
subquery (Query or type): Either a Query object that is formatted
55-
recursively or a Entity subtype in which case all it's public
56-
fields are retrieved by the query.
68+
recursively or a Entity subtype in which case the standard
69+
results (see `results_query_part`) are retrieved.
5770
where (None, Comparison or LogicalExpression): the filtering clause.
5871
paginate (bool): If the "%skip %first" pagination substring should
5972
be added to the query. Used for collection pagination in combination
@@ -72,7 +85,7 @@ def format_subquery(self):
7285
if isinstance(self.subquery, Query):
7386
return self.subquery.format()
7487
elif issubclass(self.subquery, Entity):
75-
return " ".join(f.graphql_name for f in self.subquery.fields()), {}
88+
return results_query_part(self.subquery), {}
7689
else:
7790
raise MalformedQueryException()
7891

@@ -313,7 +326,7 @@ def format_param_value(attribute, param):
313326
type_name,
314327
" ".join(format_param_value(attribute, param)
315328
for param, (_, attribute) in params.items()),
316-
" ".join(field.graphql_name for field in entity.fields()))
329+
results_query_part(entity))
317330

318331
return query_str, {name: value for name, (value, _) in params.items()}
319332

@@ -387,8 +400,7 @@ def create_metadata(meta_type, meta_value, data_row_id):
387400
metaType: $%s metaValue: $%s dataRowId: $%s}) {%s}} """ % (
388401
meta_type_param, meta_value_param, data_row_id_param,
389402
meta_type_param, meta_value_param, data_row_id_param,
390-
" ".join(field.graphql_name for field
391-
in Entity.named("AssetMetadata").fields()))
403+
results_query_part(Entity.named("AssetMetadata")))
392404
return query_str, {meta_type_param: meta_type,
393405
meta_value_param: meta_value,
394406
data_row_id_param: data_row_id}
@@ -466,7 +478,7 @@ def update_fields(db_object, values):
466478
type_name,
467479
id_param,
468480
values_str,
469-
" ".join(field.graphql_name for field in db_object.fields()))
481+
results_query_part(type(db_object)))
470482

471483
return query_str, {name: value for name, (value, _) in params.items()}
472484

@@ -516,8 +528,7 @@ def project_labels(project, datasets, order_by):
516528
query_str = """query GetProjectLabelsPyApi($project_id: ID!)
517529
{project (where: {id: $project_id})
518530
{labels (skip: %%d first: %%d%s%s) {%s}}}""" % (
519-
where, order_by_str, " ".join(f.graphql_name
520-
for f in label_entity.fields()))
531+
where, order_by_str, results_query_part(label_entity))
521532
return query_str, {"project_id": project.uid}
522533

523534

@@ -560,19 +571,16 @@ def bulk_delete(db_objects, use_where_clause):
560571

561572
def create_webhook(topics, url, secret, project):
562573
project_str = "" if project is None else ("project:{id:\"%s\"}," % project.uid)
563-
fields_str = " ".join(field.graphql_name for field
564-
in Entity.named("Webhook").fields())
565574

566575
query_str = """mutation CreateWebhookPyApi {
567576
createWebhook(data:{%s topics:{set:[%s]}, url:"%s", secret:"%s" }){%s}
568-
} """ % (project_str, " ".join(topics), url, secret, fields_str)
577+
} """ % (project_str, " ".join(topics), url, secret,
578+
results_query_part(Entity.named("Webhook")))
569579

570580
return query_str, {}
571581

572582

573583
def edit_webhook(webhook, topics, url, status):
574-
fields_str = " ".join(field.graphql_name for field in webhook.fields())
575-
576584
topics_str = "" if topics is None else "topics: {set: [%s]}" % " ".join(topics)
577585
url_str = "" if url is None else "url: \"%s\"" % url
578586
status_str = "" if status is None else "status: %s" % status
@@ -581,6 +589,6 @@ def edit_webhook(webhook, topics, url, status):
581589
updateWebhook(where: {id: "%s"} data:{%s}){%s}} """ % (
582590
webhook.uid,
583591
", ".join(filter(None, (topics_str, url_str, status_str))),
584-
fields_str)
592+
results_query_part(type(webhook)))
585593

586594
return query_str, {}

0 commit comments

Comments
 (0)