Skip to content

Commit

Permalink
Add ability to add types explicitly in schema, for cases when they ar…
Browse files Browse the repository at this point in the history
…e not referenced anywhere else, closes #44
  • Loading branch information
OlegIlyenko committed Aug 9, 2015
1 parent 0d074ab commit ab62ad3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/scala/sangria/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ case class Directive(
case class Schema[Ctx, Val](
query: ObjectType[Ctx, Val],
mutation: Option[ObjectType[Ctx, Val]] = None,
additionalTypes: List[Type with Named] = Nil,
directives: List[Directive] = BuiltinDirectives) {
lazy val types: Map[String, (Int, Type with Named)] = {
def updated(priority: Int, name: String, tpe: Type with Named, result: Map[String, (Int, Type with Named)]) =
Expand Down Expand Up @@ -320,7 +321,8 @@ case class Schema[Ctx, Val](

val schemaTypes = collectTypes(3, introspection.__Schema, Map(BuiltinScalars map (s => s.name -> (4, s)): _*))
val queryTypes = collectTypes(2, query, schemaTypes)
val queryAndMutTypes = mutation map (collectTypes(1, _, queryTypes)) getOrElse queryTypes
val queryTypesWithAdditions = queryTypes ++ additionalTypes.map(t => t.name -> (1, t))
val queryAndMutTypes = mutation map (collectTypes(1, _, queryTypesWithAdditions)) getOrElse queryTypesWithAdditions

queryAndMutTypes
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/scala/sangria/starWars/StarWarsQuerySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sangria.starWars
import org.scalatest.{Matchers, WordSpec}
import sangria.execution.Executor
import sangria.parser.QueryParser
import sangria.schema._
import sangria.starWars.TestSchema.StarWarsSchema
import sangria.starWars.TestData.{FriendsResolver, CharacterRepo}
import sangria.util.AwaitSupport
Expand Down Expand Up @@ -52,6 +53,41 @@ class StarWarsQuerySpec extends WordSpec with Matchers with AwaitSupport {
Map("name" -> "Leia Organa")
)))))
}

"Hero query should succeed even if not all types are referenced indirectly" in {
val Success(query) = QueryParser.parse("""
query HeroNameAndFriendsQuery {
hero {
id
name
friends {
name
}
}
}
""")

val HeroOnlyQuery = ObjectType[CharacterRepo, Unit](
"HeroOnlyQuery", List[Field[CharacterRepo, Unit]](
Field("hero", TestSchema.Character,
arguments = TestSchema.EpisodeArg :: Nil,
resolve = (ctx) => ctx.ctx.getHero(ctx.argOpt(TestSchema.EpisodeArg)))
))

val heroOnlySchema = Schema(HeroOnlyQuery, additionalTypes = TestSchema.Human :: TestSchema.Droid :: Nil)

Executor(heroOnlySchema, userContext = new CharacterRepo, deferredResolver = new FriendsResolver).execute(query).await should be (
Map(
"data" -> Map(
"hero" -> Map(
"id" -> "2001",
"name" -> "R2-D2",
"friends" -> List(
Map("name" -> "Luke Skywalker"),
Map("name" -> "Han Solo"),
Map("name" -> "Leia Organa")
)))))
}
}

"Nested Queries" should {
Expand Down

0 comments on commit ab62ad3

Please sign in to comment.