/gleamql

A Simple Graphql Client Written In Gleam ✨

Primary LanguageGleam

gleamql

Package Version Hex Docs

A Simple Graphql Client Written In Gleam ✨

Usage

import gleamql
import gleam/json.{string}
import gleam/dynamic.{field}
import gleam/option.{Some}
import gleam/hackney

pub type Data {
  Data(country: Country)
}

pub type Country {
  Country(name: String)
}

const country_query = "query CountryQuery($code: ID!) {
  country(code: $code) {
    name
  }
}"

pub fn main() {
  let assert Ok(Some(Data(country: Country(name: "United Kingdom")))) =
    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)
}

Installation

If available on Hex this package can be added to your Gleam project:

gleam add gleamql

and its documentation can be found at https://hexdocs.pm/gleamql.