-
温故GraphiQL学习指南相关笔记,加深对GraphiQL基本类型、查询、突变、订阅的理解
-
结合项目实际理解对象的传递
mutation CreateReviewForEpisode($episode: Episode!, $review: ReviewInput!) {
createReview(episode: $episode, review: $review) {
stars
commentary
}
}
apollo.perform(mutation: CreateReviewForEpisodeMutation(episode: .jedi, review: review))
文件上传
mutation UploadFile($file:Upload!) {
singleUpload(file:$file) {
id
}
}
// Create the file to upload
guard
fileURL: fileURL) else {
// Either the file URL couldn't be created or the file couldn't be created.
return
}
// Actually upload the file
files: [file]) { result in
switch result {
case .success(let graphQLResult):
case .failure(let 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]
}
cell.configure(with: hero?.fragments.heroDetails)