Home > Enterprise >  elm-graphq: Get list of all objects from connection query
elm-graphq: Get list of all objects from connection query

Time:04-06

I am in deep water.
I am trying to get all rows from a PostgreSQL database through a GraphQL api.
I am using the enter image description here

Current code:

This code is just to make the query with the selectors.

module Test exposing (..)
import Json.Decode as JD exposing (Decoder)
    
import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet,with)
import Calendar.Object exposing (Events1(..))
import Calendar.Object.Events1 as Events1
import Calendar.Object.Events1SConnection as EConn
import Calendar.Query as Query
import Calendar.Scalar exposing (Id(..))
import Calendar.Interface
import Calendar.Interface
    
type alias Activity2 =
   { name : Maybe String          -- The name of each activity
   , start_date : Maybe String    -- The start date for an activity
   , start_time: Maybe String     -- The start time for an activity
   , stop_date : Maybe String     -- The end date for an activity
   , stop_time: Maybe String      -- The end time for an activity
   , responsible : Maybe String   -- who is responsible
   }
    
eventsListSelection : SelectionSet (List Activity2) Calendar.Object.Events1SConnection
eventsListSelection =
    EConn.nodes <----- this is where the problem lies
    
fetchEventsQuery : SelectionSet (Maybe (List Activity2)) RootQuery
fetchEventsQuery =
    Query.allEvents1S (List Activity2) eventsListSelection

Questions:

How would I, in elm using the elm-graphql package, collect this data and store it in a list?

CodePudding user response:

import Browser
import Calendar.Object.Events1 as Events1Object
import Calendar.Object.Events1SConnection as Events1SConnection
import Calendar.Query as Query
import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
import Html exposing (Html, div, text)



-- MAIN



main : Program () Model Msg
main =
    Browser.element
        { init = init
        , update = update
        , view = view
        , subscriptions = subscriptions
        }



-- MODEL


type alias Model =
    { events : Status (List Event) }
    

type Status a
    = Failure
    | Loading
    | Success a
    
    
type alias Event =
   { name : String
   , startDate : String
   , startTime: String
   , stopDate : String
   , stopTime: String
   , responsible : String
   }
   
   
   
-- INIT


init : () -> ( Model, Cmd Msg )
init _ =
    ( Model Loading
    , Graphql.Http.send GotEvents (Graphql.Http.queryRequest "https://graphql-calendar.example.com/" query)
    )



-- UPDATE


type Msg
    = GotEvents (Result (Error (List Event)) List Event)


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        GotEvents result ->
            case result of
                Err _ ->
                    ( { model | events = Failure }
                    , Cmd.none
                    )
                    
                Ok events ->
                    ( { model | events = Success events }
                    , Cmd.none
                    )



-- VIEW


view : Model -> Html Msg
view model =
    case model.events of
        Success events ->
            div []
                [ List.map viewEvent events ]
                
        Loading ->
            div []
                [ text "Loading" ]
                
        Failure ->
            div []
                [ text "Failure" ]


viewEvent : Event -> Html Msg
viewEvent event =
    div []
        [ text event.name ]

            
    
-- GRAPHQL


query : SelectionSet (List Event) RootQuery
query =
    Query.allEvents1s
        (Events1SConnection.nodes
            (SelectionSet.map6 Event
                Events1Object.name
                Events1Object.startDate
                Events1Object.startTime
                Events1Object.stopDate
                Events1Object.stopTime
                Events1Object.responsible
            )
        )
        |> SelectionSet.nonNullOrFail



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
  Sub.none
  • Related