Package: py-gql-test-client
A forked and updated project ofgqlclientfor making requests to a GraphQL server using
standard library or pydantic dataclasses to spare you from string manipulation.FeaturesUse standard library dataclasses to specify graphql parameters and responsesUsepydanticdataclasses to
specify graphql parameters and responses that have type validationCreate and execute GraphQL Queries based upon typed modelsCreate and execute GraphQL Mutations based upon typed modelsAsync supportInstallationpipinstallpy_gql_test_clientwithasynciosupportpipinstallpy_gql_test_client[async]ExamplesQueryfrompydantic.dataclassesimportdataclassfrompy_gql_test_clientimportGraphQLClient@dataclassclassParameters:attr_one:strattr_two:int@dataclassclassResponse:attr_three:intattr_four:strclient=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"}}Mutationfrompydantic.dataclassesimportdataclassfrompy_gql_test_clientimportGraphQLClient@dataclassclassParameters:attr_one:strattr_two:int@dataclassclassResponse:attr_three:intattr_four:strclient=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"} }}Encodersfromdataclassesimportdataclassfrompy_gql_test_clientimportGraphQLClientfrompy_gql_test_clientimportGraphQLClient,dataclass_encoder# set the default encoder to dataclass_encoderclient=GraphQLClient(gql_uri="http://127.0.0.1:30003/graphql",default_response_encoder=dataclass_encoder)@dataclassclassQueryResponse:workflowId:intworkflowName:strworkflowDescription:strresponse=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.')]frompy_gql_test_clientimportjson_encoder# for this call override the default encoder to the json encoderresponse=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."}]}'