gql.transport.requests¶
- class gql.transport.requests.RequestsHTTPTransport(url: str, headers: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, cookies: ~typing.Optional[~typing.Union[~typing.Dict[str, ~typing.Any], ~requests.cookies.RequestsCookieJar]] = None, auth: ~typing.Optional[~requests.auth.AuthBase] = None, use_json: bool = True, timeout: ~typing.Optional[int] = None, verify: ~typing.Union[bool, str] = True, retries: int = 0, method: str = 'POST', retry_backoff_factor: float = 0.1, retry_status_forcelist: ~typing.Collection[int] = (429, 500, 502, 503, 504), json_serialize: ~typing.Callable = <function dumps>, json_deserialize: ~typing.Callable = <function loads>, **kwargs: ~typing.Any)¶
Bases:
Transport
Sync Transport used to execute GraphQL queries on remote servers.
The transport uses the requests library to send HTTP POST requests.
- file_classes: Tuple[Type[Any], ...] = (<class 'io.IOBase'>,)¶
- __init__(url: str, headers: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, cookies: ~typing.Optional[~typing.Union[~typing.Dict[str, ~typing.Any], ~requests.cookies.RequestsCookieJar]] = None, auth: ~typing.Optional[~requests.auth.AuthBase] = None, use_json: bool = True, timeout: ~typing.Optional[int] = None, verify: ~typing.Union[bool, str] = True, retries: int = 0, method: str = 'POST', retry_backoff_factor: float = 0.1, retry_status_forcelist: ~typing.Collection[int] = (429, 500, 502, 503, 504), json_serialize: ~typing.Callable = <function dumps>, json_deserialize: ~typing.Callable = <function loads>, **kwargs: ~typing.Any)¶
Initialize the transport with the given request parameters.
- Parameters
url – The GraphQL server URL.
headers – Dictionary of HTTP Headers to send with the
Request
(Default: None).cookies – Dict or CookieJar object to send with the
Request
(Default: None).auth – Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth (Default: None).
use_json – Send request body as JSON instead of form-urlencoded (Default: True).
timeout – Specifies a default timeout for requests (Default: None).
verify – Either a boolean, in which case it controls whether we verify the server’s TLS certificate, or a string, in which case it must be a path to a CA bundle to use. (Default: True).
retries – Pre-setup of the requests’ Session for performing retries
method – HTTP method used for requests. (Default: POST).
retry_backoff_factor – A backoff factor to apply between attempts after the second try. urllib3 will sleep for: {backoff factor} * (2 ** ({number of previous retries}))
retry_status_forcelist – A set of integer HTTP status codes that we should force a retry on. A retry is initiated if the request method is in allowed_methods and the response status code is in status_forcelist. (Default: [429, 500, 502, 503, 504])
json_serialize – Json serializer callable. By default json.dumps() function
json_deserialize – Json deserializer callable. By default json.loads() function
kwargs – Optional arguments that
request
takes. These can be seen at the requests source code or the official docs
- connect()¶
Establish a session with the transport.
- execute(document: DocumentNode, variable_values: Optional[Dict[str, Any]] = None, operation_name: Optional[str] = None, timeout: Optional[int] = None, extra_args: Optional[Dict[str, Any]] = None, upload_files: bool = False) ExecutionResult ¶
Execute GraphQL query.
Execute the provided document AST against the configured remote server. This uses the requests library to perform a HTTP POST request to the remote server.
- Parameters
document – GraphQL query as AST Node object.
variable_values – Dictionary of input parameters (Default: None).
operation_name – Name of the operation that shall be executed. Only required in multi-operation documents (Default: None).
timeout – Specifies a default timeout for requests (Default: None).
extra_args – additional arguments to send to the requests post method
upload_files – Set to True if you want to put files in the variable values
- Returns
The result of execution. data is the result of executing the query, errors is null if no errors occurred, and is a non-empty array if an error occurred.
- execute_batch(reqs: List[GraphQLRequest], timeout: Optional[int] = None, extra_args: Optional[Dict[str, Any]] = None) List[ExecutionResult] ¶
Execute multiple GraphQL requests in a batch.
Execute the provided requests against the configured remote server. This uses the requests library to perform a HTTP POST request to the remote server.
- Parameters
reqs – GraphQL requests as a list of GraphQLRequest objects.
timeout – Specifies a default timeout for requests (Default: None).
extra_args – additional arguments to send to the requests post method
- Returns
A list of results of execution. For every result data is the result of executing the query, errors is null if no errors occurred, and is a non-empty array if an error occurred.
- close()¶
Closing the transport by closing the inner session