在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):jamesmacaulay/elm-graphql开源软件地址(OpenSource Url):https://github.com/jamesmacaulay/elm-graphql开源编程语言(OpenSource Language):Elm 100.0%开源软件介绍(OpenSource Introduction):jamesmacaulay/elm-graphqlA GraphQL library for Elm, written entirely in Elm. The goal of this package is to provide a really good interface for working directly with GraphQL queries and schemas in Elm. Right now the main offering of the package is an interface for building up nested queries and mutations in a way that also builds up a decoder capable of decoding successful responses to the request. The package also provides a module for sending these requests to a GraphQL server over HTTP and decoding the responses accordingly. The docs can be found here. And here's an end-to-end example that builds a query, sends it to a server, and decodes the response. Building requestsBuilding up a GraphQL query with this package feels a lot like building a JSON decoder, especially if you are familiar with the elm-decode-pipeline package. First you define type aliases for each of the nested record types you want to construct out of the response: import GraphQL.Request.Builder exposing (..)
import GraphQL.Request.Builder.Arg as Arg
import GraphQL.Request.Builder.Variable as Var
type alias Photo =
{ url : String
, caption : String
}
type alias User =
{ name : String
, photos : List Photo } Then you build a query document: userQuery : Document Query User { vars | userID : String }
userQuery =
let
userIDVar =
Var.required "userID" .userID Var.id
photo =
object Photo
|> with (field "url" [] string)
|> with (field "caption" [] string)
user =
object User
|> with (field "name" [] string)
|> with (field "photos" [] (list photo))
queryRoot =
extract
(field "user"
[ ( "id", Arg.variable userIDVar ) ]
user
)
in
queryDocument queryRoot The
Here's what the above Document looks like when you encode it to a string to be sent to the server: query ($userID: ID!) {
user(id: $userID) {
name
photos {
url
caption
}
}
} To supply values for the variables used in the userQueryRequest : Request Query User
userQueryRequest =
userQuery
|> request { userID = "123" } Assuming you've built a query that is valid for the server's schema, sending it to the server will result in a JSON response that can be decoded with a JSON decoder that is built up automatically along with the structure of the query. Here's what a JSON response for
When it is decoded with the help of the decoder contained in { name = "Lola"
, photos =
[ { url = "http://cdn.catphotos.com/lola.jpg"
, caption = "Lola curling up on the chair"
}
]
} Mutations are built just like queries, except that you wrap them up in a call to type alias LoginVars =
{ username : String
, password : String
}
loginMutation : Document Mutation String LoginVars
loginMutation =
let
usernameVar =
Var.required "username" .username Var.string
passwordVar =
Var.required "password" .password Var.string
in
mutationDocument <|
extract
(field "login"
[ ( "username", Arg.variable usernameVar )
, ( "password", Arg.variable passwordVar )
]
(extract (field "token" [] string))
) Future plans for this packageThere are a lot of things that this package can't do right now, but might do in the future. What gets done depends on how the package ends up being used, and how much demand there is for each feature. Here are some likely possibilities:
Getting and giving helpIf you're having trouble figuring out how to do something with this package, check out the #graphql channel on the Elm Slack — there are usually people there who can help. And if you can be one of those people who help other people, then thank you! Running the testsInstall the
Make sure you have the latest version of Now run
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论