Skip to content

Commit 5dd0e47

Browse files
committed
Merge composite and leaf fields in the same Dict so their order is preserved. Update ordering in test cases.
1 parent 2cec9e9 commit 5dd0e47

File tree

2 files changed

+36
-55
lines changed

2 files changed

+36
-55
lines changed

src/Graphql/Document/Field.elm

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module Graphql.Document.Field exposing (hashedAliasName, serializeChildren)
22

3-
import OrderedDict as Dict
43
import Graphql.Document.Argument as Argument
54
import Graphql.Document.Hash exposing (hashString)
65
import Graphql.Document.Indent as Indent
7-
import Graphql.Internal.Builder.Argument exposing (Argument)
86
import 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

1214
hashedAliasName : RawField -> String
1315
hashedAliasName field =
@@ -127,29 +129,11 @@ serializeChildren indentationLevel children =
127129

128130
mergedFields : List RawField -> List RawField
129131
mergedFields 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

139135
type 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
161145
mergeFields 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

196177
nonemptyChildren : List RawField -> List RawField

tests/DocumentTests.elm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ all =
178178
|> Expect.equal """query {
179179
me {
180180
firstName0: firstName
181-
lastName0: lastName
182181
middleName0: middleName
182+
lastName0: lastName
183183
}
184184
}"""
185185
, test "different arguments are not merged" <|
@@ -198,12 +198,12 @@ all =
198198
]
199199
|> Graphql.Document.serializeQuery
200200
|> Expect.equal """query {
201-
me1529416052: me(id: 456) {
202-
lastName0: lastName
203-
}
204201
me3003759287: me(id: 123) {
205202
firstName0: firstName
206203
}
204+
me1529416052: me(id: 456) {
205+
lastName0: lastName
206+
}
207207
}"""
208208
, test "identical leaves are de-duped" <|
209209
\() ->

0 commit comments

Comments
 (0)