Skip to content

Commit e14404d

Browse files
authored
Remove query method from client.py (#61)
Other clients do not have the query as part of the client interface, only the transaction interface can do that. This change removes query from the client interface so that it matches the rest of the clients.
1 parent ff8e326 commit e14404d

File tree

7 files changed

+21
-43
lines changed

7 files changed

+21
-43
lines changed

examples/simple/simple.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def delete_data(client):
9999
}
100100
}"""
101101
variables1 = {'$a': 'Bob'}
102-
res1 = client.query(query1, variables=variables1)
102+
res1 = client.txn(read_only=True).query(query1, variables=variables1)
103103
ppl1 = json.loads(res1.json)
104104
for person in ppl1['all']:
105105
print('Query to find Uid for Bob :')
@@ -142,7 +142,7 @@ def query_data(client):
142142
}"""
143143

144144
variables = {'$a': 'Alice'}
145-
res = client.query(query, variables=variables)
145+
res = client.txn(read_only=True).query(query, variables=variables)
146146
ppl = json.loads(res.json)
147147

148148
# Print results.
@@ -178,7 +178,7 @@ def query_data01(client):
178178
}"""
179179

180180
variables01 = {'$b': 'Bob'}
181-
res01 = client.query(query01, variables=variables01)
181+
res01 = client.txn(read_only=True).query(query01, variables=variables01)
182182
ppl01 = json.loads(res01.json)
183183

184184
print('Number of people named "Bob": {}'.format(len(ppl01['all'])))

examples/tls/mutualtls_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def main():
4646
txn.commit()
4747
finally:
4848
txn.discard()
49-
49+
5050
# Query
51-
res = client.query('''
51+
res = client.txn(read_only=True).query('''
5252
query dgraph($name: string) {
5353
data(func: eq(name, $name)) {
5454
uid

pydgraph/client.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,6 @@ def alter(self, operation, timeout=None, metadata=None, credentials=None):
8686
else:
8787
raise error
8888

89-
90-
def query(self, query, variables=None, timeout=None, metadata=None,
91-
credentials=None):
92-
"""Runs a query via this client."""
93-
new_metadata = self.add_login_metadata(metadata)
94-
txn = self.txn(read_only=True)
95-
96-
try:
97-
return txn.query(query, variables=variables, timeout=timeout,
98-
metadata=new_metadata, credentials=credentials)
99-
except Exception as error:
100-
if util.is_jwt_expired(error):
101-
self.retry_login()
102-
new_metadata = self.add_login_metadata(metadata)
103-
return txn.query(query, variables=variables, timeout=timeout,
104-
metadata=new_metadata, credentials=credentials)
105-
else:
106-
raise error
107-
10889
def txn(self, read_only=False, best_effort=False):
10990
"""Creates a transaction."""
11091
return txn.Txn(self, read_only=read_only, best_effort=best_effort)

tests/test_acct_upsert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def assert_changes(self, firsts, accounts):
8989
}}
9090
}}""".format(' '.join(firsts))
9191
logging.debug(query)
92-
result = json.loads(self.client.query(query).json)
92+
result = json.loads(self.client.txn(read_only=True).query(query).json)
9393

9494
account_set = set()
9595
for acct in result['all']:

tests/test_essentials.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ class TestEssentials(helper.ClientIntegrationTestCase):
2828
"""Tests mutation after query behavior."""
2929

3030
def testMutationAfterQuery(self):
31-
"""Tests what happens when making a mutation on a txn after querying
32-
on the client."""
31+
"""Tests what happens when making a mutation on a txn after querying."""
3332

34-
_ = self.client.query('{firsts(func: has(first)) { uid first }}')
33+
_ = self.client.txn(read_only=True).query('{firsts(func: has(first)) { uid first }}')
3534

3635
txn = self.client.txn()
3736
mutation = txn.mutate(set_nquads='_:node <first> "Node name first" .')
@@ -43,7 +42,7 @@ def testMutationAfterQuery(self):
4342
txn.commit()
4443

4544
query = '{{node(func: uid({uid:s})) {{ uid }} }}'.format(uid=created)
46-
reread = self.client.query(query)
45+
reread = self.client.txn(read_only=True).query(query)
4746
self.assertEqual(created, json.loads(reread.json).get('node')[0]['uid'])
4847

4948

