Skip to content

Commit e351b0d

Browse files
authored
Merge pull request #596 from hariroshan/better-serialize
Better serialization on graphql queries and refactoring
2 parents f0d0662 + 182c125 commit e351b0d

File tree

8 files changed

+259
-55
lines changed

8 files changed

+259
-55
lines changed

elm.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"elm/http": "2.0.0 <= v < 3.0.0",
2323
"elm/json": "1.1.1 <= v < 2.0.0",
2424
"elm/url": "1.0.0 <= v < 2.0.0",
25-
"elm-community/list-extra": "8.0.0 <= v < 9.0.0",
2625
"j-maas/elm-ordered-containers": "1.0.0 <= v < 2.0.0",
2726
"lukewestby/elm-string-interpolate": "1.0.4 <= v < 2.0.0"
2827
},

src/Graphql/Document/Field.elm

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Graphql.Document.Field exposing (hashedAliasName, serializeChildren)
33
import Graphql.Document.Argument as Argument
44
import Graphql.Document.Hash exposing (hashString)
55
import Graphql.Document.Indent as Indent
6-
import Graphql.RawField exposing (RawField(..))
6+
import Graphql.RawField exposing (RawField(..), name)
77
import OrderedDict as Dict
88

99

@@ -15,7 +15,7 @@ hashedAliasName : RawField -> String
1515
hashedAliasName field =
1616
field
1717
|> alias
18-
|> Maybe.withDefault (Graphql.RawField.name field)
18+
|> Maybe.withDefault (name field)
1919

2020

2121
maybeAliasHash : RawField -> Maybe Int
@@ -52,7 +52,7 @@ alias : RawField -> Maybe String
5252
alias field =
5353
field
5454
|> maybeAliasHash
55-
|> Maybe.map (\aliasHash -> Graphql.RawField.name field ++ String.fromInt aliasHash)
55+
|> Maybe.map (\aliasHash -> name field ++ String.fromInt aliasHash)
5656

5757

5858
serialize : Maybe String -> Maybe Int -> RawField -> Maybe String
@@ -112,9 +112,10 @@ serializeChildren indentationLevel children =
112112
children
113113
|> mergedFields
114114
|> nonemptyChildren
115+
|> canAllowHashing
115116
|> List.map
116-
(\field ->
117-
serialize (alias field) (indentationLevel |> Maybe.map ((+) 1)) field
117+
(\( field, maybeAlias ) ->
118+
serialize maybeAlias (indentationLevel |> Maybe.map ((+) 1)) field
118119
)
119120
|> List.filterMap identity
120121
|> String.join
@@ -127,6 +128,42 @@ serializeChildren indentationLevel children =
127128
)
128129

129130

131+
canAllowHashing : List RawField -> List ( RawField, Maybe String )
132+
canAllowHashing rawFields =
133+
let
134+
fieldCounts =
135+
rawFields
136+
|> List.map name
137+
|> List.foldl
138+
(\fld acc ->
139+
acc
140+
|> Dict.update fld
141+
(\val ->
142+
Just
143+
(case val of
144+
Nothing ->
145+
0
146+
147+
Just count ->
148+
count + 1
149+
)
150+
)
151+
)
152+
Dict.empty
153+
in
154+
rawFields
155+
|> List.map
156+
(\field ->
157+
( field
158+
, if (fieldCounts |> Dict.get (name field) |> Maybe.withDefault 0) == 0 then
159+
Nothing
160+
161+
else
162+
alias field
163+
)
164+
)
165+
166+
130167
mergedFields : List RawField -> List RawField
131168
mergedFields children =
132169
Dict.values (mergeFields children)

src/Graphql/Http/QueryHelper.elm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Graphql.Http.QueryHelper exposing (HttpMethod(..), build)
22

33
import Graphql.Document as Document
44
import Graphql.Http.QueryParams as QueryParams
5-
import Graphql.Operation exposing (RootMutation, RootQuery)
5+
import Graphql.Operation exposing (RootQuery)
66
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
77
import Http
88
import Json.Encode
@@ -44,7 +44,7 @@ build forceMethod url queryParams maybeOperationName queryDocument =
4444

