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+ )
0 commit comments