tests/test_queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_mutation_and_query(self):
5757
}
5858
}"""
5959

60-
response = self.client.query(query, variables={'$a': 'Alice'})
60+
response = self.client.txn().query(query, variables={'$a': 'Alice'})
6161
self.assertEqual([{'name': 'Alice', 'follows': [{'name': 'Greg'}]}],
6262
json.loads(response.json).get('me'))
6363
self.assertTrue(is_number(response.latency.parsing_ns),

tests/test_txn.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_commit_now(self):
7575
name
7676
}}
7777
}}""".format(uid=uid)
78-
resp = self.client.query(query)
78+
resp = self.client.txn(read_only=True).query(query)
7979
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))
8080

8181
def test_discard(self):
@@ -98,7 +98,7 @@ def test_discard(self):
9898
name
9999
}}
100100
}}""".format(uid=uid)
101-
resp = self.client.query(query)
101+
resp = self.client.txn(read_only=True).query(query)
102102
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))
103103

104104
def test_mutate_error(self):
@@ -143,7 +143,7 @@ def test_read_before_start_ts(self):
143143
}}
144144
}}""".format(uid=uid)
145145

146-
resp = self.client.query(query)
146+
resp = self.client.txn(read_only=True).query(query)
147147
self.assertEqual([], json.loads(resp.json).get('me'))
148148

149149
def test_read_after_start_ts(self):
@@ -163,7 +163,7 @@ def test_read_after_start_ts(self):
163163
}}
164164
}}""".format(uid=uid)
165165

166-
resp = self.client.query(query)
166+
resp = self.client.txn(read_only=True).query(query)
167167
self.assertEqual([{'name': 'Manish'}], json.loads(resp.json).get('me'))
168168

169169
def test_read_before_and_after_start_ts(self):
@@ -197,7 +197,7 @@ def test_read_before_and_after_start_ts(self):
197197
# once txn3 is committed, other txns observe the update
198198
txn3.commit()
199199

200-
resp4 = self.client.query(query)
200+
resp4 = self.client.txn(read_only=True).query(query)
201201
self.assertEqual([{'name': 'Manish2'}], json.loads(resp4.json).get('me'))
202202

203203
def test_read_from_new_client(self):
@@ -218,7 +218,7 @@ def test_read_from_new_client(self):
218218
}}
219219
}}""".format(uid=uid)
220220

221-
resp2 = client2.query(query)
221+
resp2 = client2.txn(read_only=True).query(query)
222222
self.assertEqual([{'name': 'Manish'}], json.loads(resp2.json).get('me'))
223223
self.assertTrue(resp2.txn.start_ts > 0)
224224

@@ -227,7 +227,7 @@ def test_read_from_new_client(self):
227227
self.assertTrue(assigned.context.start_ts > 0)
228228
txn2.commit()
229229

230-
resp = self.client.query(query)
230+
resp = self.client.txn(read_only=True).query(query)
231231
self.assertEqual([{'name': 'Manish2'}], json.loads(resp.json).get('me'))
232232

233233
def test_read_only_txn(self):
@@ -236,14 +236,12 @@ def test_read_only_txn(self):
236236

237237
query = '{ me() {} }'
238238

239-
# Using client.query helper method
240-
resp1 = self.client.query(query)
239+
resp1 = self.client.txn(read_only=True).query(query)
241240
start_ts1 = resp1.txn.start_ts
242-
resp2 = self.client.query(query)
241+
resp2 = self.client.txn(read_only=True).query(query)
243242
start_ts2 = resp2.txn.start_ts
244243
self.assertEqual(start_ts1, start_ts2)
245244

246-
# Using client.txn method
247245
txn = self.client.txn(read_only=True)
248246
resp1 = txn.query(query)
249247
start_ts1 = resp1.txn.start_ts
@@ -361,7 +359,7 @@ def test_commit_conflict(self):
361359
}}
362360
}}""".format(uid=uid)
363361

364-
resp4 = self.client.query(query)
362+
resp4 = self.client.txn(read_only=True).query(query)
365363
self.assertEqual([{'name': 'Jan the man'}], json.loads(resp4.json).get('me'))
366364

367365
def test_mutate_conflict(self):
@@ -408,7 +406,7 @@ def test_conflict_ignore(self):
408406
}
409407
}"""
410408

411-
resp = self.client.query(query)
409+
resp = self.client.txn(read_only=True).query(query)
412410
self.assertEqual([{'uid': uid1}, {'uid': uid2}], json.loads(resp.json).get('me'))
413411

414412
def test_read_index_key_same_txn(self):

0 commit comments

Comments
 (0)