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: fn(country) {
      country
      |> 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(inner: Dynamic)
}

Constructors

  • ErrorMessage(message: String)
  • UnexpectedStatus(status: Int)
  • UnrecognisedResponse(response: String)
  • UnknownError(inner: Dynamic)

GleamQL Request

pub type Request(t) {
  Request(
    http_request: request.Request(String),
    query: Option(String),
    variables: Option(List(#(String, Json))),
    decoder: Option(Decoder(t)),
  )
}

Constructors

  • Request(
      http_request: request.Request(String),
      query: Option(String),
      variables: Option(List(#(String, Json))),
      decoder: Option(Decoder(t)),
    )

Functions

pub fn new() -> Request(a)

Construct a GleamQL Request

Use with set functions to customise.

pub fn send(req: Request(a), send: fn(
    gleam/http/request.Request(String),
  ) -> Result(Response(String), b)) -> Result(
  Option(a),
  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(a), decoder: fn(Dynamic) ->
    Result(a, List(DecodeError))) -> Request(a)

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: fn(country) {
      country
      |> dynamic.decode1(Country, field("name", of: string))
    },
  ),
))
pub fn set_header(req: Request(a), key: String, value: String) -> Request(
  a,
)

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(a), host: String) -> Request(a)

Set the host of the request.

gleamql.set_host("countries.trevorblades.com")
pub fn set_path(req: Request(a), path: String) -> Request(a)

Set the path of the request.

gleamql.set_path("/graphql")
pub fn set_query(req: Request(a), query: String) -> Request(a)

Set the query of the request

pub fn set_variable(req: Request(a), key: String, value: Json) -> Request(
  a,
)

Set a variable that is needed in the request query

gleamql.set_variable("code", json.string("GB"))
Search Document