gleamql
Query a GraphQL server with gleamql
.
gleamql.new()
|> gleamql.set_query(country_query)
|> gleamql.set_variable("code", json.string("GB"))
|> gleamql.set_host("countries.trevorblades.com")
|> gleamql.set_path("/graphql")
|> gleamql.set_header("Content-Type", "application/json")
|> gleamql.set_decoder(dynamic.decode1(
Data,
field("country", of: dynamic.decode1(Country, field("name", of: string))),
))
|> gleamql.send(hackney.send)
Types
GleamQL Error
pub type GraphQLError {
ErrorMessage(message: String)
UnexpectedStatus(status: Int)
UnrecognisedResponse(response: String)
UnknownError
}
Constructors
-
ErrorMessage(message: String)
-
UnexpectedStatus(status: Int)
-
UnrecognisedResponse(response: String)
-
UnknownError
GleamQL Request
pub type Request(t) {
Request(
http_request: request.Request(String),
query: option.Option(String),
variables: option.Option(List(#(String, json.Json))),
decoder: option.Option(decode.Decoder(t)),
)
}
Constructors
-
Request( http_request: request.Request(String), query: option.Option(String), variables: option.Option(List(#(String, json.Json))), decoder: option.Option(decode.Decoder(t)), )
Values
pub fn send(
req: Request(t),
send: fn(request.Request(String)) -> Result(
response.Response(String),
b,
),
) -> Result(option.Option(t), GraphQLError)
Send the built request to a GraphQL server.
A HTTP client is needed to send the request, see https://github.com/gleam-lang/http#client-adapters.
pub fn set_decoder(
req: Request(t),
decoder: decode.Decoder(t),
) -> Request(t)
Set the decoder that will be used to deserialize the graphql response.
If not given, the response will not be deserialized.
gleamql.set_decoder(dynamic.decode1(
Data,
field("country", of: dynamic.decode1(Country, field("name", of: string))),
))
pub fn set_default_content_type_header(
req: Request(t),
) -> Request(t)
Set the Content-Type
header to application/json
If already present, it is replaced.
gleamql.set_default_content_type_header()
pub fn set_header(
req: Request(t),
key: String,
value: String,
) -> Request(t)
Set the header with the given value under the given header key.
If already present, it is replaced.
gleamql.set_header("Content-Type", "application/json")
pub fn set_host(req: Request(t), host: String) -> Request(t)
Set the host of the request.
gleamql.set_host("countries.trevorblades.com")
pub fn set_path(req: Request(t), path: String) -> Request(t)
Set the path of the request.
gleamql.set_path("/graphql")