Mutations
GraphQL mutations are operations that allow the client to create, update, or delete data on the server. Unlike queries, which are read-only operations and can be executed in parallel, mutations are write operations. For more information about GraphQL mutations, refer to the GraphQL documentation.
Medplum implements the draft FHIR GraphQL Mutation spec. For the inputs, you would append the action (Create, Update, or Delete) to the resource type.
Here are examples of mutations for the Patient
resource. You can test these mutations at graphiql.medplum.com
Create Mutation
- GraphQL
- Typescript
mutation {
# Define the fields for the resource being created
PatientCreate(
res: {
resourceType: "Patient"
gender: "male"
name: [
{
given: "Homer"
}
]
}
)
# Specify which of the newly created fields to return in the response
{
id
gender
name {
given
}
}
}
const patient = await medplum.graphql(`
mutation {
# Define the fields for the resource being created
PatientCreate(
res: {
resourceType: "Patient"
gender: "male"
name: [
{
given: "Homer"
}
]
}
)
# Specify which of the newly created fields to return in the response
{
id
gender
name {
given
}
}
}`);
Example Response
{
data: {
PatientCreate: {
id: 'example-id',
name: {
given: 'Homer',
},
gender: 'male',
},
},
};
Just as with GraphQL queries, you can alias the newly created resource.
- GraphQL
- Typescript
mutation {
# Define the fields for the resource being created, and alias as "newPatient"
newPatient: PatientCreate(
res: {
resourceType: "Patient"
gender: "male"
name: [
{
given: "Homer"
}
]
}
)
# Specify which of the newly created fields to return in the response
{
id
gender
name {
given
}
}
}
const patientAlias = await medplum.graphql(`
mutation {
# Define the fields for the resource being created, and alias as "newPatient"
newPatient: PatientCreate(
res: {
resourceType: "Patient"
gender: "male"
name: [
{
given: "Homer"
}
]
}
)
# Specify which of the newly created fields to return in the response
{
id
gender
name {
given
}
}
}`);
Example Response
{
data: {
newPatient: {
id: 'example-id',
name: {
given: 'Homer',
},
gender: 'male',
},
},
};
Update Mutation
- GraphQL
- Typescript
mutation {
# Define the elements for the updated resources. Note that this will *overwrite* the entire resource.
PatientUpdate(
id: "example-id"
res: {
id: "example-id"
resourceType: "Patient"
gender: "male"
name: [
{
given: "Bob"
},
{
family: "Smith"
}
]
}
)
# Specify which fields to return from the updated resource
{
id
gender
name {
given
}
}
}
const update = await medplum.graphql(`
mutation {
# Define the elements for the updated resources. Note that this will *overwrite* the entire resource.
PatientUpdate(
id: "example-id"
res: {
id: "example-id"
resourceType: "Patient"
gender: "male"
name: [
{
given: "Bob"
},
{
family: "Smith"
}
]
}
)
# Specify which fields to return from the updated resource
{
id
gender
name {
given
}
}
}
`);
Example Response
{
data: {
PatientUpdate: {
id: 'example-id',
name: {
given: 'Homer',
},
gender: 'male',
},
},
};
Delete Mutation
- GraphQL
- Typescript
mutation {
PatientDelete(
id: "example-id"
) {
id
}
}
const deleteObject = await medplum.graphql(`
mutation {
PatientDelete(
id: "example-id"
) {
id
}
}
`);