mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-22 15:01:39 +03:00
5920134dcb
### Motivation #2338 introduced a way to validate REST queries against the metadata after a change, to properly report any inconsistency that would emerge from a change in the underlying structure of our schema. However, the way this was done was quite complex and error-prone. Namely: we would use the generated schema parsers to statically execute an introspection query, similar to the one we use for remote schemas, then parse the resulting bytestring as it were coming from a remote schema. This led to several issues: the code was using remote schema primitives, and was associated with remote schema code, despite being unrelated, which led to absurd situations like creating fake `Variable`s whose type was also their name. A lot of the code had to deal with the fact that we might fail to re-parse our own schema. Additionally, some of it was dead code, that for some reason GHC did not warn about? But more fundamentally, this architecture decision creates a dependency between unrelated pieces of the engine: modifying the internal processing of root fields or the introspection of remote schemas now risks impacting the unrelated `OpenAPI` feature. ### Description This PR decouples that process from the remote schema introspection logic and from the execution engine by making `Analyse` and `OpenAPI` work on the generic `G.SchemaIntrospection` instead. To accomplish this, it: - adds `GraphQL.Parser.Schema.Convert`, to convert from our "live" schema back to a flat `SchemaIntrospection` - persists in the schema cache the `admin` introspection generated when building the schema, and uses it both for validation and for generating the `OpenAPI`. ### Known issues and limitations This adds a bit of memory pressure to the engine, as we persist the entire schema in the schema cache. This might be acceptable in the short-term, but we have several potential ideas going forward should this be a problem: - cache the result of `Analyze`: when it becomes possible to build the `OpenAPI` purely with the result of `Analyze` without any additional schema information, then we could cache that instead, reducing the footprint - caching the `OpenAPI`: if it doesn't need to change every time the endpoint is queried, then it should be possible to cache the entire `OpenAPI` object instead of the schema - cache a copy of the `FieldParsers` used to generate the schema: as those are persisted through the GraphQL `Context`, and are the only input required to generate the `Schema`, making them accessible in the schema cache would allow us to have the exact same feature with no additional memory cost, at the price of a slightly slower and more complicated process (need to rebuild the `Schema` every time we query the OpenAPI endpoint) - cache nothing at all, and rebuild the admin schema from scratch every time. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3962 Co-authored-by: paritosh-08 <85472423+paritosh-08@users.noreply.github.com> GitOrigin-RevId: a8b9808170b231fdf6787983b4a9ed286cde27e0
110 lines
3.8 KiB
Haskell
110 lines
3.8 KiB
Haskell
-- | Provides utilities to convert a runtime schema back to a static
|
|
-- intrsospection schema.
|
|
module Hasura.GraphQL.Parser.Schema.Convert
|
|
( convertToSchemaIntrospection,
|
|
)
|
|
where
|
|
|
|
import Hasura.GraphQL.Parser.Schema
|
|
import Hasura.Prelude
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- | Convert back from our "live" schema representation into a flat
|
|
-- static set of definitions.
|
|
convertToSchemaIntrospection :: Schema -> G.SchemaIntrospection
|
|
convertToSchemaIntrospection = G.SchemaIntrospection . fmap convertType . sTypes
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
convertType :: SomeDefinitionTypeInfo -> G.TypeDefinition [G.Name] G.InputValueDefinition
|
|
convertType (SomeDefinitionTypeInfo Definition {..}) = case dInfo of
|
|
TIScalar ->
|
|
G.TypeDefinitionScalar $
|
|
G.ScalarTypeDefinition
|
|
{ G._stdDescription = dDescription,
|
|
G._stdName = dName,
|
|
G._stdDirectives = noDirectives
|
|
}
|
|
TIEnum enumInfo ->
|
|
G.TypeDefinitionEnum $
|
|
G.EnumTypeDefinition
|
|
{ G._etdDescription = dDescription,
|
|
G._etdName = dName,
|
|
G._etdDirectives = noDirectives,
|
|
G._etdValueDefinitions = map convertEnumValue $ toList enumInfo
|
|
}
|
|
TIInputObject (InputObjectInfo values) ->
|
|
G.TypeDefinitionInputObject $
|
|
G.InputObjectTypeDefinition
|
|
{ G._iotdDescription = dDescription,
|
|
G._iotdName = dName,
|
|
G._iotdDirectives = noDirectives,
|
|
G._iotdValueDefinitions = map convertInputField values
|
|
}
|
|
TIObject (ObjectInfo fields interfaces) ->
|
|
G.TypeDefinitionObject $
|
|
G.ObjectTypeDefinition
|
|
{ G._otdDescription = dDescription,
|
|
G._otdName = dName,
|
|
G._otdDirectives = noDirectives,
|
|
G._otdImplementsInterfaces = map getDefinitionName interfaces,
|
|
G._otdFieldsDefinition = map convertField fields
|
|
}
|
|
TIInterface (InterfaceInfo fields possibleTypes) ->
|
|
G.TypeDefinitionInterface $
|
|
G.InterfaceTypeDefinition
|
|
{ G._itdDescription = dDescription,
|
|
G._itdName = dName,
|
|
G._itdDirectives = noDirectives,
|
|
G._itdFieldsDefinition = map convertField fields,
|
|
G._itdPossibleTypes = map getDefinitionName possibleTypes
|
|
}
|
|
TIUnion (UnionInfo possibleTypes) ->
|
|
G.TypeDefinitionUnion $
|
|
G.UnionTypeDefinition
|
|
{ G._utdDescription = dDescription,
|
|
G._utdName = dName,
|
|
G._utdDirectives = noDirectives,
|
|
G._utdMemberTypes = map getDefinitionName possibleTypes
|
|
}
|
|
|
|
convertEnumValue :: Definition EnumValueInfo -> G.EnumValueDefinition
|
|
convertEnumValue Definition {..} =
|
|
G.EnumValueDefinition
|
|
{ G._evdDescription = dDescription,
|
|
G._evdName = G.EnumValue dName,
|
|
G._evdDirectives = noDirectives
|
|
}
|
|
|
|
convertInputField :: Definition InputFieldInfo -> G.InputValueDefinition
|
|
convertInputField Definition {..} = case dInfo of
|
|
InputFieldInfo typeInfo defaultValue ->
|
|
G.InputValueDefinition
|
|
{ G._ivdDescription = dDescription,
|
|
G._ivdName = dName,
|
|
G._ivdType = toGraphQLType typeInfo,
|
|
G._ivdDefaultValue = defaultValue,
|
|
G._ivdDirectives = noDirectives
|
|
}
|
|
|
|
convertField :: Definition FieldInfo -> G.FieldDefinition G.InputValueDefinition
|
|
convertField Definition {..} = case dInfo of
|
|
FieldInfo arguments typeInfo ->
|
|
G.FieldDefinition
|
|
{ G._fldDescription = dDescription,
|
|
G._fldName = dName,
|
|
G._fldArgumentsDefinition = map convertInputField arguments,
|
|
G._fldType = toGraphQLType typeInfo,
|
|
G._fldDirectives = noDirectives
|
|
}
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
getDefinitionName :: Definition a -> G.Name
|
|
getDefinitionName = dName
|
|
|
|
noDirectives :: [G.Directive Void]
|
|
noDirectives = []
|