We’re migrating from Apollo 0.x to 1.22. We need to initialize Apollo classes manually, because we intercept and “fake” some of GQL endpoint responses. I’m currently using this in Apollo 1.22:
let itemDetail = itemDTO.toGQLModel()
RenameItemMutation.Data.RenameItem
return RenameItemMutation.Data(
renameItem: RenameItemMutation.Data.RenameItem(
_dataDict: itemDetail.__data
)
)
This works properly. However _dataDict initializer is for internal use, and I’d like to know if there is a cleaner way to create RenameItemMutation.Data.RenameItem and ItemDetail. For these particular classes there are no typesafe initializers generated.
We have set selectionSetInitializers configuration for both operations and named fragments.
itemDTO.toGQLModel() impl
extension ItemDTO {
func toGQLModel() -> ItemDetail {
switch self {
case .file(let file):
// also having to use _dataDict initializer here
return .init(_dataDict: file.toGQLModel().__data)
case .folder(let folder):
return .init(_dataDict: folder.toGQLModel().__data)
case .webLink(let weblink):
return .init(_dataDict: weblink.toGQLModel().__data)
}
}
}
extension FileDTO {
func toGQLModel() -> ItemDetail.AsFile {
return ItemDetail.AsFile(
id: .init(id),
type: .init(.file),
name: name,
sha1: sha1,
[...] // more fields
)
}
}
// here we can use type-safe initializers
extension FolderDTO {
func toGQLModel() -> ItemDetail.AsFolder {
ItemDetail.AsFolder(
id: .init(id),
type: .init(.folder),
name: name,
createdAt: createdAt?.utcString,
[...] // more fields
)
}
}
extension WebLinkDTO {
func toGQLModel() -> ItemDetail.AsWeblink {
ItemDetail.AsWeblink(
id: .init(id),
type: .init(.webLink),
name: name,
url: url,
[...] // more fields
)
}
}
gql definitions
mutation RenameItem(
$id: String!,
$type: ItemType!,
$newName: String!,
) {
renameItem(input: {
itemId: $id,
type: $type,
newName: $newName,
}) {
...ItemDetail
}
}
fragment ItemDetail on Item {
...on CoreItem {
...CoreItemDetail
}
...on File {
...FileDetail
}
...on Folder {
...FolderDetail
}
...on Weblink {
...WeblinkDetail
}
}
fragment FileDetail on File { //similar for Folder, Weblink
size
hasCollaborations
isExternallyOwned
sha1
[...] more fields
}
Question: Can we do better to avoid using the internal initializer forRenameItemMutation.Data.RenameItem and ItemDetail?
Let me know if you would need more details.