错误榆树处理graphql(0.18)
问题描述:
我试图用榆树0.18使用graphql。我在网上找到的图书馆似乎没有0.18的工作,所以我滚动自己的。错误榆树处理graphql(0.18)
比方说,我有一个嵌套查询。这使得查询和HTTP调用函数看起来是这样的:
import Http
import HttpBuilder exposing (..)
import Json.Decode as Decode exposing (..)
import Json.Encode as Encode exposing (..)
import Json.Decode.Pipeline as Pipeline exposing (decode, required)
fetchPosts : Model -> Cmd Msg
fetchPosts model =
let
graphiql =
"""
query {
postById(id: 1) {
id
author {
id
name
}
content
comments {
date
author {
id
name
}
content
}
}
}
"""
localUserDecoder =
Pipeline.decode User
|> Pipeline.required "id" Decode.int
|> Pipeline.required "name" Decode.string
localCommentDecoder =
Pipeline.decode Comment
|> Pipeline.required "date" Decode.string
|> Pipeline.required "author" localUserDecoder
|> Pipeline.required "string" Decode.string
localPostDecoder =
Pipeline.decode Post
|> Pipeline.required "id" Decode.int
|> Pipeline.required "author" localUserDecoder
|> Pipeline.required "content" Decode.string
|> Pipeline.required "comments" (Decode.list localCommentDecoder)
localDecoder =
Decode.at [ "data", "postById" ] <|
localPostDecoder
in
HttpBuilder.post ("http://myserver/api")
|> HttpBuilder.withStringBody "text/plain" graphiql
|> HttpBuilder.withExpect (Http.expectJson localDecoder)
|> HttpBuilder.send GetPostCompleted
当它通过,并沿着返回Post
类型GetPostCompleted
通过,一切都很好。但是,假设有些事情是关闭的。我误标为author
某处user
或字段是无序的解码器。编译器不会告诉我我在哪里犯了错误,而只是在Network表中看到一个正确的查询,但是从我的elm代码中抛出了一个非描述性错误。
是否有任何方法来构造这样的结构,如果其中一个解码器出现问题,我可以看到抛出控制台或错误的错误?目前,我有解压整个事情,把它连同一块一块,这是非常困难的,非榆树样。
答
我不知道你是正确的关于graphql库不支持0.18。
更大的问题是为什么无法从键入的elm数据结构转换为(有效)类型的graphql请求以及相应的解码器。但是这当然需要分析类型,这听起来超出了编译器的目标。 (请参阅https://www.youtube.com/watch?v=sh4H8yzXnvw了解如何使用泛型在Haskell中完成这项工作)
因此,即使现有的库需要更多的锅炉板,也比您希望的要多。我没有看到解决办法。
如果您可以迭代Elm记录类型的字段,那么您可以更直接地构建请求,但即使这样,您也无法获得自动生成的解码器......我想。
我什么不知道的是你是否能解析graphql JSON定义和派生从
我还是新榆树榆树解码器,但我听说过的类型系统的投诉。这是人们抱怨的主要事情吗? –
没有,投诉 - 比如他们 - 是关于类型类,并归结为额外的样板。在我看来,这需要更先进的类型系统(认为Haskell或更高版本) –