Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy Union Types #534

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ class DefaultAstSchemaBuilder[Ctx] extends AstSchemaBuilder[Ctx] {
existing: UnionType[Ctx],
types: List[ObjectType[Ctx, _]],
mat: AstSchemaMaterializer[Ctx]) =
existing.copy(types = types,
existing.copy(typesFn = () => types,
astDirectives = existing.astDirectives ++ extensions.flatMap(_.directives),
astNodes = existing.astNodes ++ extensions)

Expand Down
41 changes: 36 additions & 5 deletions modules/core/src/main/scala/sangria/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,51 @@ object PossibleType {
case class UnionType[Ctx](
name: String,
description: Option[String] = None,
types: List[ObjectType[Ctx, _]],
typesFn: () => List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive] = Vector.empty,
astNodes: Vector[ast.AstNode] = Vector.empty) extends OutputType[Any] with CompositeType[Any] with AbstractType with NullableType with UnmodifiedType with HasAstInfo {
def rename(newName: String) = copy(name = newName).asInstanceOf[this.type]
def toAst: ast.TypeDefinition = SchemaRenderer.renderType(this)

/**
* Creates a type-safe version of union type which might be useful in cases where the value is wrapped in a type like `Either`.
*/
def mapValue[T](func: T => Any): OutputType[T] = new UnionType[Ctx](name, description, types, astDirectives, astNodes) with MappedAbstractType[T] {
def mapValue[T](func: T => Any): OutputType[T] = new UnionType[Ctx](name, description, typesFn, astDirectives, astNodes) with MappedAbstractType[T] {
override def contraMap(value: T): Any = func(value)
}.asInstanceOf[OutputType[T]]

lazy val types = typesFn()
}

object UnionType {
def apply[Ctx](
name: String,
types: List[ObjectType[Ctx, _]]): UnionType[Ctx] =
UnionType[Ctx](name, None, () => types)

def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types)

def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types, astDirectives)

def apply[Ctx](
name: String,
description: Option[String],
types: List[ObjectType[Ctx, _]],
astDirectives: Vector[ast.Directive],
astNodes: Vector[ast.AstNode]): UnionType[Ctx] =
UnionType[Ctx](name, description, () => types, astDirectives, astNodes)
}



case class Field[Ctx, Val](
name: String,
fieldType: OutputType[_],
Expand Down Expand Up @@ -864,8 +895,8 @@ case class Schema[Ctx, Val](
t.interfaces.foldLeft(withPossible) {
case (acc, interface) => collectTypes(s"an interface defined in '${t.name}' type", priority, interface, acc)
}
case t @ UnionType(name, _, types, _, _) =>
types.foldLeft(updated(priority, name, t, result, parentInfo)) {case (acc, tpe) => collectTypes(s"a '$name' type", priority, tpe, acc)}
case t @ UnionType(name, _, _, _, _) =>
t.types.foldLeft(updated(priority, name, t, result, parentInfo)) {case (acc, tpe) => collectTypes(s"a '$name' type", priority, tpe, acc)}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class IntrospectionSchemaMaterializerSpec extends AnyWordSpec with Matchers with

lazy val FriendlyUnionType = UnionType("Friendly", types = DogUnionType :: HumanUnionType :: Nil)

lazy val LazilyInitializedUnionType = UnionType("FriendlyButLazy", typesFn = () => DogUnionType :: HumanUnionType :: Nil)

val CustomScalar = ScalarType[Int]("Custom",
description = Some("Some custom"),
coerceOutput = (i, _) => ast.IntValue(i),
Expand Down