ryannhg/graphql/1.0.0

NOTE: the latest version is 2.1.0

Renamed to ryan-haskell/graphql. It is published under that name, and new versions come out there.

Versions published as ryannhg/graphql still install under that name. 4 versions of 4 come from the new location (see their notes).

Rerouted (renamed, needs approval). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/ryan-haskell/graphql.

What we checked: The repo moved to github.com/ryan-haskell/graphql and tag 1.0.0 is there; its commit 8fb41eb48e93 is from 2024-03-12 23:41 UTC, after the version was published (2022-12-26 04:35 UTC); elm.json differs from package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one and nothing else confirms its contents.

We could not verify this copy against what was published, so the Lamdera compiler asks before using it (y/n). Without a terminal, as in CI, it stops with an error unless approved:

  • lamdera make --auto-approve-package-rerouting approves rerouted versions for that run,
  • LAMDERA_AUTO_APPROVE_PACKAGE_REROUTING=1 does the same for every run in that environment,
  • LAMDERA_CACHE=off skips the Lamdera package server altogether: packages come from package.elm-lang.org and GitHub only, as with elm, and a rerouted version then fails to download.

Details · package.elm-lang.org's zip: https://github.com/ryannhg/graphql/zipball/1.0.0/

GraphQL.Encode

type Value

The Value type represents a JSON value that will be sent to your GraphQL API. When you use this with the GraphQL.Http module, this is an example of what will be sent with your HTTP request:

{
    "variables": {
        "id": "123",
        "form": {
            "email": "[email protected]",
            "password": "secret123"
        }
    }
}

Scalars

string : String -> Value

Create a GraphQL input from a String value:

import GraphQL.Encode


name : GraphQL.Encode.Value
name =
    GraphQL.Encode.string "Ryan"
float : Float -> Value

Create a GraphQL input from a Float value:

import GraphQL.Encode


score : GraphQL.Encode.Value
score =
    GraphQL.Encode.float 0.99
int : Int -> Value

Create a GraphQL input from a Int value:

import GraphQL.Encode


age : GraphQL.Encode.Value
age =
    GraphQL.Encode.int 62
bool : Bool -> Value

Create a GraphQL input from a Bool value:

import GraphQL.Encode


isReadingThis : GraphQL.Encode.Value
isReadingThis =
    GraphQL.Encode.bool True
id : Id -> Value

Create a GraphQL input from a Id value:

import GraphQL.Encode
import GraphQL.Scalar.Id exposing (Id)


userId : Id
userId =
    GraphQL.Scalar.Id.fromInt 1203


isReadingThis : GraphQL.Encode.Value
isReadingThis =
    GraphQL.Encode.id userId
{ toJson : scalar -> Value
, value : scalar
}
-> Value

Here's an example of using this function to work with a DateTime type. We recommend defining each custom scalar in a separate Elm module, like GraphQL.Scalar.DateTime:

-- module GraphQL.Scalar.DateTime exposing
--     ( DateTime
--     , decoder, encode
--     )




import GraphQL.Encode
import Iso8601
import Time


type alias DateTime =
    Time.Posix


encode : DateTime -> GraphQL.Encode.Value
encode dateTime =
    GraphQL.Encode.scalar
        { toJson = Iso8601.encode
        , value = dateTime
        }

The snippet above uses elm/time and rtfeldman/elm-iso8601-date-strings to convert our Time.Posix value into an ISO 8601 String value for our API.

import GraphQL.Scalar.DateTime


GraphQL.Scalar.DateTime.encode
    (Time.millisToPosix 1672019156520)
    == Json "2022-12-26T01:45:56.520Z"

Enums

{ toString : enum -> String
, value : enum
}
-> Value

Create a GraphQL input from an enum value. To avoid duplicating code, we recommend you create a separate Elm module per enum, like GraphQL.Enum.Episode.

In that module, you can define your encode function in one place:

-- module GraphQL.Enum.Episode exposing
--     ( Episode(..), list
--     , decoder, encode
--     )




import GraphQL.Encode


type Episode
    = NewHope
    | EmpireStrikesBack
    | ReturnOfTheJedi


encode : Episode -> GraphQL.Encode.Value
encode episode =
    GraphQL.Encode.enum
        { toString = toString
        , value = episode
        }


toString : Episode -> String
toString episode =
    case episode of
        NewHope ->
            "NEWHOPE"


        EmpireStrikesBack ->
            "EMPIRE"


        ReturnOfTheJedi ->
            "RETURN"

Input types

input : List ( String, Value ) -> Value

GraphQL allows the API to define input types. These allow you to group your inputs into an object, like this:

import GraphQL.Encode


reviewInputValue : GraphQL.Encode.Value
reviewInputValue =
    GraphQL.Encode.input
        [ ( "stars"
          , GraphQL.Encode.int 10
          )
        , ( "commentary"
          , GraphQL.Encode.int "Would eat again!"
          )
        ]

Send a null value to a GraphQL API. This is commonly used by mutations to mark a field as removed.

import GraphQL.Encode


profileInputValue : GraphQL.Encode.Value
profileInputValue =
    GraphQL.Encode.input
        [ ( "name"
          , GraphQL.Encode.string "Ryan"
          )
        , ( "bio"
          , GraphQL.Encode.null
          )
        ]

Internals

These functions are used internally by GraphQL.Http, and you won't need them in your projects

toJson : Value -> Value