4545
urlForGetRequest =
4646
QueryParams.urlWithQueryParams
47-
(queryParams ++ [ ( "query", serializedQueryForGetRequest ) ] ++ operationNameParamForGetRequest)
47+
(queryParams ++ ( "query", serializedQueryForGetRequest ) :: operationNameParamForGetRequest)
4848
url
4949
in
5050
if forceMethod == Just Post || (String.length urlForGetRequest >= maxLength && forceMethod /= Just Get) then
@@ -64,8 +64,8 @@ build forceMethod url queryParams maybeOperationName queryDocument =
6464
, body =
6565
Http.jsonBody <|
6666
Json.Encode.object <|
67-
[ ( "query", Json.Encode.string serializedQuery ) ]
68-
++ operationNameParamForPostRequest
67+
( "query", Json.Encode.string serializedQuery )
68+
:: operationNameParamForPostRequest
6969
}
7070

7171
else

src/Graphql/Internal/Builder/Object.elm

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ scalarDecoder =
2929
, Decode.bool
3030
|> Decode.map
3131
(\bool ->
32-
case bool of
33-
True ->
34-
"true"
32+
if bool then
33+
"true"
3534

36-
False ->
37-
"false"
35+
else
36+
"false"
3837
)
3938
]
4039

@@ -48,9 +47,10 @@ selectionForField typeString fieldName args decoder =
4847
leaf { typeString = typeString, fieldName = fieldName } args
4948
in
5049
SelectionSet [ newLeaf ]
51-
(Decode.field
52-
(Graphql.Document.Field.hashedAliasName newLeaf)
53-
decoder
50+
(Decode.oneOf
51+
[ Decode.field fieldName decoder
52+
, Decode.field (Graphql.Document.Field.hashedAliasName newLeaf) decoder
53+
]
5454
)
5555

5656

@@ -64,9 +64,12 @@ selectionForCompositeField :
6464
-> SelectionSet b lockedTo
6565
selectionForCompositeField fieldName args (SelectionSet fields decoder) decoderTransform =
6666
SelectionSet [ composite fieldName args fields ]
67-
(Decode.field
68-
(Graphql.Document.Field.hashedAliasName (composite fieldName args fields))
69-
(decoderTransform decoder)
67+
(Decode.oneOf
68+
[ Decode.field fieldName (decoderTransform decoder)
69+
, Decode.field
70+
(Graphql.Document.Field.hashedAliasName (composite fieldName args fields))
71+
(decoderTransform decoder)
72+
]
7073
)
7174

7275

src/Graphql/Internal/Encode.elm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ maybeObject maybeValues =
5151
maybeValues
5252
|> List.filterMap
5353
(\( key, value ) ->
54-
case value of
55-
Just actualValue ->
56-
Just ( key, actualValue )
57-
58-
Nothing ->
59-
Nothing
54+
value
55+
|> Maybe.andThen
56+
(\actualValue ->
57+
Just ( key, actualValue )
58+
)
6059
)
6160
|> Object
6261

src/Graphql/SelectionSet.elm

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,8 @@ take a look at the
277277
-}
278278

279279
import Dict exposing (Dict)
280-
import Graphql.Document.Field
281280
import Graphql.RawField as RawField exposing (RawField)
282281
import Json.Decode as Decode exposing (Decoder)
283-
import List.Extra
284282

285283

286284
{-| SelectionSet type
@@ -829,11 +827,7 @@ combineMaybeList : List (Maybe a) -> Maybe (List a)
829827
combineMaybeList listOfMaybes =
830828
let
831829
step maybeElement accumulator =
832-
case maybeElement of
833-
Nothing ->
834-
Nothing
835-
836-
Just element ->
837-
Maybe.map ((::) element) accumulator
830+
maybeElement
831+
|> Maybe.andThen (\element -> Maybe.map ((::) element) accumulator)
838832
in
839833
List.foldr step (Just []) listOfMaybes

0 commit comments

Comments
 (0)