-
温故GraphiQL学习指南相关笔记,加深对GraphiQL基本类型、查询、突变、订阅的理解
-
结合项目实际理解对象的传递
mutation CreateReviewForEpisode($episode: Episode!, $review: ReviewInput!) {
createReview(episode: $episode, review: $review) {
stars
commentary
} } let review = ReviewInput(stars: 5, commentary: "This is a great movie!") apollo.perform(mutation: CreateReviewForEpisodeMutation(episode: .jedi, review: review))
文件上传
mutation UploadFile($file:Upload!) {
singleUpload(file:$file) {
id
}
} // Create the file to upload guard let fileURL = Bundle.main.url(forResource: "a", withExtension: "txt"), let file = GraphQLFile(fieldName: "file", originalName: "a.txt", mimeType: "text/plain", // <-defaults to "application/octet-stream" fileURL: fileURL) else { // Either the file URL couldn't be created or the file couldn't be created. return } // Actually upload the file client.upload(operation: UploadFileMutation(file: "a"), // <-- `Upload` is a custom scalar that's a `String` under the hood. files: [file]) { result in switch result { case .success(let graphQLResult): print("ID: \(graphQLResult.data?.singleUpload.id)") case .failure(let error): print("error: \(error)") } }
- 有效上传
mutation AvatarUpload($userID: GraphQLID!, $file: Upload!) {
id
}
- 无效上传
// Assumes AvatarObject(userID: GraphQLID, file: Upload) exists
mutation AvatarUpload($avatarObject: AvatarObject!) {
id
}
- note:对象需要使用具体文件,如果上传文件数组需要同名,数量也得对应
-
片段使用
query HeroAndFriends($episode: Episode) {
hero(episode: $episode) {
name
...HeroDetails
friends { ...HeroDetails } } } fragment HeroDetails on Character { name appearsIn } func configure(with heroDetails: HeroDetails?) { textLabel?.text = heroDetails?.name } apollo.fetch(query: HeroAndFriendsQuery(episode: .empire)) { result in guard let data = try? result.get().data else { return } print(data.hero?.name) // Luke Skywalker print(data.hero?.appearsIn) // WON'T WORK print(data.hero?.fragments.heroDetails.appearsIn) // [.newhope, .empire, .jedi] print(data.hero?.friends?.flatMap { $0?.fragments.heroDetails.name }.joined(separator: ", ")) // Han Solo, Leia Organa, C-3PO, R2-D2 } cell.configure(with: hero?.fragments.heroDetails)