GraphQL Query for ImagingStudy and Related Data
# support
b
Hi all. We're working on developing "worklists" for launching images linked to ImagingStudy resources. Would like to create a single GraphQL query for retrieving the DiagnosticReport and Patient related to the ImagingStudy. In reviewing the GraphQL docs, those resources are linked under ServiceRequest. I've attempted to use the "...List" tables for child resources, but can't seem to get the syntax correct. Is anyone willing to share a GraphQL query that links ServiceRequest, DiagnosticReport, ImagingStudy & Patient?
r
Hi @bnapora , do you mind sharing the query that you've tried so far? Might be easier to tweak what you already have
b
@rahul1 Was attempting to do something similar to the example here: https://github.com/medplum/medplum/blob/main/examples/medplum-chart-demo/src/components/chart/PatientChart.tsx Using ServiceRequest -> DiagnosticReport -> ImagingStudy. Don't have much of a sample yet, because still figuring how to link "indexes".
r
@bnapora , I'm happy to help! If it's ok, I'd like to explain a bit about what's going on in that query, as well as offer an example query you might be able to work with.
b
Great!
r
So that query you linked makes heavy use of "reverse references" (https://www.medplum.com/docs/graphql/basic-queries#searching-reverse-references-using-the-_reference-keyword)
ConditionList(_reference: patient, _count: 100, _sort: "-_lastUpdated")
^ this can be read as "Find all the conditions who are linked to the current
Patient
via the
Condition.patient
reference parameter"
So we start with a Patient, then find all the resources of each type that point to it
In your case, i think your root resources would eb a serviceRequest
Have you used https://graphiql.medplum.com help design / debug your query?
When you see the
resource
field inside the query, this is a forward reference search (https://www.medplum.com/docs/graphql/basic-queries#resolving-nested-resources-with-the-resource-element)
Putting those concepts together, this might be the query you're looking for:
Copy code
query{
  ServiceRequest(id: "2987ea86-a59b-42c4-8660-331562d817aa") {
    id
    subject {
      resource {
        ...on Patient {
          id
          name {given, family}
        }
      }
    }
    
    DiagnosticReportList(_reference:based_on) {
      id, 
      imagingStudy {
        resource {
         ...on ImagingStudy {
          id
        } 
        }
      }
    }
  }
}
b
Thanks for direction. When running the query you supplied (with a actaul ServiceRequest ID in our system) I get:
Copy code
message": "invalid input syntax for type uuid: \"1EKG4-TEST-RG56\"",
      "path": [
        "ServiceRequest",
        "subject",
        "resource"
      ],
I'll dig around and see if its some silly syntax issue.
@rahul1 Was able to get the approach to work. I will add my completed query to issue when I get it complete, if it may be helpful to others.
r
Thanks @bnapora , that would be very helpful
Looking at the errror message you posted, it seems like you are not using an UUID for the ServiceRequest iD?
By any chance, did you create the ServiceRequest in the "Super Admin" project? As noted here, this by passes a lot of data validation, including enforcing that
ids
are UUIDs. https://www.medplum.com/docs/access/projects#superadmin
This would generally be considered a broken state
b
The error message was the result of a broken link to the Patient record. I'm not sure what happened to the patient, but when I used a different ServiceRequest with different patient, it worked.
If anyone is interested below is a query for returning ServiceRequests, PatientDemo, DiagnosticReport and ImagingStudy. Still need to add in Tasks. Thanks for your help @rahul1
Copy code
{
  # ServiceRequest(id: "7ccf0ac5-f5c6-4828-9c1e-dcffcff7e45c") {
  ServiceRequestList {
    resourceType
    id
    status
    intent
    code {
      coding {
        id
        display
        code
      }   
    }
    patient: subject {
      resource {
        ...on Patient {
          id
          name {given, family}
        }
      }
    }
    DiagnosticReportList(_reference:based_on) {
      resourceType
      id, 
      status,
      code {
        coding {
          id
          display
          code
        }
      }
      imagingStudy {
        resource {
         ...on ImagingStudy {
          resourceType
          id
          identifier {
            system
            value
          }
          procedureCode {
            coding {
              display
              code
            }
          }
          numberOfSeries
          numberOfInstances
          series {
            started
            modality {
              code
              display
            }
            bodySite {
              code
              display
            }
          }
        }
        }
      }
    }
  }
}
r
Oh I see. Well thanks for following up and sharing with the community 🙂
157 Views