/
Launch Graph Manager

9. Fragments and cache manipulation


🚧 THIS SECTION UNDER CONSTRUCTION 🚧

Using a Fragment

LaunchDetails.graphql
fragment LaunchDetails on Launch {
  id
  site
  mission {
    name
    missionPatch(size:LARGE)
  }
  rocket {
    name
    type
  }
  isBooked
}
LaunchDetails.graphql
query LaunchDetails($id:ID!) {
  launch(id: $id) {
    ...LaunchDetails
  }
}
BookTrip.graphql
mutation BookTrip($id:ID!) {
  bookTrips(launchIds: [$id]) {
    success
    message
    launches {
      ... LaunchDetails
    }
  }
}
CancelTrip.graphql
mutation CancelTrip($id:ID!) {
  cancelTrip(launchId: $id) {
    success
    message
    launches {
      ... LaunchDetails
    }
  }
}

Updating the cache yourself

DetailViewController.swift
private func updateCachedDetailQuery(with details: LaunchDetails) {
  DispatchQueue.global(qos: .background).async {
    Network.shared.apollo.store.withinReadWriteTransaction({ transaction in
      let query = LaunchDetailsQuery(id: details.id)
      try transaction.update(query: query) { (data: inout LaunchDetailsQuery.Data) in
        data.launch?.fragments.launchDetails = details
      }
    })
  }
}

Now go out and go back in again.

Edit on GitHub