Skip to content

Commit

Permalink
allow unknown scalar types as foreign #14
Browse files Browse the repository at this point in the history
  • Loading branch information
roryc89 committed Mar 16, 2021
1 parent 763f31b commit 60c7b75
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions src/GraphQL/Client/CodeGen/Schema.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" ]

Expand Down
39 changes: 39 additions & 0 deletions test/GraphQL/Client/CodeGen/SchemaFromGqlToPurs.Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 60c7b75

Please sign in to comment.