How to upsert using GraphQL?

Sure @amaster507. Your post there outlines the same question I’ve raised. Indeed it gets increasingly complex when you have multiple updates that can either be an Add or Update operation. What I want here is to be able to perform these mutations in one request. If this works, there are great benefits when combined with external IDs (or when UIDs are known beforehand). It means I can perform deeply nested mutations in one request and not worry about the existence of leaf nodes.

For example:

type Person {
    personId: String! @id
    name: String!
}

type Vehicle{
    vehicleId: String! @id
    model: String!
    owner: Person
}

mutation {
    updateVehicle(input: {
        filter: {vehicleId: {eq: "thiscar"}},
        set: {
            model: "foo",
            owner: {
                personId: "thatperson",
                name: "bar"
            }
        }
    }
}

With an upsert operation, I’ll expect this query to succeed even if thiscar or thatperson does not exist and the same applies to the add operation. Currently, I’ll need to make at least 4 requests on the first run to achieve the same result. I can imagine situations where an upsert is not desired, but in theory, there can be a way to flag this behaviour.

1 Like