This folder (src/fhir/generator
) contains the code generator for the FHIR schema. It can be used to generate code.
Currently we generate the following code:
- Services in
src/services
- Javascript classes in
src/fhir/classes
- Javascript file
src/fhir/classes/4_0_0/resources/index.js
that contains a list of resources - GraphQL schemas and resolvers in
src/graphql/v2
- Search parameters in
src/searchParameters
- Resources Non-clinical fields in
src/graphs/patient
- Validation schema in
src/fhir/generator/json
- Resource field types in
src/fhir/generator/json
- Resource DB Schema in
src/fhir/generator/json/fhir-generated.db-schema/
Schema files are downloaded from the FHIR web site.
- Go to https://hl7.org/fhir/R4B/downloads.html (For other versions, change the R4B to the version you want)
- Download from FHIR Definitions->XML (we use XML because the JSON version has bugs)
- The download link should be https://hl7.org/fhir/R4B/definitions.xml.zip for R4B
- Unzip the folder and then unzip the fhir-all-xsd.zip inside it
- Delete all the files in src/fhir/generator/xsd/definitions.xml
- Paste in the files from the unzipped folder
- Go to https://hl7.org/fhir/R4B/downloads.html (For other versions, change the R4B to the version you want)
- Download JSON -> JSON Schema
- Save in
src/fhir/generator/json/fhir.schema.json
The generator creates a list of FhirEntity classes for each:
- Resource
- BackboneElement
- ComplexType
The FhirEntity classes are easier to use in templates since they collect all the information in one class.
Each FhirEntity class contains a field called properties
that is a list of FhirProperty classes.
Then a set of Jinja2 templates are used to generate the four types of code using the FhirEntity classes:
You can run this via make generate
.
This runs src/services/generate_services.py
.
This does not use Jinja2 templates.
You can run this via make classes
.
This runs src/fhir/generator/generate_classes.py
.
This uses the following Jinja2 template template.javascript.class.jinja2
to generate the classes.
This generates Javascript classes for each:
- resource
- backboneElement
- extension
- complex type
- valueset
This is run by the same command as in #2.
This runs src/fhir/generator/generate_classes_index.py
.
This uses the following Jinja2 template template.javascript.index.jinja2
to generate the index.js file.
This is run by the command make graphql
.
This runs src/fhir/generator/generate_graphql_classes.py
.
This uses the following Jinja2 templates:
template.query.jinja2
to generate the query schematemplate.resource.jinja2
to generate the resource schematemplate.resource_queries.jinja2
to generate the resource queriesresolvers/template.resource.jinja2
to generate the resource resolverstemplate.backbone_element.jinja2
to generate the backbone element schemaresolvers/template.backbone_element.jinja2
to generate the backbone element resolverstemplate.complex_type.jinja2
to generate the complex type schemaresolvers/template.complex_type.jinja2
to generate the complex type resolvers
This is run by the command make searchParameters
.
This runs src/searchParameters/generate_search_parameters.py
.
This reads the src/searchParameters/search-parameters.json
file and generates the following files:
src/searchParameters/searchParameters.js
src/fhir/generator/search_parameters.py
This is run by the command make nonClinicalResourceFields
.
This runs src/fhir/generator/generate_non_clinical_fields.py
and makes a list of fields for each resources which contains reference to non-clinical resources, which is used by everything operation for finding linked non-clinical resources.
This reads the src/graphs/patient/everything.json
file and generates the following file:
src/graphs/patient/generated.non_clinical_resources_fields.json
This is run by the command make schema
.
This runs src/fhir/generator/generate_schema.py
.
It generates src/fhir/generator/json/fhir-generated.schema.json
file used for resources validation.
This is run by the command make resourceFieldTypes
.
This runs src/fhir/generator/generate_resource_fields_type.py
.
It reads src/fhir/generator/xsd/definitions.xml/profiles-resources.xml
file and generates src/fhir/generator/json/fhir-generated.field-types.json
file used for getting field types while making queries.
This is run by the command make dbSchema
.
This runs src/fhir/generator/generate_db_schema.py
.
It reads generates src/fhir/generator/json/fhir-generated.db-schema/*.json
containing the DB schema for all fields of all resources.
The generator uses the following files:
src/fhir/generator/xsd/definitions.xml/fhir-all-xsd/fhir-all.xsd
contains the list of resources and what .xsd file contains the schema for each resource.src/fhir/generator/xsd/definitions.xml/fhir-all-xsd/fhir-base.xsd
contains the base types for FHIR.- From
fhir-all.xsd
we can get the list of the schema files for each resource insrc/fhir/generator/xsd/definitions.xml/*.xsd
. src/fhir/generator/xsd/definitions.xml/dataelements.xml
contains the types for the properties of each resource, the types for the references and the types for the CodeableConcepts.
The Fhir Schema Parser code is in src/fhir/generator/fhir_xml_schema_parser.py
. This class reads the FHIR schema xml
files and generates a list of FhirEntity classes.
You can debug the fhir schema parser by running src/fhir/generator/test_generator.py
and putting breakpoints in the
fhir schema parser code.