server: remove built-in scalars from graphql schema printer

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6541
Co-authored-by: Auke Booij <164426+abooij@users.noreply.github.com>
GitOrigin-RevId: 5a2e35da2cf8718ee452429c56986066a3dc0c54
This commit is contained in:
paritosh-08 2022-11-11 00:21:25 +05:30 committed by hasura-bot
parent 4b90c8219a
commit 0b827fae66
2 changed files with 30 additions and 14 deletions

View File

@ -124,6 +124,17 @@ instance Print Void where
instance Print Name where
printP = nameP
-- Don't inline, to avoid the risk of unreasonably long code being generated
{-# NOINLINE builtInScalars #-}
builtInScalars :: [Name]
builtInScalars =
[ Name.Name "Boolean",
Name.Name "Float",
Name.Name "ID",
Name.Name "Int",
Name.Name "String"
]
renderExecutableDoc :: ExecutableDocument Name -> Text
renderExecutableDoc = Text.run . executableDocument
@ -353,7 +364,17 @@ typeSystemDefinition (TypeSystemDefinitionType typeDefn) = typeDefinitionP typeD
schemaDocument :: Printer a => SchemaDocument -> a
schemaDocument (SchemaDocument typeDefns) =
mconcat $ intersperse (textP "\n\n") $ map typeSystemDefinition $ sort typeDefns
mconcat $ intersperse (textP "\n\n") $ map typeSystemDefinition $ sort $ filter isNotBuiltInScalar typeDefns
where
-- According to https://spec.graphql.org/June2018/#sec-Scalars:
-- > When representing a GraphQL schema using the type system definition language, the builtin scalar types should
-- > be omitted for brevity.
isNotBuiltInScalar :: TypeSystemDefinition -> Bool
isNotBuiltInScalar
( TypeSystemDefinitionType
(TypeDefinitionScalar (ScalarTypeDefinition _ name _))
) = name `notElem` builtInScalars
isNotBuiltInScalar _ = True
typeDefinitionP :: Printer a => TypeDefinition () InputValueDefinition -> a
typeDefinitionP (TypeDefinitionScalar scalarDefn) = scalarTypeDefinition scalarDefn

View File

@ -14,6 +14,7 @@ import Data.Aeson qualified as J
import Data.Aeson.Key qualified as K
import Data.Aeson.KeyMap qualified as KMap
import Data.Aeson.Ordered qualified as JO
import Data.Bifunctor (Bifunctor (bimap))
import Data.HashMap.Strict qualified as Map
import Data.HashMap.Strict.InsOrd qualified as OMap
import Data.HashSet qualified as Set
@ -194,7 +195,7 @@ generateSDL (G.SchemaIntrospection sIntro) = sdl
-- first we filter out the type definitions which are not relevent such as
-- schema fields and types (starts with `__`)
typeDefns = mapMaybe filterAndWrapTypeSystemDefinition (Map.elems sIntro)
typeDefns = map (G.TypeSystemDefinitionType . filterTypeDefinition . bimap (const ()) id) (Map.elems sIntro)
-- next we get the root operation type definitions
rootOpTypeDefns =
@ -214,22 +215,16 @@ generateSDL (G.SchemaIntrospection sIntro) = sdl
getSchemaDocument :: G.SchemaDocument
getSchemaDocument =
G.SchemaDocument $
G.TypeSystemDefinitionSchema (G.SchemaDefinition Nothing (rootOpTypeDefns)) : typeDefns
G.TypeSystemDefinitionSchema (G.SchemaDefinition Nothing rootOpTypeDefns) : typeDefns
-- | Filter out schema components from sdl which are not required by apollo federation and
-- wraps it in `TypeSystemDefinition`
filterAndWrapTypeSystemDefinition :: G.TypeDefinition [G.Name] G.InputValueDefinition -> Maybe G.TypeSystemDefinition
filterAndWrapTypeSystemDefinition = \case
G.TypeDefinitionScalar (G.ScalarTypeDefinition {}) -> Nothing
G.TypeDefinitionInterface (G.InterfaceTypeDefinition a b c d _) ->
Just $ G.TypeSystemDefinitionType (G.TypeDefinitionInterface (G.InterfaceTypeDefinition a b c d ()))
-- | Filter out schema components from sdl which are not required by apollo federation
filterTypeDefinition :: G.TypeDefinition possibleTypes G.InputValueDefinition -> G.TypeDefinition possibleTypes G.InputValueDefinition
filterTypeDefinition = \case
G.TypeDefinitionObject (G.ObjectTypeDefinition a b c d e) ->
-- We are skipping the schema types here
Just . G.TypeSystemDefinitionType . G.TypeDefinitionObject $
G.TypeDefinitionObject $
G.ObjectTypeDefinition a b c d (filter (not . T.isPrefixOf "__" . G.unName . G._fldName) e)
G.TypeDefinitionUnion defn -> Just $ G.TypeSystemDefinitionType (G.TypeDefinitionUnion defn)
G.TypeDefinitionEnum defn -> Just $ G.TypeSystemDefinitionType (G.TypeDefinitionEnum defn)
G.TypeDefinitionInputObject defn -> Just $ G.TypeSystemDefinitionType (G.TypeDefinitionInputObject defn)
typeDef -> typeDef
-------------------------------------------------------------------------------
-- Related to @_entities@ field