Skip to content

Commit

Permalink
sort schema imports
Browse files Browse the repository at this point in the history
  • Loading branch information
roryc89 committed Jul 31, 2024
1 parent 977598e commit f9fa0cc
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 63 deletions.
1 change: 1 addition & 0 deletions codegen/schema/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ You can edit this file as you like.
, "spec-discovery"
, "strings"
, "strings-extra"
, "tidy"
, "tidy-codegen"
, "transformers"
, "tuples"
Expand Down
12 changes: 10 additions & 2 deletions codegen/schema/src/GraphQL/Client/CodeGen/SchemaCst.purs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import Data.Unfoldable (none)
import GraphQL.Client.CodeGen.Types (InputOptions, GqlEnum)
import GraphQL.Client.CodeGen.UtilCst (qualifiedTypeToName)
import Partial.Unsafe (unsafePartial)
import PureScript.CST.Types (Module, Proper, QualifiedName)
import PureScript.CST.Types (ImportDecl, Module(..), ModuleHeader(..), ModuleName(..), Proper, QualifiedName)
import PureScript.CST.Types as CST
import Tidy.Codegen (declDerive, declNewtype, declType, docComments, leading, lineComments, typeApp, typeArrow, typeCtor, typeRecord, typeRecordEmpty, typeRow, typeString, typeWildcard)
import Tidy.Codegen.Class (class OverLeadingComments, toQualifiedName)
import Tidy.Codegen.Monad (CodegenT, codegenModule, importFrom, importType)
import Tidy.Util (nameOf)
import Unsafe.Coerce (unsafeCoerce)

gqlToPursSchema :: InputOptions -> String -> String -> AST.Document -> Array GqlEnum -> Module Void
gqlToPursSchema
Expand All @@ -42,7 +44,7 @@ gqlToPursSchema
mName
(AST.Document defs)
enums = do
unsafePartial $ codegenModule mName do
sortImports $ unsafePartial $ codegenModule mName do
directives <- importFrom directivesMName (importType "Directives")
voidT <- importQualified "Data.Void" "Void"
proxyT <- importQualified "Type.Proxy" "Proxy"
Expand Down Expand Up @@ -350,6 +352,12 @@ gqlToPursSchema
tell
$ [ schema ] <> declarations

sortImports :: Module Void -> Module Void
sortImports (Module m@{ header: ModuleHeader header }) = Module m { header = ModuleHeader header { imports = Array.sortWith getModuleName header.imports } }
where
getModuleName :: ImportDecl Void -> ModuleName
getModuleName = unwrap >>> _.module >>> nameOf

hasRootOp :: forall f. Foldable f => f AST.Definition -> AST.OperationType -> Boolean
hasRootOp defs op = defs # any case _ of
AST.Definition_TypeSystemDefinition (AST.TypeSystemDefinition_SchemaDefinition (AST.SchemaDefinition d)) ->
Expand Down
Loading

0 comments on commit f9fa0cc

Please sign in to comment.