elm-graphql:从连接查询中获取所有对象的列表
elm-graphq: Get list of all objects from connection query
我在深水中。
我正在尝试通过 GraphQL api.
从 PostgreSQL 数据库中获取所有行
我正在使用 dillonkearns/elm-graphql
elm 包。
这是我的查询(上下文图片):
query getAllTheDATA {
allEvents1S {
nodes {
name
stopTime
stopDate
startTime
startDate
responsible
}
}
}
当前代码:
这段代码只是为了使用选择器进行查询。
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
问题:
在使用 elm-graphql 包的 elm 中,我如何收集这些数据并将其存储在列表中?
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
我在深水中。
我正在尝试通过 GraphQL api.
从 PostgreSQL 数据库中获取所有行
我正在使用 dillonkearns/elm-graphql
elm 包。
这是我的查询(上下文图片):
query getAllTheDATA {
allEvents1S {
nodes {
name
stopTime
stopDate
startTime
startDate
responsible
}
}
}
当前代码:
这段代码只是为了使用选择器进行查询。
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
问题:
在使用 elm-graphql 包的 elm 中,我如何收集这些数据并将其存储在列表中?
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