11module Graphql.Document.Field exposing (hashedAliasName , serializeChildren )
22
3- import OrderedDict as 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
10- type alias Dict comparable v = Dict . OrderedDict comparable v
1113
1214hashedAliasName : RawField -> String
1315hashedAliasName field =
@@ -127,29 +129,11 @@ serializeChildren indentationLevel children =
127129
128130mergedFields : List RawField -> List RawField
129131mergedFields children =
130- let
131- mergeThing : MergedFields
132- mergeThing =
133- mergeFields children
134- in
135- ( mergeThing. leaves |> Dict . values |> List . map leafToField)
136- ++ ( mergeThing. composites |> Dict . values |> List . map compositeToField)
132+ Dict . values ( mergeFields children)
137133
138134
139135type alias MergedFields =
140- { leaves : Dict String ( { typeString : String , fieldName : String }, List Argument )
141- , composites : Dict String ( { name : String , args : List Argument }, List RawField )
142- }
143-
144-
145- leafToField : ( { typeString : String , fieldName : String }, List Argument ) -> RawField
146- leafToField ( record, arguments ) =
147- Leaf record arguments
148-
149-
150- compositeToField : ( { name : String , args : List Argument }, List RawField ) -> RawField
151- compositeToField ( record, children ) =
152- Composite record. name record. args children
136+ Dict String RawField
153137
154138
155139{- | Fields will have collisions if there is more than one with the same field name or field alias.
@@ -161,36 +145,33 @@ mergeFields : List RawField -> MergedFields
161145mergeFields rawFields =
162146 rawFields
163147 |> List . foldl
164- ( \ field { leaves , composites } ->
148+ ( \ field mergedSoFar ->
165149 case field of
166- Composite fieldName args children ->
167- { leaves = leaves
168- , composites =
169- composites
170- |> Dict . update ( hashedAliasName field)
171- ( \ maybeChildrenSoFar ->
172- maybeChildrenSoFar
173- |> Maybe . withDefault ( { name = fieldName, args = args }, [] )
174- |> Tuple . mapSecond ( (++) children)
175- |> Just
176- )
177- }
178-
179- Leaf info args ->
180- { leaves =
181- leaves
182- |> Dict . update ( hashedAliasName field)
183- ( \ maybeChildrenSoFar ->
184- maybeChildrenSoFar
185- |> Maybe . withDefault ( info, args )
186- |> Just
187- )
188- , composites = composites
189- }
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+ )
190173 )
191- { leaves = Dict . empty
192- , composites = Dict . empty
193- }
174+ Dict . empty
194175
195176
196177nonemptyChildren : List RawField -> List RawField
0 commit comments