用于进行GraphQL调用的客户端库。
项目描述
使用标准库数据类或pydantic数据类来发送请求到GraphQL服务器,从而让您免于字符串操作。
功能
使用标准库数据类指定graphql参数和响应
使用pydantic数据类指定具有类型验证的graphql参数和响应
基于类型模型创建和执行GraphQL查询
基于类型模型创建和执行GraphQL突变
异步支持
安装
pip install gqlclient
支持 asyncio
pip install gqlclient[async]
支持 pydantic
pip install gqlclient[pydantic]
开发者版
pip install gqlclient[test]
pip install pre-commit
pre-commit install
示例
查询
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")]
突变
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")]
编码器
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.')]
项目详情
下载文件
下载您平台对应的文件。如果您不确定该选择哪一个,请了解有关安装包的更多信息。
源分布
gqlclient-1.1.1.tar.gz (23.8 kB 查看哈希值)
构建分布
gqlclient-1.1.1-py3-none-any.whl (26.3 kB 查看哈希值)