medplum GraphQL mapping resources
# support
b
Hey there, I’m currently using medplum’s GraphQL API and I’ve noticed that a lot of data we would like to get in a single query belong to different resources. Say for example we need to get care teams related to each patient in a list of patients, what would be the ideal approach for getting this data together? Right now I have a GraphQL call to PatientList and subsequent GraphQL calls to CareTeamList using each patient id I got back from the PatientList query. Is there a better way we can use medplum’s GQL api to avoid making so many GraphQL calls?
c
Hi @boogiecoco. - the FHIR GraphQL actually has exactly what you want. You can embed the
CareTeamList
query in the
PatientList
query, and it will automatically be added:
Copy code
graphql
{
  PatientList(name:"Homer") {
    resourceType
    id
    name {
      given
      family
    }
    CareTeamList(_reference: subject) {
      resourceType
      id
    }
  }
}
Sample respons:
Copy code
json
{
  "data": {
    "PatientList": [
      {
        "resourceType": "Patient",
        "id": "e92fa10f-7d67-45e8-9667-896d94df2ce2",
        "name": [
          {
            "given": [
              "Homer",
              "J"
            ],
            "family": "Simpson"
          }
        ],
        "CareTeamList": [
          {
            "resourceType": "CareTeam",
            "id": "77749218-466b-4a26-9572-4d647049783a"
          }
        ]
      }
    ]
  }
}
The trick is to use the
_reference
syntax
See the spec on "Reverse References": https://hl7.org/fhir/r4/graphql.html#reverse
b
Amazing ! Thanks for sharing, I didn’t realize we could use keywords in reference
r
You can use the _count and _offset params detailed in our "Paginated Search Guide" https://www.medplum.com/docs/search/paginated-search
You can pass in multiple search parameters into the
PatientList(...)
parens
b
Awesome thank you for pointing that out
@rahul1 do you have an example of GraphQL mutations with the medplum API? Particularly creating a new organization or physician
@cody
r
Hi @boogiecoco. , Unfortunately, at present the Medplum GraphQL does not support mutations. They are technically an optional feature of the FHIR GraphQL spec: https://hl7.org/fhir/R4/graphql.html#mutations We've created a tracking ticket here: https://github.com/medplum/medplum/issues/2048 For now, we recommend using the normal REST endpoints, or the "batch" API to perform updates.
b
Another question: Is there support for graphql schemas and types ? Would like to use something like this for our queries in order to have better data validation on our client side. https://graphql.org/learn/schema/
If we can’t use schemas, do you have any suggestions for validating/typing fields we pass into our GraphQL queries?
c
Hi @boogiecoco. - The Medplum production API server disables introspection queries due to DoS concerns. The schema is available as a static file here: https://graphiql.medplum.com/schema/schema-v4.json (warning 48mb json file)
b
@cody any alternative options here to get support for typing/schema validations?
c
What tool are you using to perform the validation?
b
Right now we are only using React and Typescript. We were hoping to use GraphQL Schema, but since that’s not supported with medplum’s GraphQL API we were hoping for an alternative solution.
c
Hm, do you need the schema in SDL format, like this? https://graphiql.medplum.com/schema/schema-v4.graphql
(warning, 28 MB)
b
The JSON schema works, but two issues 1. The medplum.GraphQL API does not support type arguments that we want to use in order to get compile time type safety with queries 2. If there are any major schemes changes or versioning changes with medplum, will we have to get a new schema from you guys every time we want to stay up to date?
3. I also noticed medplum.GraphQL doesn’t have support for ‘GQL-tag’ which is required for code-gen to generate types. Is there any chance support will be added?
@rahul1 @cody any chance you guys know if using GraphQL code gen with the static schema would be sustainable ?
c
Hey @boogiecoco. - A few thoughts; 1. The GraphQL spec is generally pretty stable. It is currently on the 4th version we've ever published. Each version was due to Medplum adding new capabilities (v1 was the original, v2 added extension list args, v3 fixed a small bug in v2, v4 added the "Connection" API). It's all just an implementation of the FHIR GraphQL spec, which for FHIR R4 is very stable . 2. With that said, there is work coming soon that will add Mutations support (Create, Update, Patch, Delete). So, yes, there are changes coming. 3. If you are concerned about ongoing maintenance, we would be happy to collaborate and integrate your codegen into our build process, so that it would be run automatically, and even published with our release schedule if you are open to it.
When you say "GQL-tag", do you mean
graphql-tag
(https://www.npmjs.com/package/graphql-tag )? Or something else?
b
I noticed mutation support was added a few days ago. If we’re updating multiple resources in a single batch is it recommended to still use the medplum native post/put/delete methods or can this now be done with the new mutation API?
c
GraphQL mutations and Batch updates are largely equivalent. They should be almost identical from a performance perspective. There are a few subtle differences. For example: 1. With Batch, you can use
"type": "transaction"
and then create operations can refer to other create operations using the
urn:uuid:
syntax. This is useful when you want to create resources that refer to each other. 2. With Batch, you can use
"ifNoneExist"
for conditional creates / upserts 3. With Batch, you can mix and match "update" and "patch" (there is no "patch" defined for GraphQL) 4. With GraphQL, you can selectively return only the fields that you care about Those are just a few off the top of my head. There are probably many more. So... I suppose it depends on the specific use case. If you're comfortable and happy with the GraphQL API, then go for it 👍
b
Awesome thank you !
Resurfacing this as it sounds like introspection queries are available on a project level. Would this allow us to have greater flexibility in typing ? (setting non-nullable fields) rather than default from the schema which defines every field as nullable @rahul1 @cody
r
The intropsection queries allow you to ask questions about the types in the GraphQL schema we host. However, it still won't allow you to set your own schema on our server
b
Got it, so it sounds like we would need a transformation layer to handle our app's specific data requirements.
r
yeah, I think that would be the way to go
167 Views