diff --git a/.travis.yml b/.travis.yml index 58bec78..605fbe6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,17 +1,16 @@ sudo: false +dist: focal language: node_js node_js: node cache: directories: - - elm-stuff/build-artifacts - - elm-stuff/packages - sysconfcpus os: - linux -env: ELM_VERSION=0.19.0 +env: ELM_VERSION=0.19.1 before_install: - echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config @@ -19,7 +18,7 @@ before_install: install: - node --version - npm --version - - npm install -g elm@$ELM_VERSION elm-test@0.19.0-beta4 + - npm install elm@$ELM_VERSION elm-test@$ELM_VERSION elm-format@0.8.4 # Faster compile on Travis. - | if [ ! -d sysconfcpus/bin ]; @@ -32,4 +31,5 @@ install: fi script: - - $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 elm-test \ No newline at end of file + - $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 ./node_modules/.bin/elm-test --compiler $TRAVIS_BUILD_DIR/node_modules/.bin/elm + - $TRAVIS_BUILD_DIR/sysconfcpus/bin/sysconfcpus -n 2 ./node_modules/.bin/elm-format --validate . diff --git a/example/Main.elm b/example/Main.elm index f57f565..a1338a3 100644 --- a/example/Main.elm +++ b/example/Main.elm @@ -1,10 +1,10 @@ module Main exposing (..) -import Html exposing (Html, div, text) +import GraphQL.Client.Http as GraphQLClient import GraphQL.Request.Builder exposing (..) import GraphQL.Request.Builder.Arg as Arg import GraphQL.Request.Builder.Variable as Var -import GraphQL.Client.Http as GraphQLClient +import Html exposing (Html, div, text) import Task exposing (Task) @@ -20,6 +20,7 @@ type alias FilmSummary = {-| The definition of `starWarsRequest` builds up a query request value that will later be encoded into the following GraphQL query document: +```graphql fragment filmPlanetsFragment on Film { planetConnection(first: $pageSize) { edges { @@ -43,9 +44,11 @@ query ($filmID: ID!, $pageSize: Int = 3) { ...filmPlanetsFragment } } +``` This query is sent along with variable values extracted from the record passed to `request`, and the response is decoded into a `FilmSummary`. + -} starWarsRequest : Request Query FilmSummary starWarsRequest = @@ -66,24 +69,24 @@ starWarsRequest = ) ) in - extract - (field "film" - [ ( "filmID", Arg.variable filmID ) ] - (object FilmSummary - |> with (field "title" [] (nullable string)) - |> with - (field "characterConnection" - [ ( "first", Arg.variable pageSize ) ] - (connectionNodes (extract (field "name" [] (nullable string)))) - ) - |> with (fragmentSpread planetsFragment) - ) + extract + (field "film" + [ ( "filmID", Arg.variable filmID ) ] + (object FilmSummary + |> with (field "title" [] (nullable string)) + |> with + (field "characterConnection" + [ ( "first", Arg.variable pageSize ) ] + (connectionNodes (extract (field "name" [] (nullable string)))) + ) + |> with (fragmentSpread planetsFragment) ) - |> queryDocument - |> request - { filmID = "1" - , pageSize = Nothing - } + ) + |> queryDocument + |> request + { filmID = "1" + , pageSize = Nothing + } {-| A function that helps you extract node objects from paginated Relay connections. diff --git a/src/GraphQL/Client/Http.elm b/src/GraphQL/Client/Http.elm index d55f774..87e7141 100644 --- a/src/GraphQL/Client/Http.elm +++ b/src/GraphQL/Client/Http.elm @@ -1,20 +1,9 @@ -module GraphQL.Client.Http - exposing - ( RequestError - , DocumentLocation - , Error(..) - , RequestOptions - , sendQuery - , sendMutation - , customSendQuery - , customSendQueryRaw - , customSendMutation - , customSendMutationRaw - ) +module GraphQL.Client.Http exposing (Error(..), RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw) {-| The functions in this module let you perform HTTP requests to conventional GraphQL server endpoints. -@docs Error, RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw +@docs Error, RequestError, DocumentLocation, sendQuery, sendMutation, RequestOptions, customSendQuery, customSendMutation, customSendQueryRaw, customSendMutationRaw + -} import GraphQL.Client.Http.Util as Util @@ -126,17 +115,18 @@ Example of response decoding: , withCredentials = False } in - request - |> GraphQL.Client.Http.customSendQueryRaw options - |> Task.andThen - (\response -> - case Json.Decode.decodeString decoder response.body of - Err err -> - Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response - - Ok decodedValue -> - Task.succeed decodedValue - ) + request + |> GraphQL.Client.Http.customSendQueryRaw options + |> Task.andThen + (\response -> + case Json.Decode.decodeString decoder response.body of + Err err -> + Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response + + Ok decodedValue -> + Task.succeed decodedValue + ) + -} customSendQueryRaw : RequestOptions @@ -175,17 +165,17 @@ Example of response decoding: , withCredentials = False } in - mutationRequest - |> GraphQL.Client.Http.customSendMutationRaw options - |> Task.andThen - (\response -> - case Json.Decode.decodeString decoder response.body of - Err err -> - Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response - - Ok decodedValue -> - Task.succeed decodedValue - ) + mutationRequest + |> GraphQL.Client.Http.customSendMutationRaw options + |> Task.andThen + (\response -> + case Json.Decode.decodeString decoder response.body of + Err err -> + Task.fail <| GraphQL.Client.Http.HttpError <| Http.BadPayload err response + + Ok decodedValue -> + Task.succeed decodedValue + ) -} customSendMutationRaw : @@ -210,7 +200,7 @@ send options request = expect = Util.defaultExpect (Builder.responseDataDecoder request) in - sendExpecting expect options request + sendExpecting expect options request sendExpecting : @@ -226,7 +216,7 @@ sendExpecting expect requestOptions request = variableValues = Builder.jsonVariableValues request in - Util.requestConfig requestOptions documentString expect variableValues - |> Http.request - |> Http.toTask - |> Task.mapError (Util.convertHttpError HttpError GraphQLError) + Util.requestConfig requestOptions documentString expect variableValues + |> Http.request + |> Http.toTask + |> Task.mapError (Util.convertHttpError HttpError GraphQLError) diff --git a/src/GraphQL/Client/Http/Util.elm b/src/GraphQL/Client/Http/Util.elm index 56c6661..ce81a19 100644 --- a/src/GraphQL/Client/Http/Util.elm +++ b/src/GraphQL/Client/Http/Util.elm @@ -18,7 +18,7 @@ postBodyJson documentString variableValues = |> Maybe.map (\obj -> [ ( "variables", obj ) ]) |> Maybe.withDefault [] in - Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams) + Json.Encode.object ([ ( "query", documentValue ) ] ++ extraParams) postBody : String -> Maybe Json.Encode.Value -> Http.Body @@ -32,6 +32,7 @@ parameterizedUrl url documentString variableValues = firstParamPrefix = if String.contains "?" url then "&" + else "?" @@ -46,7 +47,7 @@ parameterizedUrl url documentString variableValues = ) |> Maybe.withDefault "" in - url ++ queryParam ++ variablesParam + url ++ queryParam ++ variablesParam type alias RequestOptions = @@ -107,17 +108,18 @@ requestConfig requestOptions documentString expect variableValues = ( url, body ) = if requestOptions.method == "GET" then ( parameterizedUrl requestOptions.url documentString variableValues, Http.emptyBody ) + else ( requestOptions.url, postBody documentString variableValues ) in - { method = requestOptions.method - , headers = requestOptions.headers - , url = url - , body = body - , expect = expect - , timeout = requestOptions.timeout - , withCredentials = requestOptions.withCredentials - } + { method = requestOptions.method + , headers = requestOptions.headers + , url = url + , body = body + , expect = expect + , timeout = requestOptions.timeout + , withCredentials = requestOptions.withCredentials + } defaultExpect : Json.Decode.Decoder result -> Http.Expect result @@ -139,12 +141,12 @@ convertHttpError wrapHttpError wrapGraphQLError httpError = |> Result.map wrapGraphQLError |> Result.withDefault (wrapHttpError httpError) in - case httpError of - Http.BadStatus { body } -> - handleErrorWithResponseBody body + case httpError of + Http.BadStatus { body } -> + handleErrorWithResponseBody body - Http.BadPayload _ { body } -> - handleErrorWithResponseBody body + Http.BadPayload _ { body } -> + handleErrorWithResponseBody body - _ -> - wrapHttpError httpError + _ -> + wrapHttpError httpError diff --git a/src/GraphQL/Request.elm b/src/GraphQL/Request.elm index b42418b..21a6526 100644 --- a/src/GraphQL/Request.elm +++ b/src/GraphQL/Request.elm @@ -1,8 +1,7 @@ -module GraphQL.Request - exposing - ( Document - , Request - ) +module GraphQL.Request exposing + ( Document + , Request + ) import GraphQL.Request.Document.AST as AST import GraphQL.Request.Document.AST.Serialize exposing (serializeDocument) diff --git a/src/GraphQL/Request/Builder.elm b/src/GraphQL/Request/Builder.elm index ae8c6a1..f57dd0e 100644 --- a/src/GraphQL/Request/Builder.elm +++ b/src/GraphQL/Request/Builder.elm @@ -1,60 +1,15 @@ -module GraphQL.Request.Builder - exposing - ( Request - , Document - , Query - , Mutation - , Fragment - , ValueSpec - , Nullable - , NonNull - , IntType - , FloatType - , StringType - , BooleanType - , IdType - , EnumType - , ListType - , ObjectType - , SelectionSpec - , Field - , FragmentSpread - , InlineFragment - , TypeCondition - , request - , requestBody - , jsonVariableValues - , responseDataDecoder - , queryDocument - , namedQueryDocument - , mutationDocument - , namedMutationDocument - , fragment - , onType - , int - , float - , string - , bool - , id - , enum - , enumWithDefault - , customScalar - , list - , nullable - , object - , extract - , with - , withLocalConstant - , withDirectives - , keyValuePairs - , dict - , assume - , field - , aliasAs - , fragmentSpread - , inlineFragment - , map - ) +module GraphQL.Request.Builder exposing + ( ValueSpec, NonNull, Nullable, IntType, FloatType, StringType, BooleanType, IdType, EnumType, ListType, ObjectType + , object, SelectionSpec, with, withLocalConstant, extract, assume, withDirectives, keyValuePairs, dict + , Field, field, aliasAs + , Fragment, FragmentSpread, InlineFragment, TypeCondition, fragment, onType, fragmentSpread, inlineFragment + , int, float, string, bool, id, enum, enumWithDefault, customScalar + , nullable + , list + , map + , Document, Query, queryDocument, namedQueryDocument, Mutation, mutationDocument, namedMutationDocument + , Request, request, requestBody, jsonVariableValues, responseDataDecoder + ) {-| This module provides an interface for building up GraphQL requests in a way that gives you everything you need to safely and conveniently integrate them with your Elm program: @@ -349,9 +304,8 @@ request vars ((Document { operation, ast, serialized }) as doc) = { documentAST = ast , documentString = serialized , variableValues = - (documentVariables doc + documentVariables doc |> Variable.extractValuesFrom vars - ) , responseDataDecoder = documentResponseDecoder doc } @@ -367,6 +321,7 @@ variableValuesToJson : List ( String, AST.ConstantValue ) -> Maybe Encode.Value variableValuesToJson kvPairs = if List.isEmpty kvPairs then Nothing + else kvPairs |> List.map (Tuple.mapSecond ValueEncode.encode) @@ -394,7 +349,7 @@ fragmentDefinitionsFromOperation (Operation { spec }) = (ValueSpec _ _ _ fragments) = spec in - fragments + fragments document : Operation operationType result vars -> Document operationType result vars @@ -409,11 +364,11 @@ document operation = ++ [ AST.OperationDefinition (operationAST operation) ] ) in - Document - { operation = operation - , ast = ast - , serialized = Serialize.serializeDocument ast - } + Document + { operation = operation + , ast = ast + , serialized = Serialize.serializeDocument ast + } {-| Take a `ValueSpec` and return a `Document` for a single query operation. The argument must be a `NonNull Object` ValueSpec, because it represents the root-level selection set of the query operation. @@ -470,14 +425,14 @@ queryOperationType = passwordVar = Var.required "password" .password Var.string in - mutationDocument <| - extract - (field "login" - [ ( "username", Arg.variable usernameVar ) - , ( "password", Arg.variable passwordVar ) - ] - (extract (field "token" [] string)) - ) + mutationDocument <| + extract + (field "login" + [ ( "username", Arg.variable usernameVar ) + , ( "password", Arg.variable passwordVar ) + ] + (extract (field "token" [] string)) + ) -} mutationDocument : @@ -564,8 +519,8 @@ If you used this code to construct a query document with `userAvatarUrls ["alice ...and a successful decoded result would look something like this: - [ ("alice", "https://cdn.example.com/alice.png") - , ("bob", "https://cdn.example.com/bob.png") + [ ( "alice", "https://cdn.example.com/alice.png" ) + , ( "bob", "https://cdn.example.com/bob.png" ) ] Note that field aliases must still conform to the GraphQL spec: @@ -588,7 +543,7 @@ keyValuePairs selections = extract selection |> map (\value -> ( Util.responseKey fieldInfo, value )) in - map2 (::) keyValueSpec accSpec + map2 (::) keyValueSpec accSpec _ -> accSpec @@ -702,11 +657,11 @@ field name arguments (ValueSpec sourceType decoder fieldVars fragments) = vars = VarUtil.mergeVariables (varsFromArguments arguments) fieldVars in - SelectionSpec - (AST.Field astFieldInfo) - decoder - vars - fragments + SelectionSpec + (AST.Field astFieldInfo) + decoder + vars + fragments updateInfoWithDirectives : List ( String, List ( String, Arg.Value vars ) ) -> { info | directives : List AST.Directive } -> { info | directives : List AST.Directive } @@ -888,11 +843,11 @@ fragmentSpread ((Fragment { name, spec }) as fragmentRecord) = (ValueSpec _ decoder _ nestedFragments) = spec in - SelectionSpec - (AST.FragmentSpread astFragmentSpreadInfo) - (Decode.maybe << decoder) - (fragmentVariables fragmentRecord) - (mergeFragments [ fragmentAST fragmentRecord ] nestedFragments) + SelectionSpec + (AST.FragmentSpread astFragmentSpreadInfo) + (Decode.maybe << decoder) + (fragmentVariables fragmentRecord) + (mergeFragments [ fragmentAST fragmentRecord ] nestedFragments) {-| Constructs a `SelectionSpec` for an object with a single inline fragment. Takes an optional `TypeCondition`, a list of optional directives, and a `ValueSpec` representing the selection set of the inline fragment. The directives are tuples whose first element is the name of the directive, and whose second element is a list of key-value tuples representing the directive arguments. Argument values are constructed using functions from [`GraphQL.Request.Builder.Value`](GraphQL-Request-Builder-Value). @@ -948,11 +903,11 @@ inlineFragment maybeTypeCondition spec = , selectionSet = selectionSetFromSourceType sourceType } in - SelectionSpec - (AST.InlineFragment astInlineFragmentInfo) - (Decode.maybe << decoder) - vars - fragments + SelectionSpec + (AST.InlineFragment astInlineFragmentInfo) + (Decode.maybe << decoder) + vars + fragments varsFromArguments : List ( String, Arg.Value vars ) -> List (Variable vars) @@ -1103,17 +1058,17 @@ enumWithFallback fallbackDecoder labelledValues = labels = List.map Tuple.first labelledValues in - ValueSpec - (SpecifiedType - { nullability = nonNullFlag - , coreType = EnumType labels - , join = enumJoin - , selectionSet = emptySelectionSet - } - ) - (always decoder) - [] - [] + ValueSpec + (SpecifiedType + { nullability = nonNullFlag + , coreType = EnumType labels + , join = enumJoin + , selectionSet = emptySelectionSet + } + ) + (always decoder) + [] + [] decoderFromEnumLabel : @@ -1124,7 +1079,7 @@ decoderFromEnumLabel : decoderFromEnumLabel fallbackDecoder labelledValues = let valueFromLabel = - (\key -> Dict.get key (Dict.fromList labelledValues)) + \key -> Dict.get key (Dict.fromList labelledValues) decoder enumString = case valueFromLabel enumString of @@ -1134,7 +1089,7 @@ decoderFromEnumLabel fallbackDecoder labelledValues = Nothing -> fallbackDecoder enumString in - decoder + decoder {-| Constructs a `ValueSpec` for a GraphQL List type. Takes any kind of `ValueSpec` to use for the items of the list, and returns a `ValueSpec` that decodes into an Elm `List`. @@ -1146,7 +1101,7 @@ list (ValueSpec itemType decoder vars fragments) = ValueSpec (SpecifiedType { nullability = nonNullFlag - , coreType = (ListType itemType) + , coreType = ListType itemType , join = listJoin , selectionSet = selectionSetFromSourceType itemType } @@ -1244,7 +1199,7 @@ map2 f (ValueSpec sourceTypeA decoderA varsA fragmentsA) (ValueSpec sourceTypeB mergedFragments = mergeFragments fragmentsA fragmentsB in - ValueSpec joinedSourceType joinedDecoder mergedVariables mergedFragments + ValueSpec joinedSourceType joinedDecoder mergedVariables mergedFragments {-| Use this function to add `SelectionSpec`s to an object `ValueSpec` pipeline: @@ -1320,7 +1275,7 @@ join a b = ( SpecifiedType typeInfoA, SpecifiedType typeInfoB ) -> SpecifiedType { typeInfoA - | coreType = (typeInfoA.join typeInfoA.coreType typeInfoB.coreType) + | coreType = typeInfoA.join typeInfoA.coreType typeInfoB.coreType , selectionSet = mergeSelectionSets typeInfoA.selectionSet typeInfoB.selectionSet } @@ -1426,7 +1381,7 @@ fragmentVariables (Fragment { directives, spec }) = (ValueSpec _ _ specVariables _) = spec in - VarUtil.mergeVariables directiveVariables specVariables + VarUtil.mergeVariables directiveVariables specVariables documentAST : Document operationType result vars -> AST.Document @@ -1447,7 +1402,7 @@ documentResponseDecoder (Document { operation }) = (Operation { spec }) = operation in - specDecoder spec + specDecoder spec documentVariables : Document operationType result vars -> List (Variable vars) @@ -1459,7 +1414,7 @@ documentVariables (Document { operation }) = (ValueSpec _ _ vars _) = spec in - vars + vars specDecoder : ValueSpec nullability coreType result vars -> Decoder result diff --git a/src/GraphQL/Request/Builder/Arg.elm b/src/GraphQL/Request/Builder/Arg.elm index 804b090..193d6f7 100644 --- a/src/GraphQL/Request/Builder/Arg.elm +++ b/src/GraphQL/Request/Builder/Arg.elm @@ -1,29 +1,14 @@ -module GraphQL.Request.Builder.Arg - exposing - ( Value - , variable - , int - , float - , string - , bool - , true - , false - , null - , enum - , object - , list - , getAST - , getVariables - ) +module GraphQL.Request.Builder.Arg exposing (Value, variable, int, float, string, bool, true, false, null, enum, object, list, getAST, getVariables) {-| The functions in this module let you construct argument values that you can pass to fields and directives using the functions in [`GraphQL.Request.Builder`](GraphQL-Request-Builder). @docs Value, variable, int, float, string, bool, true, false, null, enum, object, list, getAST, getVariables + -} -import GraphQL.Request.Document.AST as AST import GraphQL.Request.Builder.Variable as Variable import GraphQL.Request.Builder.Variable.Util as VarUtil +import GraphQL.Request.Document.AST as AST {-| An argument value, which might be either a constant or a variable. The `vars` parameter is the type of Elm value that variables will extract their values from. diff --git a/src/GraphQL/Request/Builder/TypeRef.elm b/src/GraphQL/Request/Builder/TypeRef.elm index 49d92e5..19b4807 100644 --- a/src/GraphQL/Request/Builder/TypeRef.elm +++ b/src/GraphQL/Request/Builder/TypeRef.elm @@ -1,15 +1,14 @@ -module GraphQL.Request.Builder.TypeRef - exposing - ( TypeRef - , namedType - , list - , nullable - , int - , float - , string - , boolean - , id - ) +module GraphQL.Request.Builder.TypeRef exposing + ( TypeRef + , boolean + , float + , id + , int + , list + , namedType + , nullable + , string + ) import GraphQL.Request.Document.AST as AST diff --git a/src/GraphQL/Request/Builder/Variable.elm b/src/GraphQL/Request/Builder/Variable.elm index ad344cb..a0fa865 100644 --- a/src/GraphQL/Request/Builder/Variable.elm +++ b/src/GraphQL/Request/Builder/Variable.elm @@ -1,35 +1,13 @@ -module GraphQL.Request.Builder.Variable - exposing - ( VariableSpec - , Nullable - , NonNull - , Variable - , Field - , required - , optional - , int - , float - , string - , bool - , id - , enum - , nullable - , list - , object - , field - , optionalField - , name - , toDefinitionAST - , extractValuesFrom - ) +module GraphQL.Request.Builder.Variable exposing (VariableSpec, Nullable, NonNull, Variable, Field, required, optional, int, float, string, bool, id, enum, nullable, list, object, field, optionalField, name, toDefinitionAST, extractValuesFrom) {-| The functions in this module let you define GraphQL variables that you can pass as arguments in your request documents built with the functions in [`GraphQL.Request.Builder`](GraphQL-Request-Builder). @docs VariableSpec, Nullable, NonNull, Variable, Field, required, optional, int, float, string, bool, id, enum, nullable, list, object, field, optionalField, name, toDefinitionAST, extractValuesFrom + -} -import GraphQL.Request.Document.AST as AST import GraphQL.Request.Builder.TypeRef as TypeRef exposing (TypeRef) +import GraphQL.Request.Document.AST as AST {-| A specification for the type of a `Variable` which includes enough information to extract a conforming value from an Elm value. @@ -73,6 +51,7 @@ required variableName extract (VariableSpec _ typeRef convert) = {-| Construct a `Variable` that has a default value, and therefore its `source` may or may not provide a value for it. The first three arguments are the same as for the `required` function, except that the function to extract a value from `source` must return a `Maybe` of the type expected by the `VariableSpec`. The last argument is a default value for the variable. Note that the `VariableSpec` may be either `Nullable` or `NonNull`, but in both cases the variable definition is serialized _without_ a Non-Null modifier in the GraphQL request document, because optional variables may not be Non-Null in GraphQL. If you pass a `NonNull` `VariableSpec` into this function, it just means that you won't be able to represent an explicit `null` for the variable's value. If instead you pass a `Nullable` `VariableSpec` into this function, you will be able to represent an explicit `null` value for the variable, but you'll also have to deal with double-wrapped `Maybe` values – a missing value is then represented as a `Nothing` returned from your extraction function, and a `null` value is represented as `Just Nothing`. For this reason, it is recommended that you stick to `NonNull` `VariableSpec` values here unless you really need to be able to pass `null` explictly to the GraphQL server. + -} optional : String -> (source -> Maybe a) -> VariableSpec nullability a -> a -> Variable source optional variableName extractMaybe (VariableSpec nullability typeRef convert) defaultValue = @@ -132,6 +111,7 @@ id = accessLevel : VariableSpec NonNull AccessLevel accessLevel = enum "AccessLevel" accessLevelToEnumSymbol + -} enum : String -> (source -> String) -> VariableSpec NonNull source enum typeName convert = @@ -174,6 +154,7 @@ list (VariableSpec _ typeRef convert) = , field "email" .email string ] ) + -} object : String -> List (Field source) -> VariableSpec NonNull source object typeName fields = @@ -216,6 +197,7 @@ In the following example, both the `phoneNumber` and `email` fields are optional , optionalField "phoneNumber" .phoneNumber (nullable string) ] ) + -} optionalField : String @@ -236,7 +218,7 @@ valueFromSource : source -> Variable source -> Maybe ( String, AST.ConstantValue valueFromSource source var = case var of RequiredVariable _ _ f -> - Just ( name var, (f source) ) + Just ( name var, f source ) OptionalVariable _ _ f _ -> case f source of diff --git a/src/GraphQL/Request/Builder/Variable/Util.elm b/src/GraphQL/Request/Builder/Variable/Util.elm index 9f7a89e..3787215 100644 --- a/src/GraphQL/Request/Builder/Variable/Util.elm +++ b/src/GraphQL/Request/Builder/Variable/Util.elm @@ -1,7 +1,4 @@ -module GraphQL.Request.Builder.Variable.Util - exposing - ( mergeVariables - ) +module GraphQL.Request.Builder.Variable.Util exposing (mergeVariables) import GraphQL.Request.Builder.Variable as Variable exposing (Variable) @@ -20,4 +17,4 @@ variableIsNotInList existingVars thisVar = sameASTAsThisVar var = Variable.toDefinitionAST var == thisVarAST in - not (List.any sameASTAsThisVar existingVars) + not (List.any sameASTAsThisVar existingVars) diff --git a/src/GraphQL/Request/Document/AST/Serialize.elm b/src/GraphQL/Request/Document/AST/Serialize.elm index e4baec8..3adcd0c 100644 --- a/src/GraphQL/Request/Document/AST/Serialize.elm +++ b/src/GraphQL/Request/Document/AST/Serialize.elm @@ -1,11 +1,8 @@ -module GraphQL.Request.Document.AST.Serialize - exposing - ( serializeDocument - ) +module GraphQL.Request.Document.AST.Serialize exposing (serializeDocument) import GraphQL.Request.Document.AST as AST -import String import Json.Encode as Encode +import String serializeDocument : AST.Document -> String @@ -64,6 +61,7 @@ serializeVariableDefinitions : List AST.VariableDefinition -> List String serializeVariableDefinitions defs = if List.isEmpty defs then [] + else [ "(" ++ String.join ", " (List.map serializeVariableDefinition defs) ++ ")" ] @@ -109,7 +107,7 @@ serializeCoreTypeRef coreTypeRef = name AST.ListTypeRef typeRef -> - "[" ++ (serializeTypeRef typeRef) ++ "]" + "[" ++ serializeTypeRef typeRef ++ "]" serializeValue : AST.Value variableConstraint -> String @@ -165,6 +163,7 @@ serializeArgList : List ( String, AST.ArgumentValue ) -> List String serializeArgList args = if List.isEmpty args then [] + else [ "(" ++ String.join ", " (List.map serializeKeyValuePair args) ++ ")" ] @@ -173,6 +172,7 @@ serializeSelectionSet : Int -> AST.SelectionSet -> List String serializeSelectionSet indentLevel (AST.SelectionSet selections) = if List.isEmpty selections then [] + else [ "{\n" ++ String.join "\n" (List.map (serializeSelection (indentLevel + 1)) selections) @@ -185,6 +185,7 @@ indent : Int -> String -> String indent level string = if level <= 0 then string + else " " ++ indent (level - 1) string diff --git a/src/GraphQL/Request/Document/AST/Util.elm b/src/GraphQL/Request/Document/AST/Util.elm index f9a0acd..8dec51f 100644 --- a/src/GraphQL/Request/Document/AST/Util.elm +++ b/src/GraphQL/Request/Document/AST/Util.elm @@ -1,7 +1,4 @@ -module GraphQL.Request.Document.AST.Util - exposing - ( responseKey - ) +module GraphQL.Request.Document.AST.Util exposing (responseKey) import GraphQL.Request.Document.AST as AST diff --git a/src/GraphQL/Response.elm b/src/GraphQL/Response.elm index b979092..9cd7860 100644 --- a/src/GraphQL/Response.elm +++ b/src/GraphQL/Response.elm @@ -1,9 +1,8 @@ -module GraphQL.Response - exposing - ( RequestError - , DocumentLocation - , errorsDecoder - ) +module GraphQL.Response exposing + ( DocumentLocation + , RequestError + , errorsDecoder + ) import Json.Decode as Decode exposing (Decoder) diff --git a/src/GraphQL/Schema/Decode.elm b/src/GraphQL/Schema/Decode.elm index f417569..c3b814c 100644 --- a/src/GraphQL/Schema/Decode.elm +++ b/src/GraphQL/Schema/Decode.elm @@ -1,20 +1,19 @@ -module GraphQL.Schema.Decode - exposing - ( scalarTypeDecoder - , objectTypeDecoder - , unionTypeDecoder - , interfaceTypeDecoder - , enumTypeDecoder - , inputObjectTypeDecoder - , directiveDecoder - , schemaDecoder - , introspectionResponseDecoder - ) - -import Json.Decode as Decode exposing (Decoder, field, string, bool, null, list) -import Json.Encode as Encode -import GraphQL.Schema as Schema exposing (Schema) +module GraphQL.Schema.Decode exposing + ( directiveDecoder + , enumTypeDecoder + , inputObjectTypeDecoder + , interfaceTypeDecoder + , introspectionResponseDecoder + , objectTypeDecoder + , scalarTypeDecoder + , schemaDecoder + , unionTypeDecoder + ) + import Dict exposing (Dict) +import GraphQL.Schema as Schema exposing (Schema) +import Json.Decode as Decode exposing (Decoder, bool, field, list, null, string) +import Json.Encode as Encode nullable : Decoder a -> Decoder (Maybe a) @@ -121,22 +120,22 @@ typeRefDecoder = (\kind -> case kind of "LIST" -> - (field "ofType" typeRefDecoder) + field "ofType" typeRefDecoder |> Decode.map Schema.List "NON_NULL" -> - (field "ofType" typeRefDecoder) + field "ofType" typeRefDecoder |> Decode.map Schema.NonNull _ -> - (field "name" string) + field "name" string |> Decode.map Schema.Ref ) namedTypeDecoder : Decoder Schema.NamedType namedTypeDecoder = - (field "kind" string) + field "kind" string |> Decode.andThen (\kind -> case kind of diff --git a/tests/GraphQL/Client/HttpTests.elm b/tests/GraphQL/Client/HttpTests.elm index 714bc66..1665000 100644 --- a/tests/GraphQL/Client/HttpTests.elm +++ b/tests/GraphQL/Client/HttpTests.elm @@ -1,10 +1,10 @@ module GraphQL.Client.HttpTests exposing (..) -import Test exposing (..) import Expect import GraphQL.Client.Http import GraphQL.Client.Http.Util as Util import Json.Encode +import Test exposing (..) minimalVars : Json.Encode.Value diff --git a/tests/GraphQL/Request/BuilderTests.elm b/tests/GraphQL/Request/BuilderTests.elm index ee8b843..b8af643 100644 --- a/tests/GraphQL/Request/BuilderTests.elm +++ b/tests/GraphQL/Request/BuilderTests.elm @@ -1,6 +1,6 @@ module GraphQL.Request.BuilderTests exposing (..) -import Test exposing (..) +import Dict exposing (Dict) import Expect import GraphQL.Request.Builder exposing (..) import GraphQL.Request.Builder.Arg as Arg @@ -8,7 +8,7 @@ import GraphQL.Request.Builder.Variable as Var import GraphQL.Response as Response import Json.Decode as Decode import Json.Encode as Encode -import Dict exposing (Dict) +import Test exposing (..) testDecoder : @@ -309,14 +309,14 @@ exampleMutationDocument = passwordVar = Var.required "password" .password Var.string in - mutationDocument <| - extract - (field "login" - [ ( "username", Arg.variable usernameVar ) - , ( "password", Arg.variable passwordVar ) - ] - (extract (field "token" [] string)) - ) + mutationDocument <| + extract + (field "login" + [ ( "username", Arg.variable usernameVar ) + , ( "password", Arg.variable passwordVar ) + ] + (extract (field "token" [] string)) + ) exampleMutationRequest : Request Mutation String @@ -588,7 +588,7 @@ tests = |> Expect.equal (Ok (Dict.fromList exampleKeyValuePairsDecoded)) , test "named query with arguments" <| \() -> - (namedQueryDocument "MyQuery" (extract (field "foo" [ ( "bar", Var.required "bar" identity Var.bool |> Arg.variable ) ] string))) + namedQueryDocument "MyQuery" (extract (field "foo" [ ( "bar", Var.required "bar" identity Var.bool |> Arg.variable ) ] string)) |> request True |> requestBody |> Expect.equal """query MyQuery ($bar: Boolean!) { @@ -596,7 +596,7 @@ tests = }""" , test "named mutation with no arguments" <| \() -> - (namedMutationDocument "MyMutation" (extract (field "foo" [] string))) + namedMutationDocument "MyMutation" (extract (field "foo" [] string)) |> request True |> requestBody |> Expect.equal """mutation MyMutation { diff --git a/tests/GraphQL/Schema/DecodeTests.elm b/tests/GraphQL/Schema/DecodeTests.elm index 706619e..7f53fd7 100644 --- a/tests/GraphQL/Schema/DecodeTests.elm +++ b/tests/GraphQL/Schema/DecodeTests.elm @@ -1,12 +1,12 @@ module GraphQL.Schema.DecodeTests exposing (..) -import Test exposing (..) +import Dict exposing (Dict) import Expect -import Json.Decode as Decode import GraphQL.Schema as Schema exposing (Schema) import GraphQL.Schema.Decode +import Json.Decode as Decode import String -import Dict exposing (Dict) +import Test exposing (..) scalarTypeJSON = @@ -393,29 +393,29 @@ tests = almostValidSchemaJSON |> Decode.decodeString GraphQL.Schema.Decode.schemaDecoder in - case decodeResult of - Ok schema -> - let - summary = - { queryType = schema.queryType - , mutationType = schema.mutationType - , subscriptionType = schema.subscriptionType - , typeNames = Dict.keys schema.types - , directiveNames = List.map .name schema.directives - } + case decodeResult of + Ok schema -> + let + summary = + { queryType = schema.queryType + , mutationType = schema.mutationType + , subscriptionType = schema.subscriptionType + , typeNames = Dict.keys schema.types + , directiveNames = List.map .name schema.directives + } - expected = - { queryType = "Thing" - , mutationType = Nothing - , subscriptionType = Nothing - , typeNames = [ "Boolean", "HasMass", "OnlyThing", "Thing", "ThingInput", "UnitSystem" ] - , directiveNames = [ "include" ] - } - in - Expect.equal expected summary + expected = + { queryType = "Thing" + , mutationType = Nothing + , subscriptionType = Nothing + , typeNames = [ "Boolean", "HasMass", "OnlyThing", "Thing", "ThingInput", "UnitSystem" ] + , directiveNames = [ "include" ] + } + in + Expect.equal expected summary - Err err -> - Expect.fail (Decode.errorToString err) + Err err -> + Expect.fail (Decode.errorToString err) ]