Skip to content

Commit c87e3c5

Browse files
authored
Merge pull request #31 from BillFarber/feature/submitGraphql
DEVEXP-575: Submit GraphQL query with Python client
2 parents 9a98197 + 8424b54 commit c87e3c5

File tree

16 files changed

+171
-8
lines changed

16 files changed

+171
-8
lines changed

marklogic/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import requests
22
from marklogic.cloud_auth import MarkLogicCloudAuth
33
from marklogic.documents import DocumentManager
4+
from marklogic.rows import RowManager
45
from requests.auth import HTTPDigestAuth
56
from urllib.parse import urljoin
67

@@ -70,3 +71,9 @@ def documents(self):
7071
if not hasattr(self, "_documents"):
7172
self._documents = DocumentManager(self)
7273
return self._documents
74+
75+
@property
76+
def rows(self):
77+
if not hasattr(self, "_rows"):
78+
self._rows = RowManager(self)
79+
return self._rows

marklogic/rows.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import json
2+
from requests import Session
3+
4+
"""
5+
Defines a RowManager class to simplify usage of the "/v1/rows" & "/v1/rows/graphql" REST
6+
endpoints defined at https://docs.marklogic.com/REST/POST/v1/rows/graphql
7+
"""
8+
9+
10+
class RowManager:
11+
"""
12+
Provides a method to simplify sending a GraphQL request to the GraphQL rows endpoint.
13+
"""
14+
def __init__(self, session: Session):
15+
self._session = session
16+
17+
def graphql(self, graphql_query, return_response=False, *args, **kwargs):
18+
"""
19+
Send a GraphQL query to MarkLogic via a POST to the endpoint defined at
20+
https://docs.marklogic.com/REST/POST/v1/rows/graphql
21+
22+
:param graphql_query: a GraphQL query string. Note - this is the query string
23+
only, not the entire query JSON object. See the following for more information:
24+
https://spec.graphql.org/October2021/#sec-Overview
25+
https://graphql.org/learn/queries/
26+
:param return_response: boolean specifying if the entire original response
27+
object should be returned (True) or if only the data should be returned (False)
28+
upon a success (2xx) response. Note that if the status code of the response is
29+
not 2xx, then the entire response is always returned.
30+
"""
31+
headers = kwargs.pop("headers", {})
32+
headers["Content-Type"] = "application/graphql"
33+
response = self._session.post(
34+
"v1/rows/graphql",
35+
headers=headers,
36+
data=json.dumps({"query": graphql_query}),
37+
**kwargs
38+
)
39+
return (
40+
response.json()
41+
if response.status_code == 200 and not return_response
42+
else response
43+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"database-name": "%%DATABASE%%",
3+
"schema-database": "%%SCHEMAS_DATABASE%%"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "%%MODULES_DATABASE%%"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"database-name": "%%SCHEMAS_DATABASE%%"
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
*=test-data
1+
*=test-data,search-test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*=test-data
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"musician": {
3+
"lastName": "Armstrong",
4+
"firstName": "Louis",
5+
"dob": "1901-08-04",
6+
"instrument": [
7+
"trumpet",
8+
"vocal"
9+
]
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"musician": {
3+
"lastName": "Byron",
4+
"firstName": "Don",
5+
"dob": "1958-11-08",
6+
"instrument": [
7+
"clarinet",
8+
"saxophone"
9+
]
10+
}
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"musician": {
3+
"lastName": "Coltrane",
4+
"firstName": "John",
5+
"dob": "1926-09-23",
6+
"instrument": [
7+
"saxophone"
8+
]
9+
}
10+
}

0 commit comments

Comments
 (0)