There have been a few places I've run into this, but most recently I was working on a profile page and it seems like anyplace you'd want to display repos (pinned, starred, users' repos, etc), you could make a graphql fragment that mirrors the SearchRepos Query and reuse existing functionality from there.. eg reuse the code in GithubClient.search to return SearchReposResult models, and then reuse the SearchResultSectionControllers.
So is there a way to handle the Repo fragment in different queries and convert them to SearchRepoResult models without duplicating a bunch of code each time? Or a best practice that you all have been using?
update 6/14:
The following represents my current best try.
the recurring fragment that will be in multiple queries:
fragment RepoSearchFields on Repository {
id
owner {
login
}
defaultBranchRef {
name
}
}
making all the instances of the fragment conform to a protocol:
protocol RepositoryFragment {
var _id: String { get }
var _owner: String { get }
var _defaultBranch: String { get }
}
extension UserStarredReposQuery.Data.User.StarredRepository.Node: RepositoryFragment {
var _id: String { get { return id }}
var _owner: String { get { return owner.login }}
var _defaultBranch: String { get { return defaultBranchRef?.name ?? "master" }}
}
extension UserReposQuery.Data.User.Repository.Node: RepositoryFragment {
var _id: String { get { return id }}
var _owner: String { get { return owner.login }}
var _defaultBranch: String { get { return defaultBranchRef?.name ?? "master" }}
}
The advantage of using a protocol being I think that it makes it easier to handle those fragments when returned inside an array:
Might be worth asking in the Apollo repo too. I'm not sure what the best approach should be here. The duplication does stink, but its the least complex solution that I can think of at the moment.
There have been a few places I've run into this, but most recently I was working on a profile page and it seems like anyplace you'd want to display repos (pinned, starred, users' repos, etc), you could make a graphql fragment that mirrors the SearchRepos Query and reuse existing functionality from there.. eg reuse the code in GithubClient.search to return SearchReposResult models, and then reuse the SearchResultSectionControllers.
So is there a way to handle the Repo fragment in different queries and convert them to SearchRepoResult models without duplicating a bunch of code each time? Or a best practice that you all have been using?
update 6/14: The following represents my current best try.
the recurring fragment that will be in multiple queries:
making all the instances of the fragment conform to a protocol:
The advantage of using a protocol being I think that it makes it easier to handle those fragments when returned inside an array: