This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

Graphqelm.Internal.Builder.Object

WARNING Graphqelm.Interal modules are used by the graphqelm command line code generator tool. They should not be consumed through hand-written code.

Internal functions for use by auto-generated code from the graphqelm CLI.

fieldDecoder : String -> List Argument -> Decoder decodesTo -> Field decodesTo lockedTo

Refer to a field in auto-generated code.

selection : (a -> constructor) -> SelectionSet (a -> constructor) typeLock

Used to create the selection functions in auto-generated code.

selectionField : String -> List Argument -> SelectionSet a objectTypeLock -> (Decoder a -> Decoder b) -> Field b lockedTo

Refer to an object in auto-generated code.

interfaceSelection : List (FragmentSelectionSet typeSpecific typeLock) -> (Maybe typeSpecific -> a -> b) -> SelectionSet (a -> b) typeLock

Used to create the selection functions in auto-generated code for interfaces.

unionSelection : List (FragmentSelectionSet typeSpecific typeLock) -> (Maybe typeSpecific -> a) -> SelectionSet a typeLock

Used to create the selection functions in auto-generated code for unions.

module Graphqelm.Internal.Builder.Object exposing (fieldDecoder, interfaceSelection, selection, selectionField, unionSelection)

{-| **WARNING** `Graphqelm.Interal` modules are used by the `graphqelm` command line
code generator tool. They should not be consumed through hand-written code.

Internal functions for use by auto-generated code from the `graphqelm` CLI.
@docs fieldDecoder, selection, selectionField, interfaceSelection, unionSelection

-}

import Dict
import Graphqelm.Field as Field exposing (Field(Field))
import Graphqelm.Internal.Builder.Argument exposing (Argument)
import Graphqelm.RawField exposing (RawField)
import Graphqelm.SelectionSet exposing (FragmentSelectionSet(FragmentSelectionSet), SelectionSet(..))
import Json.Decode as Decode exposing (Decoder)


{-| Refer to a field in auto-generated code.
-}
fieldDecoder : String -> List Argument -> Decoder decodesTo -> Field decodesTo lockedTo
fieldDecoder fieldName args decoder =
    Field (leaf fieldName args) decoder


{-| Refer to an object in auto-generated code.
-}
selectionField :
    String
    -> List Argument
    -> SelectionSet a objectTypeLock
    -> (Decoder a -> Decoder b)
    -> Field b lockedTo
selectionField fieldName args (SelectionSet fields decoder) decoderTransform =
    Field (composite fieldName args fields) (decoderTransform decoder)


composite : String -> List Argument -> List RawField -> RawField
composite fieldName args fields =
    Graphqelm.RawField.Composite fieldName args fields


leaf : String -> List Argument -> RawField
leaf fieldName args =
    Graphqelm.RawField.Leaf fieldName args


{-| Used to create the `selection` functions in auto-generated code.
-}
selection : (a -> constructor) -> SelectionSet (a -> constructor) typeLock
selection constructor =
    SelectionSet [] (Decode.succeed constructor)


{-| Used to create the `selection` functions in auto-generated code for interfaces.
-}
interfaceSelection : List (FragmentSelectionSet typeSpecific typeLock) -> (Maybe typeSpecific -> a -> b) -> SelectionSet (a -> b) typeLock
interfaceSelection typeSpecificSelections constructor =
    let
        typeNameDecoder =
            \typeName ->
                typeSpecificSelections
                    |> List.map (\(FragmentSelectionSet thisTypeName fields decoder) -> ( thisTypeName, decoder ))
                    |> Dict.fromList
                    |> Dict.get typeName
                    |> Maybe.map (Decode.map Just)
                    |> Maybe.withDefault (Decode.succeed Nothing)

        selections =
            typeSpecificSelections
                |> List.map (\(FragmentSelectionSet typeName fields decoder) -> composite ("...on " ++ typeName) [] fields)
    in
    SelectionSet (leaf "__typename" [] :: selections)
        (Decode.map2 (|>)
            (Decode.string
                |> Decode.field "__typename"
                |> Decode.andThen typeNameDecoder
            )
            (Decode.succeed constructor)
        )


{-| Used to create the `selection` functions in auto-generated code for unions.
-}
unionSelection : List (FragmentSelectionSet typeSpecific typeLock) -> (Maybe typeSpecific -> a) -> SelectionSet a typeLock
unionSelection typeSpecificSelections constructor =
    let
        typeNameDecoder =
            \typeName ->
                typeSpecificSelections
                    |> List.map (\(FragmentSelectionSet thisTypeName fields decoder) -> ( thisTypeName, decoder ))
                    |> Dict.fromList
                    |> Dict.get typeName
                    |> Maybe.map (Decode.map Just)
                    |> Maybe.withDefault (Decode.succeed Nothing)

        selections =
            typeSpecificSelections
                |> List.map (\(FragmentSelectionSet typeName fields decoder) -> composite ("...on " ++ typeName) [] fields)
    in
    SelectionSet (leaf "__typename" [] :: selections)
        (Decode.map2 (|>)
            (Decode.string
                |> Decode.field "__typename"
                |> Decode.andThen typeNameDecoder
            )
            (Decode.succeed constructor)
        )