Skip to content

Commit 83a33fa

Browse files
author
Matt Sokoloff
committed
consistent query format
1 parent 9f6655e commit 83a33fa

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

labelbox/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def convert_value(value):
126126

127127
if experimental:
128128
endpoint = self.endpoint.replace('graphql', '_gql')
129-
logger.warn(
129+
logger.info(
130130
"Experimental queries/mutations aren't guarenteed to maintain their interface."
131131
)
132132

labelbox/pagination.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,23 @@ def get_next_cursor(self, results):
6464
results = results[path]
6565
return results
6666

67+
def get_query(self):
68+
if self.cursor_path:
69+
# Cursor Pagination
70+
self.params.update({'from': self.next_cursor, 'first': _PAGE_SIZE})
71+
return self.query
72+
73+
# Offset Pagination
74+
return self.query % (self._fetched_pages * _PAGE_SIZE, _PAGE_SIZE)
75+
6776
def __next__(self):
6877
if len(self._data) <= self._data_ind:
6978
if self._fetched_all:
7079
raise StopIteration()
7180

72-
if self.cursor_path:
73-
# Cursor Pagination
74-
self.params.update({
75-
'from': self.next_cursor,
76-
'first': _PAGE_SIZE
77-
})
78-
query = self.query
79-
else:
80-
# Offset Pagination
81-
query = self.query % (self._fetched_pages * _PAGE_SIZE,
82-
_PAGE_SIZE)
83-
8481
self._fetched_pages += 1
8582

86-
results = self.client.execute(query,
83+
results = self.client.execute(self.get_query(),
8784
self.params,
8885
experimental=self.experimental)
8986
page_data = self.get_page_data(results)

labelbox/schema/organization.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ def _assign_user_role(self, email: str, role: Role,
7272
if self.client.get_user().email == email:
7373
raise ValueError("Cannot update your own role")
7474

75-
query_str = """mutation createInvitesPyApi($data: [CreateInviteInput!]){
76-
createInvites(data: $data){ invite { id createdAt organizationRoleName inviteeEmail}}}"""
75+
data_param = "data"
76+
query_str = """mutation createInvitesPyApi($%s: [CreateInviteInput!]){
77+
createInvites(data: $%s){ invite { id createdAt organizationRoleName inviteeEmail}}}""" % (
78+
data_param, data_param)
7779

7880
projects = [{
7981
"projectId": x.project.uid,
@@ -82,7 +84,7 @@ def _assign_user_role(self, email: str, role: Role,
8284

8385
res = self.client.execute(
8486
query_str, {
85-
'data': [{
87+
data_param: [{
8688
"inviterId": self.client.get_user().uid,
8789
"inviteeEmail": email,
8890
"organizationId": self.uid,
@@ -168,12 +170,11 @@ def invite_limit(self) -> InviteLimit:
168170
InviteLimit
169171
170172
"""
171-
172-
res = self.client.execute(
173-
"""query InvitesLimitPyApi($organizationId: ID!) {
174-
invitesLimit(where: {id: $organizationId}) { used limit remaining }
175-
}""", {"organizationId": self.uid},
176-
experimental=True)
173+
org_id_param = "organizationId"
174+
res = self.client.execute("""query InvitesLimitPyApi($%s: ID!) {
175+
invitesLimit(where: {id: $%s}) { used limit remaining }
176+
}""" % (org_id_param, org_id_param), {org_id_param: self.uid},
177+
experimental=True)
177178
return InviteLimit(
178179
**{utils.snake_case(k): v for k, v in res['invitesLimit'].items()})
179180

@@ -188,7 +189,8 @@ def remove_user(self, user: User):
188189
if not isinstance(user, User):
189190
raise TypeError(f"Expected user to be of type User, found {user}")
190191

192+
user_id_param = "userId"
191193
self.client.execute(
192-
"""mutation DeleteMemberPyApi($id: ID!) {
193-
updateUser(where: {id: $id}, data: {deleted: true}) { id deleted }
194-
}""", {'id': user.uid})
194+
"""mutation DeleteMemberPyApi($%s: ID!) {
195+
updateUser(where: {id: $%s}, data: {deleted: true}) { id deleted }
196+
}""" % (user_id_param, user_id_param), {user_id_param: user.uid})

labelbox/schema/project.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ def invites(self):
8686
A `PaginatedCollection` of `ProjectInvite`s.
8787
8888
"""
89-
90-
query_str = """query GetProjectInvitationsPyApi($from: ID, $first: PageSize, $projectId: ID!) {
91-
project(where: {id: $projectId}) {id
92-
invites(from: $from, first: $first) { nodes { id createdAt organizationRoleName inviteeEmail
89+
id_param = "projectId"
90+
query_str = """query GetProjectInvitationsPyApi($from: ID, $first: PageSize, $%s: ID!) {
91+
project(where: {id: $%s}) {id
92+
invites(from: $from, first: $first) { nodes { %s
9393
projectInvites { projectId projectRoleName } } nextCursor}}}
94-
"""
94+
""" % (id_param, id_param, query.results_query_part(Entity.Invite))
95+
9596
return PaginatedCollection(
9697
self.client,
97-
query_str, {"projectId": self.uid}, ['project', 'invites', 'nodes'],
98+
query_str, {id_param: self.uid}, ['project', 'invites', 'nodes'],
9899
Invite,
99100
cursor_path=['project', 'invites', 'nextCursor'],
100101
experimental=True)

labelbox/schema/user.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ def update_org_role(self, role: Role):
5050
role (Role): The role that you want to set for this user.
5151
5252
"""
53+
user_id_param = "userId"
54+
role_id_param = "roleId"
55+
query_str = """mutation SetOrganizationRolePyApi($%s: ID!, $%s: ID!) {
56+
setOrganizationRole(data: {userId: $userId, roleId: $roleId}) { id name }}
57+
""" % (user_id_param, role_id_param)
5358

54-
self.client.execute(
55-
"""mutation SetOrganizationRolePyApi($userId: ID!, $roleId: ID!) { setOrganizationRole(data: {userId: $userId, roleId: $roleId}) { id name }}""",
56-
{
57-
"userId": self.uid,
58-
"roleId": role.uid
59-
})
59+
self.client.execute(query_str, {
60+
user_id_param: self.uid,
61+
role_id_param: role.uid
62+
})
6063

6164
def remove_from_project(self, project: Project):
6265
""" Removes a User from a project. Only used for project based users.
@@ -87,11 +90,17 @@ def upsert_project_role(self, project: Project, role: Role):
8790
if not isinstance(project, Project):
8891
raise TypeError(f"Must provide a `Project` object. Found {project}")
8992

93+
project_id_param = "projectId"
94+
user_id_param = "userId"
95+
role_id_param = "roleId"
96+
query_str = """mutation SetProjectMembershipPyApi($%s: ID!, $%s: ID!, $%s: ID!) {
97+
setProjectMembership(data: {%s: $userId, roleId: $%s, projectId: $%s}) {id}}
98+
""" % (user_id_param, role_id_param, project_id_param, user_id_param,
99+
role_id_param, project_id_param)
100+
90101
self.client.execute(
91-
"""mutation SetProjectMembershipPyApi($projectId: ID!, $userId: ID!, $roleId: ID!) {
92-
setProjectMembership(data: {userId: $userId, roleId: $roleId, projectId: $projectId}) {id}}
93-
""", {
94-
"projectId": project.uid,
95-
"roleId": role.uid,
96-
"userId": self.uid
102+
query_str, {
103+
project_id_param: project.uid,
104+
user_id_param: self.uid,
105+
role_id_param: role.uid
97106
})

0 commit comments

Comments
 (0)