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

gqldoc macro for Scala 3 #849

Merged
merged 1 commit into from
Apr 17, 2021
Merged
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
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"))
)
}
}