diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4f77ffa8..5f456a4f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: steps: - uses: actions/checkout@v2 - - uses: purescript-contrib/setup-purescript@main + - uses: purescript-contrib/setup-purescript@1.1 - name: Cache PureScript dependencies uses: actions/cache@v2 diff --git a/src/GraphQL/Client/CodeGen/Schema.purs b/src/GraphQL/Client/CodeGen/Schema.purs index 155cfb7c..f9690fef 100644 --- a/src/GraphQL/Client/CodeGen/Schema.purs +++ b/src/GraphQL/Client/CodeGen/Schema.purs @@ -20,7 +20,7 @@ import Data.List (List, mapMaybe) import Data.List as List import Data.Map (lookup) import Data.Map as Map -import Data.Maybe (Maybe(..), maybe) +import Data.Maybe (Maybe(..), fromMaybe, maybe) import Data.Monoid (guard) import Data.Newtype (unwrap) import Data.String (Pattern(..), codePointFromChar, contains, joinWith) @@ -191,20 +191,23 @@ gqlToPursMainSchemaCode { externalTypes, fieldTypeOverrides, useNewtypesForRecor scalarTypeDefinitionToPurs :: AST.ScalarTypeDefinition -> String scalarTypeDefinitionToPurs (AST.ScalarTypeDefinition { description, name, directives }) = - guard (notElem tName builtInTypes) case lookup tName externalTypes of - Nothing -> unknownDebugMarker tName - Just external -> - descriptionToDocComment description - <> "type " - <> tName - <> " = " - <> external.moduleName - <> "." - <> external.typeName + guard (notElem tName builtInTypes) + $ descriptionToDocComment description + <> "type " + <> tName + <> " = " + <> typeAndModule.moduleName + <> "." + <> typeAndModule.typeName where tName = typeName name - unknownDebugMarker tn = "unknown type: " <> show tn + typeAndModule = + lookup tName externalTypes + # fromMaybe + { moduleName: "Data.Foreign" + , typeName: "Foreign -- Unknown scalar type. Add " <> tName <> " to externalTypes in codegen options override this behaviour" + } builtInTypes = [ "Int", "Number", "String", "Boolean" ] diff --git a/test/GraphQL/Client/CodeGen/SchemaFromGqlToPurs.Test.purs b/test/GraphQL/Client/CodeGen/SchemaFromGqlToPurs.Test.purs index d814c5bd..014bf724 100644 --- a/test/GraphQL/Client/CodeGen/SchemaFromGqlToPurs.Test.purs +++ b/test/GraphQL/Client/CodeGen/SchemaFromGqlToPurs.Test.purs @@ -262,6 +262,45 @@ instance argToGqlQuery :: (Newtype Query {| p}, RecordArg p a u) => ArgGql Quer ) ` result + it "handles unknown scalar types" do + let + gql = + """ +schema { + query: Query +} + +type Query { + int: Int + my_type_a: SomethingUnknown! + my_type_b: SomethingElseUnknown + my_type_c: [AlsoUnkown!]! +} + +scalar SomethingUnknown +scalar SomethingElseUnknown +scalar AlsoUnkown + """ + + result = """ +type Query = Query + +newtype Query = Query + { int :: (Maybe Int) + , my_type_a :: SomethingUnknown + , my_type_b :: (Maybe SomethingElseUnknown) + , my_type_c :: (Array AlsoUnkown) + } +derive instance newtypeQuery :: Newtype Query _ +instance argToGqlQuery :: (Newtype Query {| p}, RecordArg p a u) => ArgGql Query { | a } + +type SomethingUnknown = Data.Foreign.Foreign -- Unknown scalar type. Add SomethingUnknown to externalTypes in codegen options override this behaviour + +type SomethingElseUnknown = Data.Foreign.Foreign -- Unknown scalar type. Add SomethingElseUnknown to externalTypes in codegen options override this behaviour + +type AlsoUnkown = Data.Foreign.Foreign -- Unknown scalar type. Add AlsoUnkown to externalTypes in codegen options override this behaviour""" + + gql `shouldParseTo` result where mkMap :: forall v. Array (Tuple String (Array (Tuple String v))) -> Map String (Map String v) mkMap = Map.fromFoldable >>> map Map.fromFoldable