3434-- performed by "Hasura.GraphQL.Execute.Resolve", but for fragment definitions
3535-- rather than variables.
3636module Hasura.GraphQL.Execute.Inline
37- ( inlineSelectionSet ,
37+ ( InlineMT ,
38+ InlineM ,
39+ inlineSelectionSet ,
40+ inlineField ,
41+ runInlineMT ,
42+ runInlineM ,
3843 )
3944where
4045
@@ -74,6 +79,34 @@ type MonadInline m =
7479 MonadState InlineState m
7580 )
7681
82+ type InlineMT m a = MonadError QErr m = > (StateT InlineState (ReaderT InlineEnv m )) a
83+
84+ type InlineM a = InlineMT (Except QErr ) a
85+
86+ {-# INLINE runInlineMT #-}
87+ runInlineMT ::
88+ forall m a .
89+ (MonadError QErr m ) =>
90+ HashMap Name FragmentDefinition ->
91+ InlineMT m a ->
92+ m a
93+ runInlineMT uniqueFragmentDefinitions =
94+ flip
95+ runReaderT
96+ InlineEnv
97+ { _ieFragmentDefinitions = uniqueFragmentDefinitions,
98+ _ieFragmentStack = []
99+ }
100+ . flip evalStateT InlineState {_isFragmentCache = mempty }
101+
102+ {-# INLINE runInlineM #-}
103+ runInlineM ::
104+ forall a .
105+ HashMap Name FragmentDefinition ->
106+ InlineM a ->
107+ Either QErr a
108+ runInlineM fragments = runExcept . runInlineMT fragments
109+
77110-- | Inlines all fragment spreads in a 'SelectionSet'; see the module
78111-- documentation for "Hasura.GraphQL.Execute.Inline" for details.
79112inlineSelectionSet ::
@@ -106,6 +139,8 @@ inlineSelectionSet fragmentDefinitions selectionSet = do
106139 Set. toList $
107140 Set. difference definedFragmentNames usedFragmentNames
108141 )
142+ -- The below code is a manual inlining of 'runInlineMT', as appearently the
143+ -- inlining optimization does not trigger, even with the INLINE pragma.
109144 traverse inlineSelection selectionSet
110145 & flip evalStateT InlineState {_isFragmentCache = mempty }
111146 & flip
@@ -128,18 +163,21 @@ inlineSelection ::
128163 MonadInline m =>
129164 Selection FragmentSpread Name ->
130165 m (Selection NoFragments Name )
131- inlineSelection (SelectionField field@ Field {_fSelectionSet}) =
132- withPathK " selectionSet" $
133- withPathK (unName $ _fName field) $ do
134- selectionSet <- traverse inlineSelection _fSelectionSet
135- pure $! SelectionField field {_fSelectionSet = selectionSet}
166+ inlineSelection (SelectionField field) =
167+ withPathK " selectionSet" $ SelectionField <$> inlineField field
136168inlineSelection (SelectionFragmentSpread spread) =
137169 withPathK " selectionSet" $
138170 SelectionInlineFragment <$> inlineFragmentSpread spread
139171inlineSelection (SelectionInlineFragment fragment@ InlineFragment {_ifSelectionSet}) = do
140172 selectionSet <- traverse inlineSelection _ifSelectionSet
141173 pure $! SelectionInlineFragment fragment {_ifSelectionSet = selectionSet}
142174
175+ {-# INLINE inlineField #-}
176+ inlineField :: MonadInline m => Field FragmentSpread Name -> m (Field NoFragments Name )
177+ inlineField field@ (Field {_fSelectionSet}) = withPathK (unName $ _fName field) $ do
178+ selectionSet <- traverse inlineSelection _fSelectionSet
179+ pure $! field {_fSelectionSet = selectionSet}
180+
143181inlineFragmentSpread ::
144182 MonadInline m =>
145183 FragmentSpread Name ->
0 commit comments