Skip to content

Commit

Permalink
Optimize field parsing (#2102)
Browse files Browse the repository at this point in the history
* Optimize field parsing

* Make mima happy
  • Loading branch information
kyri-petrou authored Jan 30, 2024
1 parent 9c3d9c9 commit 8d4a177
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions core/src/main/scala/caliban/parsing/parsers/SelectionParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import scala.annotation.nowarn

@nowarn("msg=NoWhitespace") // False positive warning in Scala 2.x
private[caliban] trait SelectionParsers extends ValueParsers {

@deprecated("Kept for bincompat only, scheduled to be removed")
def alias(implicit ev: P[Any]): P[String] = P(name ~ ":")
def aliasOrName(implicit ev: P[Any]): P[String] = P(":" ~ name)

def argument(implicit ev: P[Any]): P[(String, InputValue)] = P(name ~ ":" ~ value)
def arguments(implicit ev: P[Any]): P[Map[String, InputValue]] = P("(" ~/ argument.rep ~ ")").map(_.toMap)
Expand All @@ -27,23 +30,24 @@ private[caliban] trait SelectionParsers extends ValueParsers {
def namedType(implicit ev: P[Any]): P[NamedType] = P(name.filter(_ != "null")).map(NamedType(_, nonNull = false))
def listType(implicit ev: P[Any]): P[ListType] = P("[" ~ type_ ~ "]").map(t => ListType(t, nonNull = false))

@deprecated("Kept for bincompat only, scheduled to be removed")
def nonNullType(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!").map {
case t: NamedType => t.copy(nonNull = true)
case t: ListType => t.copy(nonNull = true)
}
def type_(implicit ev: P[Any]): P[Type] = P(nonNullType | namedType | listType)

def type_(implicit ev: P[Any]): P[Type] = P((namedType | listType) ~ "!".!.?).map {
case (t: NamedType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t
case (t: ListType, nn) => if (nn.isDefined) t.copy(nonNull = true) else t
}

def field(implicit ev: P[Any]): P[Field] =
P(Index ~ alias.? ~ name ~ arguments.? ~ directives.? ~ selectionSet.?).map {
case (index, alias, name, args, dirs, sels) =>
Field(
alias,
name,
args.getOrElse(Map()),
dirs.getOrElse(Nil),
sels.getOrElse(Nil),
index
)
P(Index ~ name ~ aliasOrName.? ~ arguments.? ~ directives.? ~ selectionSet.?).map {
case (index, alias, Some(name), args, dirs, sels) =>
Field(Some(alias), name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index)
case (index, name, _, args, dirs, sels) =>
Field(None, name, args.getOrElse(Map()), dirs.getOrElse(Nil), sels.getOrElse(Nil), index)

}

def fragmentName(implicit ev: P[Any]): P[String] = P(name).filter(_ != "on")
Expand Down

0 comments on commit 8d4a177

Please sign in to comment.