11module Graphql.Document.Field exposing (hashedAliasName , serializeChildren )
22
3- import Dict exposing (Dict )
43import Graphql.Document.Argument as Argument
54import Graphql.Document.Hash exposing (hashString )
65import Graphql.Document.Indent as Indent
7- import Graphql.Internal.Builder.Argument exposing (Argument )
86import Graphql.RawField exposing (RawField (..) )
7+ import OrderedDict as Dict
8+
9+
10+ type alias Dict comparable v =
11+ Dict . OrderedDict comparable v
912
1013
1114hashedAliasName : RawField -> String
@@ -126,29 +129,11 @@ serializeChildren indentationLevel children =
126129
127130mergedFields : List RawField -> List RawField
128131mergedFields children =
129- let
130- mergeThing : MergedFields
131- mergeThing =
132- mergeFields children
133- in
134- ( mergeThing. leaves |> Dict . values |> List . map leafToField)
135- ++ ( mergeThing. composites |> Dict . values |> List . map compositeToField)
132+ Dict . values ( mergeFields children)
136133
137134
138135type alias MergedFields =
139- { leaves : Dict String ( { typeString : String , fieldName : String }, List Argument )
140- , composites : Dict String ( { name : String , args : List Argument }, List RawField )
141- }
142-
143-
144- leafToField : ( { typeString : String , fieldName : String }, List Argument ) -> RawField
145- leafToField ( record, arguments ) =
146- Leaf record arguments
147-
148-
149- compositeToField : ( { name : String , args : List Argument }, List RawField ) -> RawField
150- compositeToField ( record, children ) =
151- Composite record. name record. args children
136+ Dict String RawField
152137
153138
154139{- | Fields will have collisions if there is more than one with the same field name or field alias.
@@ -160,36 +145,33 @@ mergeFields : List RawField -> MergedFields
160145mergeFields rawFields =
161146 rawFields
162147 |> List . foldl
163- ( \ field { leaves , composites } ->
148+ ( \ field mergedSoFar ->
164149 case field of
165- Composite fieldName args children ->
166- { leaves = leaves
167- , composites =
168- composites
169- |> Dict . update ( hashedAliasName field)
170- ( \ maybeChildrenSoFar ->
171- maybeChildrenSoFar
172- |> Maybe . withDefault ( { name = fieldName, args = args }, [] )
173- |> Tuple . mapSecond ( (++) children)
174- |> Just
175- )
176- }
177-
178- Leaf info args ->
179- { leaves =
180- leaves
181- |> Dict . update ( hashedAliasName field)
182- ( \ maybeChildrenSoFar ->
183- maybeChildrenSoFar
184- |> Maybe . withDefault ( info, args )
185- |> Just
186- )
187- , composites = composites
188- }
150+ Composite _ _ newChildren ->
151+ mergedSoFar
152+ |> Dict . update ( hashedAliasName field)
153+ ( \ maybeChildrenSoFar ->
154+ case maybeChildrenSoFar of
155+ Nothing ->
156+ Just field
157+
158+ Just ( Composite existingFieldName existingArgs existingChildren) ->
159+ Composite existingFieldName existingArgs ( existingChildren ++ newChildren) |> Just
160+
161+ _ ->
162+ Just field
163+ )
164+
165+ Leaf _ _ ->
166+ mergedSoFar
167+ |> Dict . update ( hashedAliasName field)
168+ ( \ maybeChildrenSoFar ->
169+ maybeChildrenSoFar
170+ |> Maybe . withDefault field
171+ |> Just
172+ )
189173 )
190- { leaves = Dict . empty
191- , composites = Dict . empty
192- }
174+ Dict . empty
193175
194176
195177nonemptyChildren : List RawField -> List RawField
0 commit comments