【问题标题】:Sangria simple graphQL query, but syntax errorSangria 简单的 graphQL 查询,但语法错误
【发布时间】:2018-12-31 05:18:06
【问题描述】:

我正在尝试使用 Akka Http + Sangria 实现一个带有 graphql 端点的玩具服务器。但是,当我发送带有有效负载的 POST 请求时(直接 cppy 并从 Chrome 检查粘贴)

{"query":"查询 FetchPokemons($height: Int) {\n pokemonsWithHeight(height: $height) {\n name\n }\n}","变量":{"height":100},"operationName":"FetchPokemons"}

服务器报错

sangria.parser.SyntaxError:解析 GraphQL 查询时出现语法错误。 输入意外结束,预期联合,评论,接口,标量, 枚举、类型、输入类型或指令(第 1 行,第 79 列):“查询 FetchPokemons {\n pokemonsWithHeight(height: 100) {\n name\n }\n}"

但是在检查了 graphql 文档之后,这个查询字符串似乎没有理由在解析阶段失败。这是我处理 http 正文的代码。

def queryResult(request: Json, schema: Schema[Resolvers, Unit], resolvers: Resolvers)(
  implicit ec: ExecutionContext
): Route = { ctx =>
  for {
    queryJson <- Future.fromTry(Try(request.findAllByKey("query").head))
    queryAst  <- Future.fromTry(QueryParser.parse(queryJson.noSpaces))
    variables =  request.findAllByKey("variables").headOption.getOrElse(Json.obj())
    opName    =  request.findAllByKey("operationName").headOption.flatMap(_.asString)
    result    <- Executor.execute(schema, queryAst, resolvers, operationName = opName, variables = variables)
    res       <- ctx.complete(StatusCodes.OK -> result)
  } yield res
}

【问题讨论】:

    标签: graphql sangria


    【解决方案1】:

    我不得不从

    中手动删除 \" 和 \\n

    "查询 FetchPokemons($height: Int) {\n pokemonsWithHeight(height: $height) {\n 姓名\n }\n}"

    查询 FetchPokemons($height: Int) { pokemonsWithHeight(height: $height) { 名称 }}

    进入解析器使用

    def formatQuery(s: String): String = s.replaceAllLiterally("\\n","").replaceAllLiterally("\"", "")

    此解决方案简单但不干净。我希望有更好的方法——也许修改 queryJson.nospace?

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 2023-04-04
      • 2019-04-18
      • 2020-09-12
      • 2013-08-22
      • 2012-12-05
      • 2016-12-18
      • 1970-01-01
      相关资源
      最近更新 更多