Skip to content

Commit

Permalink
Default to terse parser error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
milessabin committed Dec 15, 2023
1 parent 54f5c4c commit 0b7ed52
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 11 additions & 4 deletions modules/core/src/main/scala/parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,17 @@ object GraphQLParser {
maxSelectionDepth: Int,
maxSelectionWidth: Int,
maxInputValueDepth: Int,
maxListTypeDepth: Int
maxListTypeDepth: Int,
terseError: Boolean
)

val defaultConfig: Config =
Config(
maxSelectionDepth = 100,
maxSelectionWidth = 1000,
maxInputValueDepth = 5,
maxListTypeDepth = 5
maxListTypeDepth = 5,
terseError = true
)

def apply(config: Config): GraphQLParser =
Expand All @@ -49,14 +51,19 @@ object GraphQLParser {
def toResult[T](pr: Either[Parser.Error, T]): Result[T] =
Result.fromEither(pr.leftMap(_.show))

def toResultTerseError[T](pr: Either[Parser.Error, T]): Result[T] =
Result.fromEither(pr.leftMap(_.copy().show))

import CommentedText._
import Literals._

private final class Impl(config: Config) extends GraphQLParser {
import config._

def parseText(text: String): Result[Ast.Document] =
toResult(Document.parseAll(text))
def parseText(text: String): Result[Ast.Document] = {
val res = Document.parseAll(text)
if (config.terseError) toResultTerseError(res) else toResult(res)
}

val nameInitial = ('A' to 'Z') ++ ('a' to 'z') ++ Seq('_')
val nameSubsequent = nameInitial ++ ('0' to '9')
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/test/scala/compiler/CompilerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Predicate._, Value._, UntypedOperation._
import QueryCompiler._, ComponentElaborator.TrivialJoin

final class CompilerSuite extends CatsEffectSuite {
val queryParser = QueryParser(GraphQLParser(GraphQLParser.defaultConfig))
val queryParser = QueryParser(GraphQLParser(GraphQLParser.defaultConfig.copy(terseError = false)))

test("simple query") {
val query = """
Expand Down
9 changes: 6 additions & 3 deletions modules/core/src/test/scala/parser/ParserSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import grackle.syntax._
import Ast._, OperationType._, OperationDefinition._, Selection._, Value._, Type.Named

final class ParserSuite extends CatsEffectSuite {
val parser = GraphQLParser(GraphQLParser.defaultConfig)
val parser = mkParser()

test("simple query") {
val query = doc"""
Expand Down Expand Up @@ -801,12 +801,15 @@ final class ParserSuite extends CatsEffectSuite {
maxSelectionDepth: Int = GraphQLParser.defaultConfig.maxSelectionDepth,
maxSelectionWidth: Int = GraphQLParser.defaultConfig.maxSelectionWidth,
maxInputValueDepth: Int = GraphQLParser.defaultConfig.maxInputValueDepth,
maxListTypeDepth: Int = GraphQLParser.defaultConfig.maxListTypeDepth): GraphQLParser =
maxListTypeDepth: Int = GraphQLParser.defaultConfig.maxListTypeDepth,
): GraphQLParser =
GraphQLParser(
GraphQLParser.Config(
maxSelectionDepth = maxSelectionDepth,
maxSelectionWidth = maxSelectionWidth,
maxInputValueDepth = maxInputValueDepth,
maxListTypeDepth = maxListTypeDepth)
maxListTypeDepth = maxListTypeDepth,
terseError = false
)
)
}

0 comments on commit 0b7ed52

Please sign in to comment.