How to update a query in URQL's Graphcache updater if you don't have all the args for the updateQuery function?

29 Views Asked by At

Leys say I have the following graphql schema:

type Query {
    users: [User!]
    user(id: ID!): User!
}
type User {
    id: ID!
    name: String
    devices(types: [DeviceType!]): [Device]
    device(id: ID!): Device
}
type Device {
    id: String!
    name: String
    type: DeviceType
    status: DeviceStatus
    location: DeviceLocation
}
type DeviceLocation {
    lat: Float!
    lng: Float!
}
enum DeviceType {
    PHONE
    SENSOR
}
enum DeviceStatus {
    ONLINE,
    OFFLINE,
    OUT_OF_RANGE
}
type Mutation {
    editDevice(userId: ID!, deviceId: ID!, update: DeviceUpdate!): Device!
}
input DeviceUpdate {
    name: String!
}

and the following query:

query getDevices($types: [DeviceType!], $id: ID!) {
  user(id: $id) {
    id
    devices(types: $types) {
      id
      name
      type
      status
      location {
        lat
        lng
      }
    }
  }
}

and the following mutation:

mutation editDeviceName($userId: ID!, $deviceId: ID!, $name: String!) {
  editDevice(userId: $userId, deviceId: $deviceId, update: { name: $name }) {
    id
    name
    type
    status
    location {
      lat
      lng
    }
}

How would I update that device in the getDevices query in a Graphcache cache update function?

cacheExchange({
    keys: {
        DeviceLocation: () => null,
    },
    resolvers: {},
    updates: {
        Mutation: {
            editDevice: (result: EditDeviceMutation, args: EditDeviceMutationVariables, cache, _info) => {
                const newDeviceName = result.editDevice.name
                cache.updateQuery({
                    query: GetDevicesDocument,
                    variables: {
                        id: args.userId,
                        types: // ???
                    }
                }, (data: GetDevicesQuery | null) => {
                    return data
                })
            }
        }
    },
})

I need types variable from the query I executed but I don't have an access to it in the updater function. Also if I have multiple GetDevicesDocument queries using different variables I want to update them all.
I assume I have to use cache.InspectFields or something but how?

0

There are 0 best solutions below