{"id":6206,"library":"python-graphql-client","title":"Python GraphQL Client","description":"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.","status":"active","version":"0.4.5","language":"en","source_language":"en","source_url":"https://github.com/prodigyeducation/python-graphql-client","tags":["graphql","client","api","async","websockets","aiohttp","requests"],"install":[{"cmd":"pip install python-graphql-client","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used by the synchronous GraphQLClient for HTTP operations.","package":"requests"},{"reason":"Used by the asynchronous GraphQLClientAIOHTTP for HTTP operations.","package":"aiohttp"},{"reason":"Used by GraphQLClientWebsockets for GraphQL subscriptions.","package":"websockets"}],"imports":[{"symbol":"GraphQLClient","correct":"from graphql_client import GraphQLClient"},{"symbol":"GraphQLClientAIOHTTP","correct":"from graphql_client import GraphQLClientAIOHTTP"},{"symbol":"GraphQLClientWebsockets","correct":"from graphql_client import GraphQLClientWebsockets"}],"quickstart":{"code":"import os\nimport json\nfrom graphql_client import GraphQLClient\n\n# Using a public GraphQL API endpoint for demonstration\nendpoint = os.environ.get(\"GRAPHQL_ENDPOINT\", \"https://countries.trevorblades.com/graphql\")\n\n# Initialize the synchronous client\nclient = GraphQLClient(endpoint=endpoint)\n\n# Define your GraphQL query\nquery = \"\"\"\n    query GetCountryDetails($code: ID!) {\n        country(code: $code) {\n            name\n            capital\n            currency\n        }\n    }\n\"\"\"\n\n# Define variables for the query\nvariables = {\"code\": \"BR\"}\n\n# Execute the query\nresponse_data = client.execute(query=query, variables=variables)\n\n# Print the raw JSON response\nprint(\"Raw Response:\", json.dumps(response_data, indent=2))\n\n# Accessing data and errors\nif 'data' in response_data and response_data['data']:\n    country = response_data['data']['country']\n    if country:\n        print(f\"\\nCountry: {country['name']}\")\n        print(f\"Capital: {country['capital']}\")\n        print(f\"Currency: {country['currency']}\")\nelse:\n    print(\"\\nError fetching country details.\")\n    if 'errors' in response_data:\n        print(\"GraphQL Errors:\", json.dumps(response_data['errors'], indent=2))","lang":"python","description":"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."},"warnings":[{"fix":"Explicitly import and instantiate `GraphQLClientAIOHTTP` for async operations or `GraphQLClientWebsockets` for subscriptions.","message":"The 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"After calling `client.execute()`, check `response_data.get('errors')` for a list of GraphQL errors and `response_data.get('data')` for successful results.","message":"GraphQL 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.","severity":"gotcha","affected_versions":"All versions"},{"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'}})`.","message":"When 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`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}