Registry / http-networking / python-graphql-client

python-graphql-client

JSON →
library0.4.3pypypi✓ verified 85d ago

A Python client for interacting with GraphQL APIs, offering synchronous, asynchronous, and websocket-based subscription capabilities. It provides simple interfaces for sending queries, mutations, and subscribing to real-time data streams. The current version is 0.4.5, with a relatively active release cadence addressing features and maintenance.

pip install python-graphql-client
INSTALL
IMPORT
SIG · PYTHON-GRAPHQL-CLI
P
python-graphql-client
http-networkingpythonv0.4.3
Install
4.7s avg
Import
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.4.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 31.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 4.7s · import 0.000s · 33MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphqlClient
from python_graphql_client import GraphqlClient
from graphql_client import GraphQLClient

This quickstart demonstrates how to initialize a synchronous `GraphQLClient`, define a query with variables, execute it, and process the returned JSON data and potential errors. Remember to replace the `GRAPHQL_ENDPOINT` with your actual GraphQL API endpoint.

import os import json from graphql_client import GraphQLClient # Using a public GraphQL API endpoint for demonstration endpoint = os.environ.get("GRAPHQL_ENDPOINT", "https://countries.trevorblades.com/graphql") # Initialize the synchronous client client = GraphQLClient(endpoint=endpoint) # Define your GraphQL query query = """ query GetCountryDetails($code: ID!) { country(code: $code) { name capital currency } } """ # Define variables for the query variables = {"code": "BR"} # Execute the query response_data = client.execute(query=query, variables=variables) # Print the raw JSON response print("Raw Response:", json.dumps(response_data, indent=2)) # Accessing data and errors if 'data' in response_data and response_data['data']: country = response_data['data']['country'] if country: print(f"\nCountry: {country['name']}") print(f"Capital: {country['capital']}") print(f"Currency: {country['currency']}") else: print("\nError fetching country details.") if 'errors' in response_data: print("GraphQL Errors:", json.dumps(response_data['errors'], indent=2))
Debug
Known issues
gotchaThe library provides distinct client classes for synchronous (`GraphQLClient`), asynchronous (`GraphQLClientAIOHTTP`), and websocket subscriptions (`GraphQLClientWebsockets`). Ensure you use the correct client for your desired operation, as they leverage different underlying HTTP/websocket libraries.
fix
Explicitly import and instantiate `GraphQLClientAIOHTTP` for async operations or `GraphQLClientWebsockets` for subscriptions.
affects: All versions
gotchaGraphQL API errors (i.e., errors returned in the `errors` field of the GraphQL response body) are not automatically raised as Python exceptions by the client. You must manually inspect the `data` and `errors` fields in the returned dictionary to handle successful data or API-level errors.
fix
After calling `client.execute()`, check `response_data.get('errors')` for a list of GraphQL errors and `response_data.get('data')` for successful results.
affects: All versions
gotchaWhen passing custom HTTP headers (e.g., for authentication), ensure they are provided as a dictionary to the `headers` parameter during client initialization or when calling the `execute` method. For subscriptions, initial payload headers can also be set via the `init_payload` parameter in `GraphQLClientWebsockets`.
fix
Pass `headers={'Authorization': 'Bearer YOUR_TOKEN'}` to the `GraphQLClient` constructor or `execute` method. For subscriptions, use `GraphQLClientWebsockets(..., init_payload={'headers': {'Authorization': 'Bearer YOUR_TOKEN'}})`.
affects: All versions
Upgrade
Version history
0.4.3latest on PyPI · released Mar 4, 2021
Audit
Dependencies
requestsrequiredUsed by the synchronous GraphQLClient for HTTP operations.
aiohttprequiredUsed by the asynchronous GraphQLClientAIOHTTP for HTTP operations.
websocketsrequiredUsed by GraphQLClientWebsockets for GraphQL subscriptions.
Agent activity
33 hits · last 30 days
node
30
OpenAI (training)
1
Resources
python-graphql-client — pip install python-graphql-client · libregistry