Collect interface implementations

This commit is contained in:
Eugen Wissner 2020-11-17 08:10:32 +01:00
parent 1f4eb6fb9b
commit 86a0e00f7e
4 changed files with 35 additions and 7 deletions

View File

@ -10,6 +10,8 @@ and this project adheres to
### Added ### Added
- `Validate.Rules`: - `Validate.Rules`:
- `overlappingFieldsCanBeMergedRule` - `overlappingFieldsCanBeMergedRule`
- `Type.Schema.implementations` contains a map from interfaces and objects to
interfaces they implement.
## [0.11.0.0] - 2020-11-07 ## [0.11.0.0] - 2020-11-07
### Changed ### Changed

View File

@ -14,6 +14,7 @@ module Language.GraphQL.Type.Internal
, Type(..) , Type(..)
, directives , directives
, doesFragmentTypeApply , doesFragmentTypeApply
, implementations
, instanceOf , instanceOf
, lookupCompositeField , lookupCompositeField
, lookupInputType , lookupInputType
@ -64,26 +65,31 @@ data Schema m = Schema
(Maybe (Out.ObjectType m)) (Maybe (Out.ObjectType m))
Directives Directives
(HashMap Full.Name (Type m)) (HashMap Full.Name (Type m))
(HashMap Full.Name [Type m])
-- | Schema query type. -- | Schema query type.
query :: forall m. Schema m -> Out.ObjectType m query :: forall m. Schema m -> Out.ObjectType m
query (Schema query' _ _ _ _) = query' query (Schema query' _ _ _ _ _) = query'
-- | Schema mutation type. -- | Schema mutation type.
mutation :: forall m. Schema m -> Maybe (Out.ObjectType m) mutation :: forall m. Schema m -> Maybe (Out.ObjectType m)
mutation (Schema _ mutation' _ _ _) = mutation' mutation (Schema _ mutation' _ _ _ _) = mutation'
-- | Schema subscription type. -- | Schema subscription type.
subscription :: forall m. Schema m -> Maybe (Out.ObjectType m) subscription :: forall m. Schema m -> Maybe (Out.ObjectType m)
subscription (Schema _ _ subscription' _ _) = subscription' subscription (Schema _ _ subscription' _ _ _) = subscription'
-- | Schema directive definitions. -- | Schema directive definitions.
directives :: forall m. Schema m -> Directives directives :: forall m. Schema m -> Directives
directives (Schema _ _ _ directives' _) = directives' directives (Schema _ _ _ directives' _ _) = directives'
-- | Types referenced by the schema. -- | Types referenced by the schema.
types :: forall m. Schema m -> HashMap Full.Name (Type m) types :: forall m. Schema m -> HashMap Full.Name (Type m)
types (Schema _ _ _ _ types') = types' types (Schema _ _ _ _ types' _) = types'
-- | Interface implementations.
implementations :: forall m. Schema m -> HashMap Full.Name [Type m]
implementations (Schema _ _ _ _ _ implementations') = implementations'
-- | These types may describe the parent context of a selection set. -- | These types may describe the parent context of a selection set.
data CompositeType m data CompositeType m

View File

@ -23,6 +23,7 @@ import Language.GraphQL.Type.Internal
, Schema , Schema
, Type(..) , Type(..)
, directives , directives
, implementations
, mutation , mutation
, subscription , subscription
, query , query
@ -41,9 +42,11 @@ schema :: forall m
-> Directives -- ^ Directive definitions. -> Directives -- ^ Directive definitions.
-> Schema m -- ^ Schema. -> Schema m -- ^ Schema.
schema queryRoot mutationRoot subscriptionRoot directiveDefinitions = schema queryRoot mutationRoot subscriptionRoot directiveDefinitions =
Internal.Schema queryRoot mutationRoot subscriptionRoot allDirectives collectedTypes Internal.Schema queryRoot mutationRoot subscriptionRoot
allDirectives collectedTypes collectedImplementations
where where
collectedTypes = collectReferencedTypes queryRoot mutationRoot subscriptionRoot collectedTypes = collectReferencedTypes queryRoot mutationRoot subscriptionRoot
collectedImplementations = collectImplementations collectedTypes
allDirectives = HashMap.union directiveDefinitions defaultDirectives allDirectives = HashMap.union directiveDefinitions defaultDirectives
defaultDirectives = HashMap.fromList defaultDirectives = HashMap.fromList
[ ("skip", skipDirective) [ ("skip", skipDirective)
@ -153,3 +156,20 @@ collectReferencedTypes queryRoot mutationRoot subscriptionRoot =
polymorphicTraverser interfaces fields polymorphicTraverser interfaces fields
= flip (foldr visitFields) fields = flip (foldr visitFields) fields
. flip (foldr traverseInterfaceType) interfaces . flip (foldr traverseInterfaceType) interfaces
-- | Looks for objects and interfaces under the schema types and collects the
-- interfaces they implement.
collectImplementations :: forall m
. HashMap Full.Name (Type m)
-> HashMap Full.Name [Type m]
collectImplementations = HashMap.foldr go HashMap.empty
where
go implementation@(InterfaceType interfaceType) accumulator =
let Out.InterfaceType _ _ interfaces _ = interfaceType
in foldr (add implementation) accumulator interfaces
go implementation@(ObjectType objectType) accumulator =
let Out.ObjectType _ _ interfaces _ = objectType
in foldr (add implementation) accumulator interfaces
go _ accumulator = accumulator
add implementation (Out.InterfaceType typeName _ _ _) accumulator =
HashMap.insertWith (++) typeName [implementation] accumulator

View File

@ -1,4 +1,4 @@
resolver: lts-16.21 resolver: lts-16.22
packages: packages:
- . - .