diff --git a/core/src/main/scala-3/caliban/Macros.scala b/core/src/main/scala-3/caliban/Macros.scala new file mode 100644 index 0000000000..621ecf0ac8 --- /dev/null +++ b/core/src/main/scala-3/caliban/Macros.scala @@ -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")) + ) + } +}