Skip to main content

Client library for making graphql calls.

Project description

codecov

Documentation Status

A pythonic interface for making requests to a GraphQL server using standard library or pydantic dataclasses to spare you from string manipulation.

Features

  • Use standard library dataclasses to specify graphql parameters and responses

  • Use pydantic dataclasses to specify graphql parameters and responses that have type validation

  • Create and execute GraphQL Queries based upon typed models

  • Create and execute GraphQL Mutations based upon typed models

  • Async support

Installation

pip install gqlclient

with asyncio support

pip install gqlclient[async]

with pydantic support

pip install gqlclient[pydantic]

for developers

pip install gqlclient[test]
pip install pre-commit
pre-commit install

Examples

Query

from pydantic.dataclasses import dataclass

from gqlclient import GraphQLClient

@dataclass
class Parameters:
    attr_one: str
    attr_two: int

@dataclass
class Response:
    attr_three: int
    attr_four: str

client = GraphQLClient(gql_uri="http://localhost:5000/graphql")
parameters = Parameters(attr_one="foo", attr_two=3)
query = client.get_query(query_base="baseType", query_response_cls=Response, query_parameters=parameters)
print(query)
# {'query': '{baseType(filterParams: {attr_one: "foo", attr_two: 3}){attr_three, attr_four} }'}
response = client.execute_gql_query(query_base="baseType", query_response_cls=Response, query_parameters=parameters, response_encoder=json_encoder)
print(response)
# with the default dataclass_encoder
# [Response(attr_three=5, attr_four="bar")]

Mutation

from pydantic.dataclasses import dataclass

from gqlclient import GraphQLClient


@dataclass
class Parameters:
    attr_one: str
    attr_two: int


@dataclass
class Response:
    attr_three: int
    attr_four: str

client = GraphQLClient(gql_uri="http://localhost:5000/graphql")
parameters = Parameters(attr_one="foo", attr_two=3)
query = client.get_mutation(mutation_base="baseMutation", mutation_response_cls=Response, mutation_parameters=parameters)
print(query)
# {'query': 'mutation baseType {baseType(mutateParams: {attr_one: "foo", attr_two: 3}){attr_three, attr_four} }', 'operationName': 'baseType'}

response = client.execute_gql_mutation(mutation_base="baseMutation", mutation_response_cls=Response, mutation_parameters=parameters)
print(response)
# with the default dataclass_encoder
# [Response(attr_three=5, attr_four="bar")]

Encoders

from dataclasses import dataclass

from gqlclient import GraphQLClient
from gqlclient import json_encoder

# set the default encoder to the json_encoder
client = GraphQLClient(gql_uri="http://127.0.0.1:30003/graphql", response_encoder=json_encoder)

@dataclass
class QueryResponse:
    workflowId: int
    workflowName: str
    workflowDescription: str | None

response = client.execute_gql_query("workflows",QueryResponse)
print(response)
# Response is a json formatted string
# {"workflows": [{"workflowId": 1, "workflowName": "gql3_full - workflow_name", "workflowDescription": "gql3_full - workflow_description"}, {"workflowId": 2, "workflowName": "VBI base calibration", "workflowDescription": "The base set of calibration tasks for VBI."}]}

from gqlclient import dataclass_encoder
# for this call override the default encoder
response = client.execute_gql_query("workflows", QueryResponse, response_encoder=dataclass_encoder)
print(response)
# Response type is a list of dataclasses
# [QueryResponse(workflowId=1, workflowName='gql3_full - workflow_name', workflowDescription='gql3_full - workflow_description'), QueryResponse(workflowId=2, workflowName='VBI base calibration', workflowDescription='The base set of calibration tasks for VBI.')]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gqlclient-1.1.1.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

gqlclient-1.1.1-py3-none-any.whl (26.3 kB view details)

Uploaded Python 3

File details

Details for the file gqlclient-1.1.1.tar.gz.

File metadata

  • Download URL: gqlclient-1.1.1.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for gqlclient-1.1.1.tar.gz
Algorithm Hash digest
SHA256 de5ed9ed0126d73ac47660cb390e414ba0b7f4f346f65309b299b54a3e4c381b
MD5 e06c9a9e9c4bc1c40f01c94bb37bbc6f
BLAKE2b-256 e9a8fdec6a183e30eae96cd747cb46806769def9d2aed6eb25eeca8a5aa18f8e

See more details on using hashes here.

File details

Details for the file gqlclient-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: gqlclient-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 26.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.15

File hashes

Hashes for gqlclient-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f4b5a2169246b648096f15bdde2b101217f22d2bb98e678a440b0754180ecc5
MD5 2b08c54cb5cbc5939672e386f0bbd84c
BLAKE2b-256 50b422ce31c11217444469aec14a9f408902f4a63884bf3c7747e7d811b55e87

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page