Skip to content

Commit

Permalink
Use hash aliases based on a field's type and args (not just args).
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Dec 7, 2018
1 parent 293f844 commit 9ae8fc5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 55 deletions.
11 changes: 1 addition & 10 deletions generator/src/Graphql/Generator/Field.elm
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,7 @@ initScalarField : List String -> TypeReference -> FieldGenerator
initScalarField apiSubmodule typeRef =
let
scalarName =
case typeRef of
Type.TypeReference referrableType isNullable ->
case referrableType of
Type.Scalar scalarType ->
"(Just \""
++ Scalar.toString scalarType
++ "\")"

_ ->
"Nothing"
"\"" ++ Graphql.Generator.Decoder.generateType [] typeRef ++ "\""
in
{ annotatedArgs = []
, fieldArgs = []
Expand Down
48 changes: 14 additions & 34 deletions src/Graphql/Document/Field.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,32 @@ hashedAliasName field =

maybeAliasHash : RawField -> Maybe String
maybeAliasHash field =
case field of
(case field of
Composite name arguments children ->
if List.isEmpty arguments then
Nothing

else
arguments
|> Argument.serialize
|> Murmur3.hashString 0
|> String.fromInt
|> Just

Leaf maybeScalarName name arguments ->
if List.isEmpty arguments then
Nothing

else
arguments
|> Argument.serialize
|> Murmur3.hashString 0
|> String.fromInt
|> Just
Leaf { typeString, fieldName } arguments ->
arguments
|> Argument.serialize
|> List.singleton
|> List.append [ typeString ]
|> String.concat
|> Just
)
|> Maybe.map (Murmur3.hashString 0 >> String.fromInt)


alias : RawField -> Maybe String
alias field =
let
maybeScalarAlias =
case field of
Leaf scalarName _ _ ->
scalarName

_ ->
Nothing

prefixValues =
[ maybeAliasHash field, maybeScalarAlias ]
|> List.filterMap identity
in
if prefixValues == [] then
Nothing

else
(Graphql.RawField.name field :: prefixValues)
|> String.concat
|> Just
field
|> maybeAliasHash
|> Maybe.map (\aliasHash -> Graphql.RawField.name field ++ aliasHash)


serialize : Maybe String -> Maybe Int -> RawField -> Maybe String
Expand Down Expand Up @@ -105,7 +85,7 @@ serialize aliasName mIndentationLevel field =
++ "}"
|> Just

Leaf maybeScalarName fieldName args ->
Leaf { fieldName } args ->
Just (fieldName ++ Argument.serialize args)
)
|> Maybe.map
Expand Down
20 changes: 12 additions & 8 deletions src/Graphql/Internal/Builder/Object.elm
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ scalarDecoder =

{-| Refer to a field in auto-generated code.
-}
selectionForField : Maybe String -> String -> List Argument -> Decoder decodesTo -> SelectionSet decodesTo lockedTo
selectionForField scalarName fieldName args decoder =
SelectionSet [ leaf scalarName fieldName args ]
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 (leaf scalarName fieldName args))
(Graphql.Document.Field.hashedAliasName newLeaf)
decoder
)

Expand All @@ -71,9 +75,9 @@ composite fieldName args fields =
Graphql.RawField.Composite fieldName args fields


leaf : Maybe String -> String -> List Argument -> RawField
leaf sclarName fieldName args =
Graphql.RawField.Leaf sclarName fieldName args
leaf : { typeString : String, fieldName : String } -> List Argument -> RawField
leaf details args =
Graphql.RawField.Leaf details args


{-| Used to create FragmentSelectionSets for type-specific fragmentsin auto-generated code.
Expand All @@ -94,7 +98,7 @@ exhuastiveFragmentSelection typeSpecificSelections =
in
SelectionSet (Graphql.RawField.typename :: selections)
(Decode.string
|> Decode.field "__typename"
|> Decode.field (Graphql.Document.Field.hashedAliasName Graphql.RawField.typename)
|> Decode.andThen
(\typeName ->
typeSpecificSelections
Expand Down
6 changes: 3 additions & 3 deletions src/Graphql/RawField.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Graphql.Internal.Builder.Argument as Argument exposing (Argument)

type RawField
= Composite String (List Argument) (List RawField)
| Leaf (Maybe String) String (List Argument)
| Leaf { typeString : String, fieldName : String } (List Argument)


name : RawField -> String
Expand All @@ -14,10 +14,10 @@ name field =
Composite fieldName argumentList fieldList ->
fieldName

Leaf scalarName fieldName argumentList ->
Leaf { typeString, fieldName } argumentList ->
fieldName


typename : RawField
typename =
Leaf Nothing "__typename" []
Leaf { typeString = "", fieldName = "__typename" } []

0 comments on commit 9ae8fc5

Please sign in to comment.