Skip to content

Commit

Permalink
gqldoc macro for Scala 3 (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
javimartinez authored Apr 17, 2021
1 parent 159cced commit 382fbc7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions core/src/main/scala-3/caliban/Macros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package caliban

import scala.quoted._

import caliban.parsing.Parser

object Macros {

/**
* Verifies at compile-time that the given string is a valid GraphQL document.
* @param document a string representing a GraphQL document.
*/
inline def gqldoc(inline document: String): String = ${ gqldocImpl('document) }

private def gqldocImpl(document: Expr[String])(using Quotes): Expr[String] = {
import quotes.reflect.report
document.value.fold(report.throwError("This macro can only be used with string literals."))(
Parser.check(_).fold(document)(e => report.throwError(s"GraphQL document is invalid: $e"))
)
}
}

0 comments on commit 382fbc7

Please sign in to comment.