Skip to content

Commit

Permalink
Merge pull request #178 from 47deg/graphql
Browse files Browse the repository at this point in the history
Graphql interpreter example
  • Loading branch information
purrgrammer authored Feb 26, 2019
2 parents 339cd52 + 6a3df5d commit db67321
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ lazy val debugJS = debug.js

lazy val examples = (project in file("examples"))
.settings(name := "fetch-examples")
.dependsOn(fetchJVM)
.dependsOn(fetchJVM, debugJVM)
.settings(noPublishSettings: _*)
.settings(examplesSettings: _*)
.settings(Seq(
Expand Down
46 changes: 32 additions & 14 deletions examples/src/test/scala/GithubExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,36 @@ class GithubExample extends WordSpec with Matchers {

case class Project(repo: Repo, contributors: List[Contributor], languages: List[Language])

"We can fetch org repos" in {
def fetchProject[F[_]: ConcurrentEffect](repo: Repo): Fetch[F, Project] =
(repoContributors(repo), repoLanguages(repo)).mapN({
case (contribs, langs) =>
Project(repo = repo, contributors = contribs, languages = langs)
})

def fetch[F[_]: ConcurrentEffect] =
for {
repos <- orgRepos("47deg")
projects <- repos.traverse(fetchProject[F])
} yield projects
def fetchProject[F[_]: ConcurrentEffect](repo: Repo): Fetch[F, Project] =
(repoContributors(repo), repoLanguages(repo)).mapN({
case (contribs, langs) =>
Project(repo = repo, contributors = contribs, languages = langs)
})

val io = Fetch.runLog[IO](fetch)
def fetchOrg[F[_]: ConcurrentEffect](org: String) =
for {
repos <- orgRepos(org)
projects <- repos.traverse(fetchProject[F])
} yield projects

def fetchOrgStars[F[_]: ConcurrentEffect](org: String): Fetch[F, Int] =
fetchOrg(org).map(projects => projects.map(_.repo.stargazers_count).sum)

def fetchOrgContributors[F[_]: ConcurrentEffect](org: String): Fetch[F, Int] =
fetchOrg(org).map(projects => projects.map(_.contributors.toSet).fold(Set())(_ ++ _).size)

def fetchOrgLanguages[F[_]: ConcurrentEffect](org: String): Fetch[F, Int] =
fetchOrg(org).map(projects => projects.map(_.languages.toSet).fold(Set())(_ ++ _).size)

"We can fetch org repos" in {
val io = Fetch.runLog[IO](fetchOrg("47deg"))

val (log, result) = io.unsafeRunSync

log.rounds.size shouldEqual 2
}

"We can fetch multiple repos in parallel" in {

def fetchRepo[F[_]: ConcurrentEffect](r: (String, String)): Fetch[F, Repo] =
Fetch(r, Repos.source)

Expand All @@ -228,6 +236,16 @@ class GithubExample extends WordSpec with Matchers {
log.rounds.size shouldEqual 1
}

"We can combine everything" in {
def fetch[F[_]: ConcurrentEffect](org: String): Fetch[F, (List[Project], Int, Int, Int)] =
(fetchOrg(org), fetchOrgStars(org), fetchOrgContributors(org), fetchOrgLanguages(org)).tupled

val io = Fetch.runLog[IO](fetch("47deg"))
val (log, result) = io.unsafeRunSync

log.rounds.size shouldEqual 2
}

// Github HTTP api

val GITHUB: Uri = Uri.unsafeFromString("https://api.github.com")
Expand Down
Loading

0 comments on commit db67321

Please sign in to comment.