Package: graphql-pydantic-converter
GraphQL to Pydantic Converter & Pydantic to Query BuilderOverviewTheGraphQL to Pydantic Converteris a Python package designed to simplify the process of transforming GraphQL
schemas in JSON format into Pydantic models. This tool is particularly useful for developers working with GraphQL
APIs who want to generate Pydantic models from GraphQL types for efficient data validation
and serialization/deserialization.FeaturesConverts GraphQL schemas in JSON format into Pydantic models.Build query or mutation from pydantic dataclassInstallationYou can install theGraphQL to Pydantic Transformerpackage via pip:pipinstallgraphql-pydantic-converter# orpoetryaddgit+https://github.com/FRINXio/frinx-services-python-api.git@main#subdirectory=utils/graphql-pydantic-converterUsageCli tool to transform GraphQL JSON to Pydanticgraphql-pydantic-converter[-h][-iINPUT_FILE][-oOUTPUT_FILE][--urlURL][--headersHEADERS[HEADERS...]]options:-h,--helpshowthishelpmessageandexit-iINPUT_FILE,--input-fileINPUT_FILE-oOUTPUT_FILE,--output-fileOUTPUT_FILE--urlURL--headersHEADERS[HEADERS...]# --headers "HeaderName: HeaderValue" "HeaderName: HeaderValue"Output from cli toolimporttypingfrompydanticimportFieldfrompydanticimportPrivateAttrfromgraphql_pydantic_converter.graphql_typesimportInputfromgraphql_pydantic_converter.graphql_typesimportMutationfromgraphql_pydantic_converter.graphql_typesimportPayloadBoolean:typing.TypeAlias=boolDateTime:typing.TypeAlias=typing.AnyFloat:typing.TypeAlias=floatID:typing.TypeAlias=strInt:typing.TypeAlias=intJSON:typing.TypeAlias=typing.AnyString:typing.TypeAlias=strclassCreateScheduleInput(Input):name:Stringworkflow_name:String=Field(default=None,alias='workflowName')workflow_version:String=Field(default=None,alias='workflowVersion')cron_string:String=Field(default=None,alias='cronString')enabled:typing.Optional[Boolean]=Field(default=None)parallel_runs:typing.Optional[Boolean]=Field(default=None,alias='parallelRuns')workflow_context:typing.Optional[String]=Field(default=None,alias='workflowContext')from_date:typing.Optional[DateTime]=Field(default=None,alias='fromDate')to_date:typing.Optional[DateTime]=Field(default=None,alias='toDate')classSchedule(Payload):name:typing.Optional[bool]=Field(default=False)enabled:typing.Optional[bool]=Field(default=False)parallel_runs:typing.Optional[bool]=Field(alias='parallelRuns',default=False)workflow_name:typing.Optional[bool]=Field(alias='workflowName',default=False)workflow_version:typing.Optional[bool]=Field(alias='workflowVersion',default=False)cron_string:typing.Optional[bool]=Field(alias='cronString',default=False)workflow_context:typing.Optional[bool]=Field(alias='workflowContext',default=False)from_date:typing.Optional[bool]=Field(alias='fromDate',default=False)to_date:typing.Optional[bool]=Field(alias='toDate',default=False)status:typing.Optional[bool]=Field(default=False)classCreateScheduleMutation(Mutation):_name:str=PrivateAttr('createSchedule')input:CreateScheduleInputpayload:ScheduleCreateScheduleInput.model_rebuild()CreateScheduleMutation.model_rebuild()Schedule.model_rebuild()Query & Mutation builderfromschedule_apiimportSchedule,CreateScheduleMutation,CreateScheduleInputSCHEDULE:Schedule=Schedule(name=True,enabled=True,workflow_name=True,workflow_version=True,cron_string=True)mutation=CreateScheduleMutation(payload=SCHEDULE,input=CreateScheduleInput(name='name',workflow_name='workflowName',workflow_version='workflowVersion',cron_string='* * * * *',enabled=True,parallel_runs=False,))Created query with inlined variables as stringmutation.render(form='inline')

mutation {
  createSchedule(
    input: {
      name: "name"
      workflowName: "workflowName"
      workflowVersion: "workflowVersion"
      cronString: "* * * * *"
      enabled: true
      parallelRuns: false
    }
  ) {
    name
    enabled
    workflowName
    workflowVersion
    cronString
  }
}Created query with extracted variablesmutation, variables = mutation.render(form='extracted)

# mutation as a string
mutation ($input: CreateScheduleInput!) { 
  createSchedule(input: $input) { 
    name 
    enabled 
    workflowName
    workflowVersion 
    cronString 
  } 
}

# variables as a dict[str, Any]
{
  "input": {
    "name": "name",
    "workflowName": "workflowName",
    "workflowVersion": "workflowVersion",
    "cronString": "* * * * *",
    "enabled": true,
    "parallelRuns": false
  }
}Response parserExample of generated model.pyimporttypingfrompydanticimportBaseModel,Fieldfromgraphql_pydantic_converter.graphql_typesimportENUMBoolean:typing.TypeAlias=boolDateTime:typing.TypeAlias=typing.AnyFloat:typing.TypeAlias=floatID:typing.TypeAlias=strInt:typing.TypeAlias=intJSON:typing.TypeAlias=typing.AnyString:typing.TypeAlias=strclassStatus(ENUM):UNKNOWN='UNKNOWN'COMPLETED='COMPLETED'FAILED='FAILED'PAUSED='PAUSED'RUNNING='RUNNING'TERMINATED='TERMINATED'TIMED_OUT='TIMED_OUT'classSchedulePayload(BaseModel):name:typing.Optional[typing.Optional[String]]=Field(default=None)enabled:typing.Optional[typing.Optional[Boolean]]=Field(default=None)parallel_runs:typing.Optional[typing.Optional[Boolean]]=Field(default=None,alias='parallelRuns')workflow_name:typing.Optional[typing.Optional[String]]=Field(default=None,alias='workflowName')workflow_version:typing.Optional[typing.Optional[String]]=Field(default=None,alias='workflowVersion')cron_string:typing.Optional[typing.Optional[String]]=Field(default=None,alias='cronString')workflow_context:typing.Optional[typing.Optional[String]]=Field(default=None,alias='workflowContext')from_date:typing.Optional[typing.Optional[DateTime]]=Field(default=None,alias='fromDate')to_date:typing.Optional[typing.Optional[DateTime]]=Field(default=None,alias='toDate')status:typing.Optional[typing.Optional[Status]]=Field(default=None)classCreateScheduleData(BaseModel):create_schedule:SchedulePayload=Field(default=None,alias='createSchedule')classCreateScheduleResponse(BaseModel):data:typing.Optional[CreateScheduleData]=Field(default=None)errors:typing.Optional[typing.Any]=Field(default=None)Example of responsefrommodelimportCreateScheduleResponse# send previously created request to backend servicepayload={'query':mutation.render()}resp=requests.post(SCHELLAR_URL,json=payload)response=resp.json()# Example of response# {#    'data': {#         'createSchedule': {#              'name': 'name',#              'enabled': True,#              'workflowName': 'workflowName',#              'workflowVersion': 'workflowVersion',#              'cronString': '* * * * *'#          }#     }# }schedule=CreateScheduleResponse(**response)ifschedule.errorsisNone:print(schedule.data.create_schedule.workflow_name)else:print(schedule.errors)1.0.0Migration to pydantic v21.0.1Change Map type to dict (key, value)1.0.2Stringify mutation input stringsAdd __typename to payloadcreate test folder as a module1.1.0Support inline and extracted variables for mutation and queryStringify fix1.1.1Fix missing inputs for mutation and query render in extracted formatENUM keys always uppercase1.2.0Allow to populate by name