-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Util suggestion: a way to parse bundles with multiple related resources #4399
Comments
Hi @Swizec - thanks for submitting this. You're right, that FHIR Get resources by typeA simple Get related resourcesA Or, alternatively, it could "move" the related resources into To be clear, it is not part of the official FHIR But we added it because that is the shape of the FHIR GraphQL response. We could use that same property in a utility to "get related resources". For example, for this input bundle: {
"resourceType": "Bundle",
"type": "searchset",
"entry": [
{
"resource": {
"resourceType": "DiagnosticReport",
"id": "123",
"result": [{ "reference": "Observation/456" }]
}
},
{
"resource": {
"resourceType": "Observation",
"id": "456",
"valueQuantity": { "value": 123, "unit": "mg" }
}
},
]
} The utility could "move" the resource into {
"resourceType": "DiagnosticReport",
"id": "123",
"result": [
{
"resource": {
"resourceType": "Observation",
"id": "456",
"valueQuantity": { "value": 123, "unit": "mg" }
}
}
]
} GraphQL typesYou're also right that the FHIR GraphQL API creatively abuses the type schema. We have noticed some interesting patterns. Consider this example from the Medplum GraphQL docs: {
DiagnosticReport(id: "example-id-1") {
resourceType
id
result {
resource {
... on Observation {
resourceType
id
valueQuantity {
value
unit
}
}
}
}
}
} With this query, the referenced data comes right inline, because the FHIR data: {
DiagnosticReport: {
resourceType: 'DiagnosticReport',
id: 'example-id-1',
result: [
{
resource: {
resourceType: 'Observation',
id: 'observation-id-1',
valueQuantity: {
value: 5.5,
unit: 'mg/dL',
},
},
},
{
resource: {
resourceType: 'Observation',
id: 'observation-id-2',
valueQuantity: {
value: 3.2,
unit: 'mg/dL',
},
},
},
],
},
}, Here is another more complicated example, with embedded per-resource searches: {
Patient(id: "example-patient-id") {
resourceType
id
encounters: EncounterList(_reference: patient) {
resourceType
id
}
}
} Here, we're including the search results inline: data: {
Patient: {
resourceType: 'Patient',
id: 'example-patient-id',
encounters: [
{
resourceType: 'Encounter',
id: 'encounter-id-1',
},
{
resourceType: 'Encounter',
id: 'encounter-id-2',
},
],
},
}, You can use a TypeScript union to represent this: import { Encounter, Patient } from '@medplum/fhirtypes';
type MyPatient = Patient & { encounters: Encounter[] };
type QueryResult = {
data: {
Patient: MyPatient[];
};
};
for (const myPatient of result.data.Patient) {
const { encounters, ...patient } = myPatient;
console.log('patient', patient);
console.log('encounters', encounters);
} |
Hi,
while building with the Medplum SDK we've developed a few helper methods to help us work with the API. It would be fantastic if some version of these could be added to the official SDK. Making separate issues for more focused discussion :)
We often find ourselves building a nested or merged data structure out of bundles with multiple resources. For example when fetching observations with related practitioners and service requests.
There are possibly two utils hiding in here:
We've thought about doing this with the GraphQL API instead but that doesn't play as nicely with TypeScript.
The text was updated successfully, but these errors were encountered: