Skip to content

Commit 12b6be5

Browse files
committed
Introduce fragment
1 parent bd6f151 commit 12b6be5

File tree

6 files changed

+75
-45
lines changed

6 files changed

+75
-45
lines changed

GitHub-GraphQL-API-Example-iOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
1201038B1DFBF28200E8128E /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1201038A1DFBF28200E8128E /* API.swift */; };
1616
12A3242C1DFE56EC00D905E7 /* RepositoryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12A3242B1DFE56EC00D905E7 /* RepositoryCell.swift */; };
1717
12A3242E1DFF499800D905E7 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 12A3242D1DFF499800D905E7 /* Main.storyboard */; };
18+
12A324301E0215E400D905E7 /* RepositoryDetails.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 12A3242F1E0215E400D905E7 /* RepositoryDetails.graphql */; };
1819
12F01AA41DFD05CD00EB0547 /* RepositoriesViewController.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 12F01AA31DFD05CD00EB0547 /* RepositoriesViewController.graphql */; };
1920
/* End PBXBuildFile section */
2021

@@ -29,6 +30,7 @@
2930
1201038A1DFBF28200E8128E /* API.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
3031
12A3242B1DFE56EC00D905E7 /* RepositoryCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepositoryCell.swift; sourceTree = "<group>"; };
3132
12A3242D1DFF499800D905E7 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
33+
12A3242F1E0215E400D905E7 /* RepositoryDetails.graphql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = RepositoryDetails.graphql; sourceTree = "<group>"; };
3234
12F01AA31DFD05CD00EB0547 /* RepositoriesViewController.graphql */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = RepositoriesViewController.graphql; sourceTree = "<group>"; };
3335
/* End PBXFileReference section */
3436

@@ -70,6 +72,7 @@
7072
1201038A1DFBF28200E8128E /* API.swift */,
7173
12A3242D1DFF499800D905E7 /* Main.storyboard */,
7274
12F01AA31DFD05CD00EB0547 /* RepositoriesViewController.graphql */,
75+
12A3242F1E0215E400D905E7 /* RepositoryDetails.graphql */,
7376
1201037A1DFBE26E00E8128E /* Assets.xcassets */,
7477
1201037C1DFBE26E00E8128E /* LaunchScreen.storyboard */,
7578
1201037F1DFBE26E00E8128E /* Info.plist */,
@@ -151,6 +154,7 @@
151154
1201037B1DFBE26E00E8128E /* Assets.xcassets in Resources */,
152155
12F01AA41DFD05CD00EB0547 /* RepositoriesViewController.graphql in Resources */,
153156
12A3242E1DFF499800D905E7 /* Main.storyboard in Resources */,
157+
12A324301E0215E400D905E7 /* RepositoryDetails.graphql in Resources */,
154158
);
155159
runOnlyForDeploymentPostprocessing = 0;
156160
};

