-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathObject.elm
117 lines (95 loc) · 4.54 KB
/
Object.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
module Graphql.Internal.Builder.Object exposing (scalarDecoder, exhaustiveFragmentSelection, buildFragment, selectionForField, selectionForCompositeField)
{-| **WARNING** `Graphql.Internal` modules are used by the `@dillonkearns/elm-graphql` command line
code generator tool. They should not be consumed through hand-written code.
Internal functions for use by auto-generated code from the `@dillonkearns/elm-graphql` CLI.
@docs scalarDecoder, exhaustiveFragmentSelection, buildFragment, selectionForField, selectionForCompositeField
-}
import Dict
import Graphql.Document.Field
import Graphql.Internal.Builder.Argument exposing (Argument)
import Graphql.RawField exposing (RawField)
import Graphql.SelectionSet exposing (FragmentSelectionSet(..), SelectionSet(..))
import Json.Decode as Decode exposing (Decoder)
import String.Interpolate exposing (interpolate)
{-| Decoder for scalars for use in auto-generated code.
-}
scalarDecoder : Decoder String
scalarDecoder =
Decode.oneOf
[ Decode.string
, Decode.float |> Decode.map String.fromFloat
, Decode.int |> Decode.map String.fromInt
, Decode.bool
|> Decode.map
(\bool ->
case bool of
True ->
"true"
False ->
"false"
)
]
{-| Refer to a field in auto-generated code.
-}
selectionForField : String -> String -> List Argument -> Decoder decodesTo -> SelectionSet decodesTo lockedTo
selectionForField typeString fieldName args decoder =
let
newLeaf =
leaf { typeString = typeString, fieldName = fieldName } args
in
SelectionSet [ newLeaf ]
(Decode.field
(Graphql.Document.Field.hashedAliasName newLeaf)
decoder
)
{-| Refer to an object in auto-generated code.
-}
selectionForCompositeField :
String
-> List Argument
-> SelectionSet a objectScope
-> (Decoder a -> Decoder b)
-> SelectionSet b lockedTo
selectionForCompositeField fieldName args (SelectionSet fields decoder) decoderTransform =
SelectionSet [ composite fieldName args fields ]
(Decode.field
(Graphql.Document.Field.hashedAliasName (composite fieldName args fields))
(decoderTransform decoder)
)
composite : String -> List Argument -> List RawField -> RawField
composite fieldName args fields =
Graphql.RawField.Composite fieldName args fields
leaf : { typeString : String, fieldName : String } -> List Argument -> RawField
leaf details args =
Graphql.RawField.Leaf details args
{-| Used to create FragmentSelectionSets for type-specific fragments in auto-generated code.
-}
buildFragment : String -> SelectionSet decodesTo selectionLock -> FragmentSelectionSet decodesTo fragmentLock
buildFragment fragmentTypeName (SelectionSet fields decoder) =
FragmentSelectionSet fragmentTypeName fields decoder
{-| Used to create the `selection` functions in auto-generated code for exhaustive fragments.
-}
exhaustiveFragmentSelection : List (FragmentSelectionSet decodesTo scope) -> SelectionSet decodesTo scope
exhaustiveFragmentSelection typeSpecificSelections =
let
selections =
typeSpecificSelections
|> List.map (\(FragmentSelectionSet typeName fields decoder) -> composite ("...on " ++ typeName) [] fields)
in
SelectionSet (Graphql.RawField.typename :: selections)
(Decode.string
|> Decode.field (Graphql.Document.Field.hashedAliasName Graphql.RawField.typename)
|> Decode.andThen
(\typeName ->
typeSpecificSelections
|> List.map (\(FragmentSelectionSet thisTypeName fields decoder) -> ( thisTypeName, decoder ))
|> Dict.fromList
|> Dict.get typeName
|> Maybe.withDefault (exhaustiveFailureMessage typeSpecificSelections typeName |> Decode.fail)
)
)
exhaustiveFailureMessage : List (FragmentSelectionSet decodesTo scope) -> String -> String
exhaustiveFailureMessage typeSpecificSelections typeName =
interpolate
"Unhandled type `{0}` in exhaustive fragment handling. The following types had handlers registered to handle them: [{1}]. This happens if you are parsing either a Union or Interface. Do you need to rerun the `@dillonkearns/elm-graphql` command line tool?"
[ typeName, typeSpecificSelections |> List.map (\(FragmentSelectionSet fragmentType fields decoder) -> fragmentType) |> String.join ", " ]