Skip to main content

Forked and updated version of https://pypi.org/project/gqlclient/

Project description

codecov

Documentation Status

A forked and updated project of gqlclient 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 py_gql_test_client

with asyncio support

pip install py_gql_test_client[async]

Examples

Query

from pydantic.dataclasses import dataclass

from py_gql_test_client 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(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)
print(response)
#{"baseType"{"attr_three":5, "attr_four":"bar"}}

Mutation

from pydantic.dataclasses import dataclass

from py_gql_test_client 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(attr_one: "foo", attr_two: 3){ok, attr_three, attr_four} }', 'operationName': 'baseType'}

response = client.execute_gql_mutation(mutation_base="baseMutation", mutation_response_cls=Response, mutation_parameters=parameters)
print(response)
#{"baseMutation": {"ok": true, "Response": {"attr_three":5, "attr_four":"bar"} }}

Encoders

from dataclasses import dataclass

from py_gql_test_client import GraphQLClient
from py_gql_test_client import GraphQLClient, dataclass_encoder

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

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

response = client.execute_gql_query("workflows", QueryResponse)
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.'), QueryResponse(workflowId=3, workflowName='VISP base calibration', workflowDescription='The base set of calibration tasks for VISP.'), QueryResponse(workflowId=4, workflowName='VTF base calibration', workflowDescription='The base set of calibration tasks for VTF.'), QueryResponse(workflowId=5, workflowName='DLNIRSP base calibration', workflowDescription='The base set of calibration tasks for DLNIRSP.'), QueryResponse(workflowId=6, workflowName='CRYONIRSP base calibration', workflowDescription='The base set of calibration tasks for CRYONIRSP.')]

from py_gql_test_client import json_encoder
# for this call override the default encoder to the json encoder
response = client.execute_gql_query("workflows",QueryResponse, response_encoder=json_encoder)
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."}, {"workflowId": 3, "workflowName": "VISP base calibration", "workflowDescription": "The base set of calibration tasks for VISP."}, {"workflowId": 4, "workflowName": "VTF base calibration", "workflowDescription": "The base set of calibration tasks for VTF."}, {"workflowId": 5, "workflowName": "DLNIRSP base calibration", "workflowDescription": "The base set of calibration tasks for DLNIRSP."}, {"workflowId": 6, "workflowName": "CRYONIRSP base calibration", "workflowDescription": "The base set of calibration tasks for CRYONIRSP."}]}'

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

py_gql_test_client-1.0.1.tar.gz (12.9 kB view details)

Uploaded Source

File details

Details for the file py_gql_test_client-1.0.1.tar.gz.

File metadata

  • Download URL: py_gql_test_client-1.0.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3

File hashes

Hashes for py_gql_test_client-1.0.1.tar.gz
Algorithm Hash digest
SHA256 a1721091ba1215db94d92b2c6b70f1604795a492f71d3114e3722bdae0f0cb1a
MD5 c134026994d68a3388ef2774267b9e13
BLAKE2b-256 eed3de72299dbea4e6b9624bc01a020e2d7097ed4cf286b2642856c641614a97

See more details on using hashes here.

Supported by

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