GitHub-GraphQL-API-Example-iOS/API.swift

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,13 @@ public final class SearchRepositoriesQuery: GraphQLQuery {
1010
" node {" +
1111
" __typename" +
1212
" ... on Repository {" +
13-
" name" +
14-
" owner {" +
15-
" __typename" +
16-
" path" +
17-
" }" +
18-
" stargazers {" +
19-
" totalCount" +
20-
" }" +
21-
" url" +
13+
" ...RepositoryDetails" +
2214
" }" +
2315
" }" +
2416
" }" +
2517
" }" +
2618
"}"
19+
public static let queryDocument = operationDefinition.appending(RepositoryDetails.fragmentDefinition)
2720

2821
public let query: String
2922
public let count: Int
@@ -75,39 +68,69 @@ public final class SearchRepositoriesQuery: GraphQLQuery {
7568
public static let possibleTypes = ["Repository"]
7669

7770
public let __typename = "Repository"
78-
public let name: String
79-
public let owner: Owner
80-
public let stargazers: Stargazer
81-
public let url: String
8271

83-
public init(reader: GraphQLResultReader) throws {
84-
name = try reader.value(for: Field(responseName: "name"))
85-
owner = try reader.value(for: Field(responseName: "owner"))
86-
stargazers = try reader.value(for: Field(responseName: "stargazers"))
87-
url = try reader.value(for: Field(responseName: "url"))
88-
}
89-
90-
public struct Owner: GraphQLMappable {
91-
public let __typename: String
92-
public let path: String
72+
public let fragments: Fragments
9373

94-
public init(reader: GraphQLResultReader) throws {
95-
__typename = try reader.value(for: Field(responseName: "__typename"))
96-
path = try reader.value(for: Field(responseName: "path"))
97-
}
74+
public init(reader: GraphQLResultReader) throws {
75+
let repositoryDetails = try RepositoryDetails(reader: reader)
76+
fragments = Fragments(repositoryDetails: repositoryDetails)
9877
}
9978

100-
public struct Stargazer: GraphQLMappable {
101-
public let __typename = "StargazerConnection"
102-
public let totalCount: Int
103-
104-
public init(reader: GraphQLResultReader) throws {
105-
totalCount = try reader.value(for: Field(responseName: "totalCount"))
106-
}
79+
public struct Fragments {
80+
public let repositoryDetails: RepositoryDetails
10781
}
10882
}
10983
}
11084
}
11185
}
11286
}
87+
}
88+
89+
public struct RepositoryDetails: GraphQLNamedFragment {
90+
public static let fragmentDefinition =
91+
"fragment RepositoryDetails on Repository {" +
92+
" name" +
93+
" owner {" +
94+
" __typename" +
95+
" path" +
96+
" }" +
97+
" stargazers {" +
98+
" totalCount" +
99+
" }" +
100+
" url" +
101+
"}"
102+
103+
public static let possibleTypes = ["Repository"]
104+
105+
public let __typename = "Repository"
106+
public let name: String
107+
public let owner: Owner
108+
public let stargazers: Stargazer
109+
public let url: String
110+
111+
public init(reader: GraphQLResultReader) throws {
112+
name = try reader.value(for: Field(responseName: "name"))
113+
owner = try reader.value(for: Field(responseName: "owner"))
114+
stargazers = try reader.value(for: Field(responseName: "stargazers"))
115+
url = try reader.value(for: Field(responseName: "url"))
116+
}
117+
118+
public struct Owner: GraphQLMappable {
119+
public let __typename: String
120+
public let path: String
121+
122+
public init(reader: GraphQLResultReader) throws {
123+
__typename = try reader.value(for: Field(responseName: "__typename"))
124+
path = try reader.value(for: Field(responseName: "path"))
125+
}
126+
}
127+
128+
public struct Stargazer: GraphQLMappable {
129+
public let __typename = "StargazerConnection"
130+
public let totalCount: Int
131+
132+
public init(reader: GraphQLResultReader) throws {
133+
totalCount = try reader.value(for: Field(responseName: "totalCount"))
134+
}
135+
}
113136
}

GitHub-GraphQL-API-Example-iOS/RepositoriesViewController.graphql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ query SearchRepositories($query: String!, $count: Int!) {
33
edges {
44
node {
55
... on Repository {
6-
name
7-
owner {
8-
path
9-
}
10-
stargazers {
11-
totalCount
12-
}
13-
url
6+
...RepositoryDetails
147
}
158
}
169
}

GitHub-GraphQL-API-Example-iOS/RepositoriesViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class RepositoriesViewController: UITableViewController {
3030
if let error = error { print("Error: \(error)"); return }
3131

3232
result?.data?.search.edges?.forEach { edge in
33-
guard let repository = edge?.node?.asRepository else { return }
33+
guard let repository = edge?.node?.asRepository?.fragments.repositoryDetails else { return }
3434
print("Name: \(repository.name)")
3535
print("Path: \(repository.url)")
3636
print("Owner: \(repository.owner.path)")
@@ -55,7 +55,7 @@ final class RepositoriesViewController: UITableViewController {
5555
fatalError()
5656
}
5757

58-
cell.configure(with: repository)
58+
cell.configure(with: repository.fragments.repositoryDetails)
5959
return cell
6060
}
6161

GitHub-GraphQL-API-Example-iOS/RepositoryCell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class RepositoryCell: UITableViewCell {
99
super.awakeFromNib()
1010
}
1111

12-
func configure(with repository: SearchRepositoriesQuery.Data.Search.Edge.Node.AsRepository) {
12+
func configure(with repository: RepositoryDetails) {
1313
nameLabel.text = "\(repository.owner.path)/\(repository.name)"
1414
urlLabel.text = repository.url
1515
stargazersCountLabel.text = "Stars: \(repository.stargazers.totalCount)"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fragment RepositoryDetails on Repository {
2+
name
3+
owner {
4+
path
5+
}
6+
stargazers {
7+
totalCount
8+
}
9+
url
10+
}

0 commit comments

Comments
 (0)